Class: I18n::Tasks::Data::Router::IsolatingRouter

Inherits:
Object
  • Object
show all
Includes:
KeyPatternMatching
Defined in:
lib/i18n/tasks/data/router/isolating_router.rb

Overview

Route based on source file path

Defined Under Namespace

Classes: Glob

Constant Summary

Constants included from KeyPatternMatching

KeyPatternMatching::MATCH_NOTHING

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from KeyPatternMatching

#compile_key_pattern, #compile_patterns_re, #key_pattern_re_body

Constructor Details

#initialize(_adapter, data_config) ⇒ IsolatingRouter

Returns a new instance of IsolatingRouter.



14
15
16
17
# File 'lib/i18n/tasks/data/router/isolating_router.rb', line 14

def initialize(_adapter, data_config)
  @base_locale = data_config[:base_locale]
  @config_read_patterns = Array.wrap(data_config[:read])
end

Instance Attribute Details

#base_localeObject (readonly)

Returns the value of attribute base_locale.



12
13
14
# File 'lib/i18n/tasks/data/router/isolating_router.rb', line 12

def base_locale
  @base_locale
end

#config_read_patternsObject (readonly)

Returns the value of attribute config_read_patterns.



12
13
14
# File 'lib/i18n/tasks/data/router/isolating_router.rb', line 12

def config_read_patterns
  @config_read_patterns
end

Instance Method Details

#alternate_path_for(source_path, locale) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/i18n/tasks/data/router/isolating_router.rb', line 51

def alternate_path_for(source_path, locale)
  source_path = source_path.dup

  config_read_patterns.each do |pattern|
    regexp = Glob.new(format(pattern, locale: '(*)')).to_regexp
    next unless source_path.match?(regexp)

    source_path.match(regexp) do |match_data|
      (1..match_data.size - 1).reverse_each do |capture_index|
        capture_begin, capture_end = match_data.offset(capture_index)
        source_path.slice!(Range.new(capture_begin, capture_end, true))
        source_path.insert(capture_begin, locale.to_s)
      end
    end

    return source_path
  end

  nil
end

#route(locale, forest) {|dest_path, tree_slice| ... } ⇒ Hash

Route keys to destinations

Parameters:

Yield Parameters:

Returns:

  • (Hash)

    mapping of destination => [ [key, value], … ]



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
# File 'lib/i18n/tasks/data/router/isolating_router.rb', line 24

def route(locale, forest, &block)
  return to_enum(:route, locale, forest) unless block

  locale = locale.to_s
  out = {}

  forest.keys do |key_namespaced_with_source_path, _node|
    source_path, key = key_namespaced_with_source_path.match(/\A<([^>]*)>\.(.*)/).captures
    target_path = alternate_path_for(source_path, locale)
    next unless source_path && key && target_path

    (out[target_path] ||= Set.new) << "#{locale}.#{key}"
  end

  out.each do |target_path, keys|
    file_namespace_subtree = I18n::Tasks::Data::Tree::Siblings.new(
      nodes: forest.get("#{locale}.<#{alternate_path_for(target_path, base_locale)}>")
    )
    file_namespace_subtree.set_root_key!(locale)

    block.yield(
      target_path,
      file_namespace_subtree.select_keys { |key, _| keys.include?(key) }
    )
  end
end