Sha256: 239c71bc67b76de9dbf8581041f5573d949d78a370e15b06b8b5f18ded809296

Contents?: true

Size: 1.23 KB

Versions: 6

Compression:

Stored size: 1.23 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)
    /^#{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

6 entries across 6 versions & 1 rubygems

Version Path
i18n-tasks-0.2.22 lib/i18n/tasks/key_pattern_matching.rb
i18n-tasks-0.2.21 lib/i18n/tasks/key_pattern_matching.rb
i18n-tasks-0.2.20 lib/i18n/tasks/key_pattern_matching.rb
i18n-tasks-0.2.19 lib/i18n/tasks/key_pattern_matching.rb
i18n-tasks-0.2.18 lib/i18n/tasks/key_pattern_matching.rb
i18n-tasks-0.2.17 lib/i18n/tasks/key_pattern_matching.rb