Class: Textus::Maintenance::RuleLint

Inherits:
Object
  • Object
show all
Extended by:
Contract::DSL
Defined in:
lib/textus/maintenance/rule_lint.rb

Overview

Compare the live manifest’s ‘rules:` block against a candidate YAML string. Returns a Plan describing rule additions/removals/ changes. Does NOT write anything.

Instance Method Summary collapse

Methods included from Contract::DSL

arg, contract, contract?, response, summary, surfaces, verb

Constructor Details

#initialize(container:, call:) ⇒ RuleLint

Returns a new instance of RuleLint.



17
18
19
20
21
# File 'lib/textus/maintenance/rule_lint.rb', line 17

def initialize(container:, call:)
  @container = container
  @call      = call
  @root      = container.root
end

Instance Method Details

#call(candidate_yaml:) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/textus/maintenance/rule_lint.rb', line 23

def call(candidate_yaml:)
  live_rules      = current_rules
  candidate_rules = parse_candidate(candidate_yaml)

  live_by_match      = live_rules.to_h { |r| [r["match"], r] }
  candidate_by_match = candidate_rules.to_h { |r| [r["match"], r] }

  steps = (candidate_by_match.keys - live_by_match.keys).map do |m|
    { "op" => "add_rule", "match" => m, "rule" => candidate_by_match[m] }
  end
  (live_by_match.keys - candidate_by_match.keys).each do |m|
    steps << { "op" => "remove_rule", "match" => m }
  end
  (live_by_match.keys & candidate_by_match.keys).each do |m|
    next if live_by_match[m] == candidate_by_match[m]

    steps << { "op" => "change_rule", "match" => m,
               "from" => live_by_match[m], "to" => candidate_by_match[m] }
  end

  Plan.new(steps: steps, warnings: [])
end