Class: Querly::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/querly/config.rb

Defined Under Namespace

Classes: Factory

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules:, preprocessors:, root_dir:, checks:) ⇒ Config

Returns a new instance of Config.



9
10
11
12
13
14
15
# File 'lib/querly/config.rb', line 9

def initialize(rules:, preprocessors:, root_dir:, checks:)
  @rules = rules
  @root_dir = root_dir
  @preprocessors = preprocessors
  @checks = checks
  @rules_cache = {}
end

Instance Attribute Details

#checksObject (readonly)

Returns the value of attribute checks.



6
7
8
# File 'lib/querly/config.rb', line 6

def checks
  @checks
end

#preprocessorsObject (readonly)

Returns the value of attribute preprocessors.



4
5
6
# File 'lib/querly/config.rb', line 4

def preprocessors
  @preprocessors
end

#root_dirObject (readonly)

Returns the value of attribute root_dir.



5
6
7
# File 'lib/querly/config.rb', line 5

def root_dir
  @root_dir
end

#rulesObject (readonly)

Returns the value of attribute rules.



3
4
5
# File 'lib/querly/config.rb', line 3

def rules
  @rules
end

#rules_cacheObject (readonly)

Returns the value of attribute rules_cache.



7
8
9
# File 'lib/querly/config.rb', line 7

def rules_cache
  @rules_cache
end

Class Method Details

.load(hash, config_path:, root_dir:, stderr: STDERR) ⇒ Object



17
18
19
# File 'lib/querly/config.rb', line 17

def self.load(hash, config_path:, root_dir:, stderr: STDERR)
  Factory.new(hash, config_path: config_path, root_dir: root_dir, stderr: stderr).config
end

Instance Method Details

#all_rulesObject



21
22
23
# File 'lib/querly/config.rb', line 21

def all_rules
  @all_rules ||= Set.new(rules)
end

#relative_path_from_root(path) ⇒ Object



25
26
27
# File 'lib/querly/config.rb', line 25

def relative_path_from_root(path)
  path.absolute? ? path.relative_path_from(root_dir) : path.cleanpath
end

#rules_for_path(path) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/querly/config.rb', line 29

def rules_for_path(path)
  relative_path = relative_path_from_root(path)
  matching_checks = checks.select {|check| check.match?(path: relative_path) }

  if rules_cache.key?(matching_checks)
    rules_cache[matching_checks]
  else
    matching_checks.flat_map(&:rules).inject(all_rules) do |rules, query|
      query.apply(current: rules, all: all_rules)
    end.tap do |rules|
      rules_cache[matching_checks] = rules
    end
  end
end