Sha256: 400b4285f76be41fda5e5add1c510bce59ba8728d35fd82b540c817d74783027

Contents?: true

Size: 1.1 KB

Versions: 4

Compression:

Stored size: 1.1 KB

Contents

require_relative 'validator/symmetry'
require_relative 'validator/errors'
require_relative 'yaml_ast_proxy'

class I18nFlow::Corrector
  attr_reader :ast_1
  attr_reader :ast_2

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

  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
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
i18n_flow-0.2.3 lib/i18n_flow/corrector.rb
i18n_flow-0.2.2 lib/i18n_flow/corrector.rb
i18n_flow-0.2.1 lib/i18n_flow/corrector.rb
i18n_flow-0.2.0 lib/i18n_flow/corrector.rb