Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/models/hash.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object



23
24
25
26
27
# File 'lib/models/hash.rb', line 23

def method_missing(name)
  return self[name] if key? name
  self.each { |k,v| return v if k.to_s.to_sym == name }
  super.method_missing name
end

Instance Method Details

#to_objObject

add keys to hash



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

def to_obj
  self.each do |k,v|
    if v.kind_of? Hash
      v.to_obj
    end
    k=k.gsub(/\.|\s|-|\/|\'/, '_').downcase.to_sym

    ## create and initialize an instance variable for this key/value pair
    self.instance_variable_set("@#{k}", v)

    ## create the getter that returns the instance variable
    self.class.send(:define_method, k, proc{self.instance_variable_get("@#{k}")})

    ## create the setter that sets the instance variable
    self.class.send(:define_method, "#{k}=", proc{|v| self.instance_variable_set("@#{k}", v)})
  end
  return self
end