Class: Hash

Inherits:
Object show all
Defined in:
lib/quickbooks/support/monkey_patches.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.nest(path, value) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/quickbooks/support/monkey_patches.rb', line 16

def self.nest(path, value)
  hash_constructor = Proc.new { |h, k| h[k] = Hash.new(&hash_constructor) }
  nested_hash = Hash.new(&hash_constructor)

  last_key = path.last
  path.inject(nested_hash) { |h, k| (k == last_key) ? h[k] = value : h[k] }
  nested_hash
end

Instance Method Details

#path_to_nested_key(key) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/quickbooks/support/monkey_patches.rb', line 3

def path_to_nested_key(key)
  each do |k,v|
    path = [k]
    if k == key
      return path
    elsif v.is_a? Hash
      nested_path = v.path_to_nested_key(key)
      return (path + nested_path) if nested_path
    end
  end
  return nil
end