Created
January 28, 2012 06:43
-
-
Save kei-s/1693076 to your computer and use it in GitHub Desktop.
Recursive OpenStruct
This file contains 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 RecursiveOpenStruct < OpenStruct | |
def new_ostruct_member(name) | |
name = name.to_sym | |
unless self.respond_to?(name) | |
class << self; self; end.class_eval do | |
define_method(name) { | |
v = @table[name] | |
case v | |
when Hash | |
RecursiveOpenStruct.new(v) | |
when Array | |
v.map {|vv| vv.is_a?(Hash) ? RecursiveOpenStruct.new(vv) : vv } | |
else | |
v | |
end | |
} | |
define_method("#{name}=") {|x| modifiable[name] = x } | |
define_method("#{name}_as_a_hash") { @table[name] } | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment