Sha256: 218111ea6009369c7adb148a61a5e4316b6d3cff862988d8df4bbdf8b15b0c13

Contents?: true

Size: 1.39 KB

Versions: 12

Compression:

Stored size: 1.39 KB

Contents

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)
    if key_pattern.end_with? '.'
      I18n::Tasks.warn_deprecated %Q(please change pattern "#{key_pattern}" to "#{key_pattern += '*'}" in config/i18n-tasks.yml)
    end
    /^#{key_pattern.
        gsub(/\./, '\.').
        gsub(/\*/, '.*').
        gsub(/:/, '(?<=^|\.)[^.]+?(?=\.|$)').
        gsub(/\{(.*?)}/) { "(#{$1.strip.gsub /\s*,\s*/, '|'})" }
    }$/
  end

  # @return [Array<String>] keys sans passed patterns
  def exclude_patterns(keys, patterns)
    pattern_re = compile_patterns_re patterns.select { |p| p.end_with?('.') }
    (keys - patterns).reject { |k| k =~ pattern_re }
  end

  # compile prefix matching Regexp from the list of prefixes
  # @return [Regexp] regexp matching any of the prefixes
  def compile_start_with_re(prefixes)
    if prefixes.blank?
      MATCH_NOTHING # match nothing
    else
      /^(?:#{prefixes.map { |p| Regexp.escape(p) }.join('|')})/
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
i18n-tasks-0.2.15 lib/i18n/tasks/key_pattern_matching.rb
i18n-tasks-0.2.14 lib/i18n/tasks/key_pattern_matching.rb
i18n-tasks-0.2.13 lib/i18n/tasks/key_pattern_matching.rb
i18n-tasks-0.2.12 lib/i18n/tasks/key_pattern_matching.rb
i18n-tasks-0.2.11 lib/i18n/tasks/key_pattern_matching.rb
i18n-tasks-0.2.10 lib/i18n/tasks/key_pattern_matching.rb
i18n-tasks-0.2.9 lib/i18n/tasks/key_pattern_matching.rb
i18n-tasks-0.2.8 lib/i18n/tasks/key_pattern_matching.rb
i18n-tasks-0.2.7 lib/i18n/tasks/key_pattern_matching.rb
i18n-tasks-0.2.6 lib/i18n/tasks/key_pattern_matching.rb
i18n-tasks-0.2.5 lib/i18n/tasks/key_pattern_matching.rb
i18n-tasks-0.2.4 lib/i18n/tasks/key_pattern_matching.rb