module I18n::Tasks::SplitKey

Constants

PARENS
PARENS_ZEROS

Public Instance Methods

key_parts(key, &block) click to toggle source

yield each key part dots inside braces or parenthesis are not split on

# File lib/i18n/tasks/split_key.rb, line 38
def key_parts(key, &block)
  return enum_for(:key_parts, key) unless block

  nesting = PARENS
  counts  = PARENS_ZEROS # dup'd later if key contains parenthesis
  delim   = '.'
  from    = to = 0
  key.each_char do |char|
    if char == delim && PARENS_ZEROS == counts
      block.yield key[from...to]
      from = to = (to + 1)
    else
      nest_i, nest_inc = nesting[char]
      if nest_i
        counts = counts.dup if counts.frozen?
        counts[nest_i] += nest_inc
      end
      to += 1
    end
  end
  block.yield(key[from...to]) if from < to && to <= key.length
  true
end
last_key_part(key) click to toggle source
# File lib/i18n/tasks/split_key.rb, line 30
def last_key_part(key)
  last = nil
  key_parts(key) { |part| last = part }
  last
end
split_key(key, max = Float::INFINITY) click to toggle source

split a key by dots (.) dots inside braces or parenthesis are not split on

split_key 'a.b' # => ['a', 'b'] split_key 'a.#{b.c}' # => ['a', '#{b.c}'] split_key 'a.b.c', 2 # => ['a', 'b.c']

# File lib/i18n/tasks/split_key.rb, line 14
def split_key(key, max = Float::INFINITY)
  parts = []
  pos   = 0
  return [key] if max == 1

  key_parts(key) do |part|
    parts << part
    pos += part.length + 1
    if parts.length + 1 >= max
      parts << key[pos..-1] unless pos == key.length
      break
    end
  end
  parts
end