lib/lite/ruby/open_struct.rb in lite-ruby-1.1.9 vs lib/lite/ruby/open_struct.rb in lite-ruby-1.1.10

- old
+ new

@@ -18,28 +18,16 @@ end yield self if block && block.arity == 1 end - def [](key) - key = key.to_sym unless key.is_a?(Symbol) - @table[key] - end - - def []=(key, val) - raise TypeError, "can't modify frozen #{self.class}", caller(1) if frozen? - - key = key.to_sym unless key.is_a?(Symbol) - @table[key] = val - end - def attributes @table end def replace(args) - args.each_pair { |key, val| send("#{key}=", val) } + args.each { |key, val| send("#{key}=", val) } end def to_hash(table: true) return attributes unless table @@ -50,8 +38,17 @@ to_hash(table: table).to_json end alias as_json to_json alias to_h to_hash + + private + + def new_ostruct_member!(name) + return if is_method_protected!(name) + + define_singleton_method!(name) { @table[name] } + define_singleton_method!("#{name}=") { |x| @table[name] = x } + end end end