Class: Dothash::Hash
- Inherits:
-
Object
- Object
- Dothash::Hash
- Defined in:
- lib/dothash/hash.rb
Class Method Summary collapse
Class Method Details
.with_dots(hash, prefix = nil) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/dothash/hash.rb', line 22 def self.with_dots(hash, prefix = nil) raise ArgumentError, "You should pass only Hash here" unless hash.is_a? ::Hash hash.each_with_object({}) do |(key, value), memo| new_key = [prefix, key].compact.join(".") memo.merge! with_dots_deeper(value, new_key) end end |
.without_dots(hash) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/dothash/hash.rb', line 42 def self.without_dots(hash) hash.each_with_object({}) do |(key, value), memo| key_parts = key.to_s.split(".") new_key = key_parts.shift.to_sym if key_parts.empty? memo[new_key] = value else deep_merge!(memo, new_key => without_dots(key_parts.join(".") => value)) end end end |