Sha256: 2ee69b7c5db77c2aaa5e0e57a0da2fc246d37678c3acd9ddf8a71273a74023bc

Contents?: true

Size: 797 Bytes

Versions: 4

Compression:

Stored size: 797 Bytes

Contents

class ::Hash

  # add keys to hash
  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
  
  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
  
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
koda-0.0.12 lib/models/hash.rb
koda-0.0.11 lib/models/hash.rb
koda-0.0.9 lib/models/hash.rb
koda-0.0.8 lib/models/hash.rb