Class: TFG::Support::DeepHashAccessor
- Defined in:
- lib/tfg/support/deep_hash_accessor.rb
Instance Method Summary collapse
- #[](*keys) ⇒ Object
- #[]=(*keys, value) ⇒ Object
-
#initialize(hash) ⇒ DeepHashAccessor
constructor
A new instance of DeepHashAccessor.
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 |