Class: Hash

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

Instance Method Summary collapse

Instance Method Details

#to_ostruct(klass = OpenStruct, cch = {}) ⇒ Object



2
3
4
5
6
7
8
9
# File 'lib/core_extensions/hash.rb', line 2

def to_ostruct(klass = OpenStruct, cch = {})
  cch[self] = klass.new
  each do |key,value|
    raise "Invalid key: #{ key }" unless key =~ /[a-z_][a-zA-Z0-9_]*/
    cch[self].__send__ "#{ key }=", value.is_a?(Hash) ? cch[value] || value.send(__method__,klass,cch) : value
  end
  cch[self]
end