Class: Textus::Application::Maintenance::RuleLint::Impl

Inherits:
Object
  • Object
show all
Defined in:
lib/textus/application/maintenance/rule_lint.rb

Instance Method Summary collapse

Constructor Details

#initialize(ctx:, caps:) ⇒ Impl

Returns a new instance of Impl.



15
16
17
18
# File 'lib/textus/application/maintenance/rule_lint.rb', line 15

def initialize(ctx:, caps:)
  @ctx  = ctx
  @root = caps.root
end

Instance Method Details

#call(candidate_yaml:) ⇒ Object



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

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