Created
March 23, 2010 11:19
-
-
Save kmcd/341070 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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