Sha256: 3e7854e76ec36519d5e0586f5c9d55b3f39484f70be2db251063da8155226f0a

Contents?: true

Size: 871 Bytes

Versions: 3

Compression:

Stored size: 871 Bytes

Contents

# coding: utf-8
module I18n::Tasks::KeyPatternMatching
  MATCH_NOTHING = /\z\A/

  # one regex to match any
  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

  # 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
  def compile_key_pattern(key_pattern)
    return key_pattern if key_pattern.is_a?(Regexp)
    /\A#{key_pattern_re_body(key_pattern)}\z/
  end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
i18n-tasks-0.4.5 lib/i18n/tasks/key_pattern_matching.rb
i18n-tasks-0.4.4 lib/i18n/tasks/key_pattern_matching.rb
i18n-tasks-0.4.3 lib/i18n/tasks/key_pattern_matching.rb