Class: Cocoafish::CocoafishObject

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

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ CocoafishObject

Returns a new instance of CocoafishObject.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/cocoafish/cocoafish_object.rb', line 4

def initialize(hash)
  hash.each do |k,v|
    if k == "ratings_summary"
      # ratings_summary is a hash with keys as integers
      next
    end
    v = CocoafishObject.new(v) if v.is_a?(Hash)
    v = v.map {|arrayvalue| arrayvalue.is_a?(Hash) ? CocoafishObject.new(arrayvalue) : arrayvalue} if v.is_a?(Array)
    begin
      self.instance_variable_set("@#{k}", v) ## create and initialize an instance variable for this key/value pair
      self.class.send(:define_method, k, proc{self.instance_variable_get("@#{k}")}) ## create the getter that returns the instance variable
      self.class.send(:define_method, "#{k}=", proc{|v| self.instance_variable_set("@#{k}", v)}) ## create the setter that sets the instance variable
    rescue
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key) ⇒ Object



21
22
23
# File 'lib/cocoafish/cocoafish_object.rb', line 21

def method_missing key
  nil
end