Class: Textus::Handlers::Maintenance::RuleLint

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

Instance Method Summary collapse

Constructor Details

#initialize(manifest:) ⇒ RuleLint

Returns a new instance of RuleLint.



7
8
9
# File 'lib/textus/handlers/maintenance/rule_lint.rb', line 7

def initialize(manifest:)
  @manifest = manifest
end

Instance Method Details

#call(command, _call) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/textus/handlers/maintenance/rule_lint.rb', line 11

def call(command, _call)
  root = @manifest.data.root
  live_rules = current_rules(root)
  candidate_result = parse_candidate(command.candidate_yaml)
  return candidate_result if candidate_result.is_a?(Value::Result) && candidate_result.failure?

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

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

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

  Value::Result.success(Textus::Store::Jobs::Plan.new(steps: steps, warnings: []))
end