Class: Querly::Config::Factory

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(yaml, config_path:, root_dir:, stderr: STDERR) ⇒ Factory

Returns a new instance of Factory.



50
51
52
53
54
55
# File 'lib/querly/config.rb', line 50

def initialize(yaml, config_path:, root_dir:, stderr: STDERR)
  @yaml = yaml
  @config_path = config_path
  @root_dir = root_dir
  @stderr = stderr
end

Instance Attribute Details

#config_pathObject (readonly)

Returns the value of attribute config_path.



48
49
50
# File 'lib/querly/config.rb', line 48

def config_path
  @config_path
end

#root_dirObject (readonly)

Returns the value of attribute root_dir.



46
47
48
# File 'lib/querly/config.rb', line 46

def root_dir
  @root_dir
end

#stderrObject (readonly)

Returns the value of attribute stderr.



47
48
49
# File 'lib/querly/config.rb', line 47

def stderr
  @stderr
end

#yamlObject (readonly)

Returns the value of attribute yaml.



45
46
47
# File 'lib/querly/config.rb', line 45

def yaml
  @yaml
end

Instance Method Details

#configObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/querly/config.rb', line 57

def config
  if yaml["tagging"]
    stderr.puts "tagging is deprecated and ignored"
  end

  rules = Array(yaml["rules"]).map {|hash| Rule.load(hash) }
  preprocessors = (yaml["preprocessor"] || {}).each.with_object({}) do |(key, value), hash|
    hash[key] = Preprocessor.new(ext: key, command: value)
  end

  imports = Array(yaml["import"])
  imports.each do |import|
    if import["load"]
      load_pattern = Pathname(import["load"])
      load_pattern = config_path.parent + load_pattern if load_pattern.relative?

      Pathname.glob(load_pattern.to_s) do |path|
        stderr.puts "Loading rules from #{path}..."
        YAML.load(path.read).each do |hash|
          rules << Rule.load(hash)
        end
      end
    end
  end

  checks = Array(yaml["check"]).map {|hash| Check.load(hash) }

  Config.new(rules: rules, preprocessors: preprocessors, checks: checks, root_dir: root_dir)
end