Module: I18n::Backend::Helpers
- Included in:
- Base
- Defined in:
- lib/active_support/vendor/i18n-0.3.7/i18n/backend/helpers.rb
Constant Summary collapse
- SEPARATOR_ESCAPE_CHAR =
"\001"
Instance Method Summary collapse
-
#deep_symbolize_keys(hash) ⇒ Object
Return a new hash with all keys and nested keys converted to symbols.
- #escape_default_separator(key, separator = nil) ⇒ Object
- #unescape_default_separator(key, separator = nil) ⇒ Object
-
#unwind_keys(hash, separator = ".") ⇒ Object
Expand keys chained by the the given separator through nested Hashes >> { “a.b.c” => “d”, “a.b.e” => “f”, “a.g” => “h”, “i” => “j” }.unwind => { “a” => { “b” => { “c” => “d”, “e” => “f” }, “g” => “h” }, “i” => “j”}.
-
#wind_keys(hash, separator = nil, subtree = false, prev_key = nil, result = {}, orig_hash = hash) ⇒ Object
Flatten keys for nested Hashes by chaining up keys using the separator >> { “a” => { “b” => { “c” => “d”, “e” => “f” }, “g” => “h” }, “i” => “j”}.wind => { “a.b.c” => “d”, “a.b.e” => “f”, “a.g” => “h”, “i” => “j” }.
Instance Method Details
#deep_symbolize_keys(hash) ⇒ Object
Return a new hash with all keys and nested keys converted to symbols.
7 8 9 10 11 12 13 |
# File 'lib/active_support/vendor/i18n-0.3.7/i18n/backend/helpers.rb', line 7 def deep_symbolize_keys(hash) hash.inject({}) { |result, (key, value)| value = deep_symbolize_keys(value) if value.is_a?(Hash) result[(key.to_sym rescue key) || key] = value result } end |
#escape_default_separator(key, separator = nil) ⇒ Object
36 37 38 |
# File 'lib/active_support/vendor/i18n-0.3.7/i18n/backend/helpers.rb', line 36 def escape_default_separator(key, separator=nil) key.to_s.tr(separator || I18n.default_separator, SEPARATOR_ESCAPE_CHAR) end |
#unescape_default_separator(key, separator = nil) ⇒ Object
40 41 42 |
# File 'lib/active_support/vendor/i18n-0.3.7/i18n/backend/helpers.rb', line 40 def unescape_default_separator(key, separator=nil) key.to_s.tr(SEPARATOR_ESCAPE_CHAR, separator || I18n.default_separator).to_sym end |
#unwind_keys(hash, separator = ".") ⇒ Object
Expand keys chained by the the given separator through nested Hashes
>> { "a.b.c" => "d", "a.b.e" => "f", "a.g" => "h", "i" => "j" }.unwind
=> { "a" => { "b" => { "c" => "d", "e" => "f" }, "g" => "h" }, "i" => "j"}
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/active_support/vendor/i18n-0.3.7/i18n/backend/helpers.rb', line 47 def unwind_keys(hash, separator = ".") result = {} hash.each do |key, value| keys = key.to_s.split(separator) curr = result curr = curr[keys.shift] ||= {} while keys.size > 1 curr[keys.shift] = value end result end |
#wind_keys(hash, separator = nil, subtree = false, prev_key = nil, result = {}, orig_hash = hash) ⇒ Object
Flatten keys for nested Hashes by chaining up keys using the separator
>> { "a" => { "b" => { "c" => "d", "e" => "f" }, "g" => "h" }, "i" => "j"}.wind
=> { "a.b.c" => "d", "a.b.e" => "f", "a.g" => "h", "i" => "j" }
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/active_support/vendor/i18n-0.3.7/i18n/backend/helpers.rb', line 18 def wind_keys(hash, separator = nil, subtree = false, prev_key = nil, result = {}, orig_hash=hash) separator ||= I18n.default_separator hash.each_pair do |key, value| key = escape_default_separator(key, separator) curr_key = [prev_key, key].compact.join(separator).to_sym if value.is_a?(Hash) result[curr_key] = value if subtree wind_keys(value, separator, subtree, curr_key, result, orig_hash) else result[unescape_default_separator(curr_key)] = value end end result end |