Module: I18n::Tasks::SplitKey
- Included in:
- BaseTask, Data::Tree::Siblings, Data::Tree::Siblings
- Defined in:
- lib/i18n/tasks/split_key.rb
Class Method Summary collapse
-
.key_parts(key, &block) ⇒ Object
yield each key part dots inside braces or parenthesis are not split on.
- .last_key_part(key) ⇒ Object
-
.split_key(key, max = Float::INFINITY) ⇒ Object
split a key by dots (.) dots inside braces or parenthesis are not split on.
Class Method Details
.key_parts(key, &block) ⇒ Object
yield each key part dots inside braces or parenthesis are not split on
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# 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) ⇒ Object
30 31 32 33 34 |
# 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) ⇒ Object
split a key by dots (.) dots inside braces or parenthesis are not split on
split_key ‘a.b’ # => [‘a’, ‘b’] split_key ‘a.#bb.c’ # => [‘a’, ‘#bb.c’] split_key ‘a.b.c’, 2 # => [‘a’, ‘b.c’]
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# 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..] unless pos == key.length break end end parts end |