Created
April 15, 2014 08:54
-
-
Save huydx/10715220 to your computer and use it in GitHub Desktop.
deep_struct.rb
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
# deep_struct.rb | |
require 'ostruct' | |
class DeepStruct < OpenStruct | |
def initialize(hash=nil) | |
@table = {} | |
@hash_table = {} | |
if hash | |
hash.each do |k,v| | |
@table[k.to_sym] = (v.is_a?(Hash) ? self.class.new(v) : v) | |
@hash_table[k.to_sym] = v | |
new_ostruct_member(k) | |
end | |
end | |
end | |
def to_h | |
@hash_table | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment