Class: Cocoafish::ObjectifiedHash

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoafish/objectified_hash.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ ObjectifiedHash

Returns a new instance of ObjectifiedHash.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/cocoafish/objectified_hash.rb', line 5

def initialize hash
    @data = hash.inject({}) do |data, (key,value)|
      if value.kind_of? Hash  
        value = ObjectifiedHash.new value
      elsif value.kind_of? Array
        value = value.map {|arrayvalue| ObjectifiedHash.new arrayvalue}
      end
      data[key.to_s] = value
      data
    end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/cocoafish/objectified_hash.rb', line 17

def method_missing key
    if @data.key? key.to_s
        @data[key.to_s]
    else
        nil
    end
end