Sha256: 35fb70a5f43ff3a33558212e07812114908120c3a3def4f05b07a0a345d6c335
Contents?: true
Size: 1.38 KB
Versions: 1
Compression:
Stored size: 1.38 KB
Contents
class MoStash < OpenStruct def initialize(init={}) super({}) __init__ init end def method_missing(method_name, *args) #dbg "#{method_name} was sent #{args.inspect}" if __is_setter__( method_name ) super method_name, __adjusted_value__( args.first ) else super end end def []=(key, value) self.send "#{key.to_s}=", value end def [](key) self.send "#{key.to_s}" end def merge(new_hash) new_mo = @table.merge( new_hash ) do |key, oldval, newval| if oldval.class == MoStash oldval.merge newval else newval end end MoStash.new( new_mo ) end #TODO: HACK!!!!! def merge!(new_hash) @table = self.merge( new_hash ).instance_variable_get( '@table' ) self end def to_hash hash = {} @table.each_pair do |key, value| hash[key] = if value.class == MoStash value.to_hash else value end end hash end private def __init__(hash) hash.each_pair do |key, value| self.send "#{key.to_s}=", __adjusted_value__( value ) end end def __adjusted_value__(value) case value when Hash then MoStash.new( value ) when Array then value.map{ |v| __adjusted_value__( v ) } else value end end def __is_setter__(method_name) method_name.to_s =~ /=$/ end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
mostash-0.0.2 | lib/mostash/mostash.rb |