Class: TFG::Support::DeepHashAccessor

Inherits:
Object
  • Object
show all
Defined in:
lib/tfg/support/deep_hash_accessor.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ DeepHashAccessor

Returns a new instance of DeepHashAccessor.



4
5
6
# File 'lib/tfg/support/deep_hash_accessor.rb', line 4

def initialize(hash)
  self.hash = hash
end

Instance Method Details

#[](*keys) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/tfg/support/deep_hash_accessor.rb', line 8

def [](*keys)
  head, *tail = keys

  if tail.empty?
    hash[head]
  else
    hash[head].deep[*tail] if hash[head]
  end
end

#[]=(*keys, value) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/tfg/support/deep_hash_accessor.rb', line 18

def []=(*keys, value)
  head, *tail = keys

  if tail.empty?
    hash[head] = value
  else
    hash[head] ||= Hash.new
    hash[head].deep[*tail] = value
  end
end