Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/core_ext/hash.rb
Constant Summary collapse
- @@namespace =
[]
Class Method Summary collapse
Instance Method Summary collapse
- #namespace_flatten(separator = ".") ⇒ Object
- #namespace_unflatten(separator = ".") ⇒ Object
- #recursive_symbolize_keys! ⇒ Object
Class Method Details
.nested_value(x, p) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/core_ext/hash.rb', line 30 def self.nested_value(x, p) hash = { p.pop => x } p.reverse_each do |element| hash = { element => hash } end hash end |
Instance Method Details
#namespace_flatten(separator = ".") ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/core_ext/hash.rb', line 6 def namespace_flatten(separator = ".") hash = {} self.each do |key, value| key = key.to_s if value.is_a?(Hash) @@namespace << key hash.merge! value.namespace_flatten(separator) @@namespace.pop else key = @@namespace + [key] hash[key.join(separator)] = value end end hash end |
#namespace_unflatten(separator = ".") ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/core_ext/hash.rb', line 22 def namespace_unflatten(separator = ".") hash = {} self.each do |key, value| hash.deep_merge! self.class.nested_value(value, key.split(separator)) end hash end |
#recursive_symbolize_keys! ⇒ Object
38 39 40 41 42 43 |
# File 'lib/core_ext/hash.rb', line 38 def recursive_symbolize_keys! symbolize_keys! values.each { |h| h.recursive_symbolize_keys! if h.is_a?(Hash) } values.select { |v| v.is_a?(Array) }.flatten.each { |h| h.recursive_symbolize_keys! if h.is_a?(Hash) } self end |