Class: I18nFlow::Validator::Multiplexer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository:, valid_locales:, locale_pairs:, linters: %i[file_scope symmetry]) ⇒ Multiplexer

Returns a new instance of Multiplexer.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/i18n_flow/validator/multiplexer.rb', line 12

def initialize(
  repository:,
  valid_locales:,
  locale_pairs:,
  linters: %i[file_scope symmetry]
)
  @repository    = repository
  @valid_locales = valid_locales
  @locale_pairs  = locale_pairs
  @linters       = linters
end

Instance Attribute Details

#lintersObject (readonly)

Returns the value of attribute linters.



10
11
12
# File 'lib/i18n_flow/validator/multiplexer.rb', line 10

def linters
  @linters
end

#locale_pairsObject (readonly)

Returns the value of attribute locale_pairs.



9
10
11
# File 'lib/i18n_flow/validator/multiplexer.rb', line 9

def locale_pairs
  @locale_pairs
end

#repositoryObject (readonly)

Returns the value of attribute repository.



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

def repository
  @repository
end

#valid_localesObject (readonly)

Returns the value of attribute valid_locales.



8
9
10
# File 'lib/i18n_flow/validator/multiplexer.rb', line 8

def valid_locales
  @valid_locales
end

Instance Method Details

#errorsObject



54
55
56
# File 'lib/i18n_flow/validator/multiplexer.rb', line 54

def errors
  @errors ||= Hash.new { |h, k| h[k] = {} }
end

#validate!Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/i18n_flow/validator/multiplexer.rb', line 24

def validate!
  @errors = nil

  if linters.include?(:file_scope)
    repository.asts_by_path.each do |path, tree|
      validator = FileScope.new(tree, filepath: path)
      validator.validate!
      validator.errors.each do |err|
        errors[err.file][err.key] = err
      end
    end
  end

  if linters.include?(:symmetry)
    repository.asts_by_scope.each do |scope, locale_trees|
      locale_pairs.each do |(master, slave)|
        master_tree = locale_trees[master]
        slave_tree = locale_trees[slave]
        next unless master_tree && slave_tree

        validator = Symmetry.new(master_tree[master], slave_tree[slave])
        validator.validate!
        validator.errors.each do |err|
          errors[err.file][err.key] = err
        end
      end
    end
  end
end