class Querly::Config::Factory

Attributes

config_path[R]
root_dir[R]
stderr[R]
yaml[R]

Public Class Methods

new(yaml, config_path:, root_dir:, stderr: STDERR) click to toggle source
# 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

Public Instance Methods

config() click to toggle source
# 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

    if import["require"]
      stderr.puts "Require rules from #{import["require"]}..."
      require import["require"]
    end
  end

  rules.concat Querly.required_rules

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

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