Skip to content

Instantly share code, notes, and snippets.

@kmcd
Created March 23, 2010 11:19
Show Gist options
  • Save kmcd/341070 to your computer and use it in GitHub Desktop.
Save kmcd/341070 to your computer and use it in GitHub Desktop.
class NestedOpenStruct < OpenStruct
def initialize(hash=nil)
@table = {}
if hash
for k,v in hash
# handle nested hashes
if v.is_a? Hash
@table[k.to_sym] = NestedOpenStruct.new(v)
# handle nested hashes nested in arrays
elsif v.is_a? Array
if v.all? {|entry| entry.is_a? Hash }
@table[k.to_sym] = v.map {|v| NestedOpenStruct.new(v) }
end
else
@table[k.to_sym] = v
new_ostruct_member(k)
end
end
end
end
end
class Hash
def method_missing(key)
self[key.to_sym] || self[key.to_s] || super
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment