/
config_store.rb
62 lines (52 loc) · 1.77 KB
/
config_store.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# frozen_string_literal: true
module RuboCop
# Handles caching of configurations and association of inspected
# ruby files to configurations.
class ConfigStore
def initialize
# @options_config stores a config that is specified in the command line.
# This takes precedence over configs located in any directories
@options_config = nil
# @path_cache maps directories to configuration paths. We search
# for .rubocop.yml only if we haven't already found it for the
# given directory.
@path_cache = {}
# @object_cache maps configuration file paths to
# configuration objects so we only need to load them once.
@object_cache = {}
end
def options_config=(options_config)
loaded_config = ConfigLoader.load_file(options_config)
@options_config = ConfigLoader.merge_with_default(loaded_config,
options_config)
end
def force_default_config!
@options_config = ConfigLoader.default_configuration
end
def for_file(file)
for_dir(File.dirname(file))
end
def for_pwd
for_dir(Dir.pwd)
end
# If type (file/dir) is known beforehand,
# prefer using #for_file or #for_dir for improved performance
def for(file_or_dir)
dir = if File.directory?(file_or_dir)
file_or_dir
else
File.dirname(file_or_dir)
end
for_dir(dir)
end
def for_dir(dir)
return @options_config if @options_config
@path_cache[dir] ||= ConfigLoader.configuration_file_for(dir)
path = @path_cache[dir]
@object_cache[path] ||= begin
print "For #{dir}: " if ConfigLoader.debug?
ConfigLoader.configuration_from_file(path)
end
end
end
end