Module: I18n::Processes::KeyPatternMatching

Included in:
BaseProcess, Command::Commands::Tree, Data::Router::PatternRouter
Defined in:
lib/i18n/processes/key_pattern_matching.rb

Constant Summary collapse

MATCH_NOTHING =
/\z\A/

Class Method Summary collapse

Class Method Details

.compile_key_pattern(key_pattern) ⇒ Object

convert pattern to regex In patterns:

   *     is like .* in regexs
   :     matches a single key
{ a, b.c } match any in set, can use : and *, match is captured


25
26
27
28
# File 'lib/i18n/processes/key_pattern_matching.rb', line 25

def compile_key_pattern(key_pattern)
  return key_pattern if key_pattern.is_a?(Regexp)
  /\A#{key_pattern_re_body(key_pattern)}\z/
end

.compile_patterns_re(key_patterns) ⇒ Object

one regex to match any



11
12
13
14
15
16
17
18
# File 'lib/i18n/processes/key_pattern_matching.rb', line 11

def compile_patterns_re(key_patterns)
  if key_patterns.blank?
    # match nothing
    MATCH_NOTHING
  else
    /(?:#{ key_patterns.map { |p| compile_key_pattern p } * '|' })/m
  end
end

.key_pattern_re_body(key_pattern) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/i18n/processes/key_pattern_matching.rb', line 30

def key_pattern_re_body(key_pattern)
  key_pattern
    .gsub(/\./, '\.')
    .gsub(/\*/, '.*')
    .gsub(/:/, '(?<=^|\.)[^.]+?(?=\.|$)')
    .gsub(/\{(.*?)}/) { "(#{Regexp.last_match(1).strip.gsub(/\s*,\s*/, '|')})" }
end