Class: Hash

Inherits:
Object show all
Defined in:
lib/configliere/core_ext/hash.rb,
lib/configliere/core_ext/sash.rb

Overview

core_ext/hash.rb – hash extensions

Direct Known Subclasses

Configliere::ParamParent, Sash

Constant Summary collapse

DEEP_MERGER =

lambda for recursive merges

proc do |key,v1,v2|
  (v1.respond_to?(:merge) && v2.respond_to?(:merge)) ? v1.merge(v2.compact, &Hash::DEEP_MERGER) : (v2.nil? ? v1 : v2)
end

Instance Method Summary collapse

Instance Method Details

#compactObject

remove all key-value pairs where the value is nil



100
101
102
# File 'lib/configliere/core_ext/hash.rb', line 100

def compact
  reject{|key,val| val.nil? }
end

#compact!Object

Replace the hash with its compacted self



106
107
108
# File 'lib/configliere/core_ext/hash.rb', line 106

def compact!
  replace(compact)
end

#deep_delete(*args) ⇒ Object

Treat hash as tree of hashes:

x = { 1 => :val, :subhash => { 1 => :val1, 2 => :val2 } }
x.deep_delete(:subhash, 1)
#=> :val
x
#=> { 1 => :val, :subhash => { 2 => :val2 } }


91
92
93
94
95
# File 'lib/configliere/core_ext/hash.rb', line 91

def deep_delete *args
  last_key  = args.pop
  last_hsh  = args.empty? ? self : (deep_get(*args)||{})
  last_hsh.delete(last_key)
end

#deep_get(*args) ⇒ Object

Treat hash as tree of hashes:

x = { 1 => :val, :subhash => { 1 => :val1 } }
x.deep_get(:subhash, 1)
# => :val
x.deep_get(:subhash, 2)
# => nil
x.deep_get(:subhash, 2, 3)
# => nil
x.deep_get(:subhash, 2)
# => nil


73
74
75
76
77
78
79
# File 'lib/configliere/core_ext/hash.rb', line 73

def deep_get *args
  last_key = args.pop
  # dig down to last subtree (building out if necessary)
  hsh = args.inject(self){|hsh, key| hsh[key] || {} }
  # get leaf value
  hsh[last_key]
end

#deep_merge(hsh2) ⇒ Object

Merge hashes recursively. Nothing special happens to array values

x = { :subhash => { 1 => :val_from_x, 222 => :only_in_x, 333 => :only_in_x }, :scalar => :scalar_from_x}
y = { :subhash => { 1 => :val_from_y, 999 => :only_in_y },                    :scalar => :scalar_from_y }
x.deep_merge y
=> {:subhash=>{1=>:val_from_y, 222=>:only_in_x, 333=>:only_in_x, 999=>:only_in_y}, :scalar=>:scalar_from_y}
y.deep_merge x
=> {:subhash=>{1=>:val_from_x, 222=>:only_in_x, 333=>:only_in_x, 999=>:only_in_y}, :scalar=>:scalar_from_x}

Nil values always lose.

x = {:subhash=>{:nil_in_x=>nil, 1=>:val1,}, :nil_in_x=>nil}
y = {:subhash=>{:nil_in_x=>5},              :nil_in_x=>5}
y.deep_merge x
=> {:subhash=>{1=>:val1, :nil_in_x=>5}, :nil_in_x=>5}
x.deep_merge y
=> {:subhash=>{1=>:val1, :nil_in_x=>5}, :nil_in_x=>5}


31
32
33
# File 'lib/configliere/core_ext/hash.rb', line 31

def deep_merge hsh2
  merge hsh2, &Hash::DEEP_MERGER
end

#deep_merge!(hsh2) ⇒ Object



35
36
37
38
# File 'lib/configliere/core_ext/hash.rb', line 35

def deep_merge! hsh2
  update hsh2, &Hash::DEEP_MERGER
  self
end

#deep_set(*args) ⇒ Object

Treat hash as tree of hashes:

x = { 1 => :val, :subhash => { 1 => :val1 } }
x.deep_set(:subhash, :cat, :hat)
# => { 1 => :val, :subhash => { 1 => :val1,   :cat => :hat } }
x.deep_set(:subhash, 1, :newval)
# => { 1 => :val, :subhash => { 1 => :newval, :cat => :hat } }


50
51
52
53
54
55
56
57
58
# File 'lib/configliere/core_ext/hash.rb', line 50

def deep_set *args
  val      = args.pop
  last_key = args.pop
  # dig down to last subtree (building out if necessary)
  hsh = self
  args.each{|key| hsh = (hsh[key] ||= self.class.new) }
  # set leaf value
  hsh[last_key] = val
end

#to_sashMash

Convert to Sash. This class has semantics of ActiveSupport’s HashWithIndifferentAccess and we only have it so that people can write params instead of params.

Returns:

  • (Mash)

    This hash as a Mash for string or symbol key access.



163
164
165
166
167
# File 'lib/configliere/core_ext/sash.rb', line 163

def to_sash
  hash = Sash.new(self)
  hash.default = default
  hash
end