class Querly::CLI::Rules

Attributes

config_path[R]
ids[R]
stdout[R]

Public Class Methods

new(config_path:, ids:, stdout: STDOUT) click to toggle source
# File lib/querly/cli/rules.rb, line 8
def initialize(config_path:, ids:, stdout: STDOUT)
  @config_path = config_path
  @stdout = stdout
  @ids = ids
end

Public Instance Methods

config() click to toggle source
# File lib/querly/cli/rules.rb, line 14
def config
  yaml = YAML.load(config_path.read)
  @config ||= Config.load(yaml, config_path: config_path, root_dir: config_path.parent.realpath)
end
empty(array) { |to_a| ... } click to toggle source
# File lib/querly/cli/rules.rb, line 60
def empty(array)
  unless array.empty?
    yield array.to_a
  end
end
rule_to_yaml(rule) click to toggle source
# File lib/querly/cli/rules.rb, line 32
def rule_to_yaml(rule)
  { "id" => rule.id }.tap do |hash|
    singleton rule.sources do |a|
      hash["pattern"] = a
    end

    singleton rule.messages do |a|
      hash["message"] = a
    end

    empty rule.tags do |a|
      hash["tags"] = a
    end

    singleton rule.justifications do |a|
      hash["justification"] = a
    end

    singleton rule.before_examples do |a|
      hash["before"] = a
    end

    singleton rule.after_examples do |a|
      hash["after"] = a
    end
  end
end
run() click to toggle source
# File lib/querly/cli/rules.rb, line 19
def run
  rules = config.rules.select {|rule| test_rule(rule) }
  stdout.puts YAML.dump(rules.map {|rule| rule_to_yaml(rule) })
end
singleton(array) { |first| ... } click to toggle source
# File lib/querly/cli/rules.rb, line 66
def singleton(array)
  empty(array) do
    if array.length == 1
      yield array.first
    else
      yield array.to_a
    end
  end
end
test_rule(rule) click to toggle source
# File lib/querly/cli/rules.rb, line 24
def test_rule(rule)
  if ids.empty?
    true
  else
    ids.any? {|id| rule.match?(identifier: id) }
  end
end