Class: ActiveResource::InheritingHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/active_resource/inheriting_hash.rb

Instance Method Summary collapse

Constructor Details

#initialize(parent_hash = {}) ⇒ InheritingHash

Returns a new instance of InheritingHash.



5
6
7
8
9
# File 'lib/active_resource/inheriting_hash.rb', line 5

def initialize(parent_hash = {})
  # Default hash value must be nil, which allows fallback lookup on parent hash
  super(nil)
  @parent_hash = parent_hash
end

Instance Method Details

#[](key) ⇒ Object



11
12
13
# File 'lib/active_resource/inheriting_hash.rb', line 11

def [](key)
  super || @parent_hash[key]
end

#inspectObject



26
27
28
# File 'lib/active_resource/inheriting_hash.rb', line 26

def inspect
  to_hash.inspect
end

#pretty_print(pp) ⇒ Object

So we can see the merged object in IRB or the Rails console



22
23
24
# File 'lib/active_resource/inheriting_hash.rb', line 22

def pretty_print(pp)
  pp.pp_hash to_hash
end

#to_hashObject

Merges the flattened parent hash (if it’s an InheritingHash) with ourself



17
18
19
# File 'lib/active_resource/inheriting_hash.rb', line 17

def to_hash
  @parent_hash.to_hash.merge(self)
end

#to_sObject



30
31
32
# File 'lib/active_resource/inheriting_hash.rb', line 30

def to_s
  inspect
end