Class: Dothash::Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/dothash/hash.rb

Class Method Summary collapse

Class Method Details

.with_dots(hash, prefix = nil, one_based = false) ⇒ Object

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
# File 'lib/dothash/hash.rb', line 22

def self.with_dots(hash, prefix = nil, one_based = false)
  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, one_based)
  end
end

.with_dots_one_based(hash, prefix = nil) ⇒ Object



30
31
32
# File 'lib/dothash/hash.rb', line 30

def self.with_dots_one_based(hash, prefix = nil)
  with_dots hash, prefix, true
end

.without_dots(hash) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dothash/hash.rb', line 47

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