Module: I18n::Tasks::KeyPatternMatching

Extended by:
KeyPatternMatching
Included in:
BaseTask, Command::Commands::Tree, Data::Router::PatternRouter, KeyPatternMatching
Defined in:
lib/i18n/tasks/key_pattern_matching.rb

Constant Summary collapse

MATCH_NOTHING =
/\z\A/.freeze

Instance Method Summary collapse

Instance Method Details

#compile_key_pattern(key_pattern) ⇒ Object

convert pattern to regex In patterns:

   *     is like .* in regexs
   :     matches a single key
   *:    matches part of a single key, equivalent to `[^.]+?` regex
{ a, b.c } match any in set, can use : and *, match is captured


26
27
28
29
30
# File 'lib/i18n/tasks/key_pattern_matching.rb', line 26

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/tasks/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



32
33
34
35
36
37
38
39
# File 'lib/i18n/tasks/key_pattern_matching.rb', line 32

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