Class: I18nFlow::Corrector

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n_flow/corrector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ast_1, ast_2) ⇒ Corrector

Returns a new instance of Corrector.



9
10
11
12
# File 'lib/i18n_flow/corrector.rb', line 9

def initialize(ast_1, ast_2)
  @ast_1 = ast_1
  @ast_2 = ast_2
end

Instance Attribute Details

#ast_1Object (readonly)

Returns the value of attribute ast_1.



6
7
8
# File 'lib/i18n_flow/corrector.rb', line 6

def ast_1
  @ast_1
end

#ast_2Object (readonly)

Returns the value of attribute ast_2.



7
8
9
# File 'lib/i18n_flow/corrector.rb', line 7

def ast_2
  @ast_2
end

Instance Method Details

#correct!Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/i18n_flow/corrector.rb', line 14

def correct!
  n1, n2 = [ast_1, ast_2]
    .map { |n| I18nFlow::YamlAstProxy.first_value_node_of(n) }
    .map { |n| I18nFlow::YamlAstProxy.create(n) }

  errors = I18nFlow::Validator::Symmetry.new(n2, n1)
    .tap(&:validate!)
    .errors

  errors.each do |error|
    case error
    when I18nFlow::Validator::MissingKeyError
      src_node = error.src_node.clone
      I18nFlow::YamlAstProxy.mark_as_todo(src_node)
      error.dest_node[error.dest_key] = src_node.node
    when I18nFlow::Validator::ExtraKeyError
      if error.dest_node.mapping?
        error.dest_node.delete(error.dest_key)
      elsif error.dest_node.sequence?
        error.dest_node.delete_at(error.dest_key)
      end
    when I18nFlow::Validator::TodoContentError
      error.dest_node.node.value = error.src_node.node.value
    end
  end
end