Class: Querly::CLI::Rules
- Inherits:
-
Object
- Object
- Querly::CLI::Rules
- Defined in:
- lib/querly/cli/rules.rb
Instance Attribute Summary collapse
-
#config_path ⇒ Object
readonly
Returns the value of attribute config_path.
-
#ids ⇒ Object
readonly
Returns the value of attribute ids.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Instance Method Summary collapse
- #config ⇒ Object
- #empty(array) ⇒ Object
-
#initialize(config_path:, ids:, stdout: STDOUT) ⇒ Rules
constructor
A new instance of Rules.
- #rule_to_yaml(rule) ⇒ Object
- #run ⇒ Object
- #singleton(array) ⇒ Object
- #test_rule(rule) ⇒ Object
Constructor Details
#initialize(config_path:, ids:, stdout: STDOUT) ⇒ Rules
Returns a new instance of Rules.
8 9 10 11 12 |
# File 'lib/querly/cli/rules.rb', line 8 def initialize(config_path:, ids:, stdout: STDOUT) @config_path = config_path @stdout = stdout @ids = ids end |
Instance Attribute Details
#config_path ⇒ Object (readonly)
Returns the value of attribute config_path.
4 5 6 |
# File 'lib/querly/cli/rules.rb', line 4 def config_path @config_path end |
#ids ⇒ Object (readonly)
Returns the value of attribute ids.
6 7 8 |
# File 'lib/querly/cli/rules.rb', line 6 def ids @ids end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
5 6 7 |
# File 'lib/querly/cli/rules.rb', line 5 def stdout @stdout end |
Instance Method Details
#config ⇒ Object
14 15 16 17 |
# 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) ⇒ Object
60 61 62 63 64 |
# File 'lib/querly/cli/rules.rb', line 60 def empty(array) unless array.empty? yield array.to_a end end |
#rule_to_yaml(rule) ⇒ Object
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 |
# 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. do |a| hash["message"] = a end empty rule. 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 ⇒ Object
19 20 21 22 |
# 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) ⇒ Object
66 67 68 69 70 71 72 73 74 |
# 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) ⇒ Object
24 25 26 27 28 29 30 |
# 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 |