summaryrefslogtreecommitdiff
path: root/lib/ostruct.rb
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-12-02 15:17:35 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-12-02 15:17:35 +0000
commit5847fadf7b173479fca0521b2301e6ff89cbd09a (patch)
tree0a9c7773f47815647879bd627c24d38716d1d20e /lib/ostruct.rb
parentdd1510eddcfda423b8871271f36590c458b1cc39 (diff)
* io.c (rb_file_initialize): [ruby-dev:25032]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7440 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/ostruct.rb')
-rw-r--r--lib/ostruct.rb29
1 files changed, 23 insertions, 6 deletions
diff --git a/lib/ostruct.rb b/lib/ostruct.rb
index 4d9bb33..7f46682 100644
--- a/lib/ostruct.rb
+++ b/lib/ostruct.rb
@@ -47,10 +47,8 @@ class OpenStruct
@table = {}
if hash
for k,v in hash
- if $DEBUG and self.respond_to?(k, true)
- raise NameError, "already existing member #{k}", caller(2)
- end
@table[k.to_sym] = v
+ new_ostruct_member(k)
end
end
end
@@ -61,6 +59,27 @@ class OpenStruct
@table = @table.dup
end
+ module Marshaler
+ def marshal_dump
+ table = @table
+ OpenStruct.new.instance_eval{@table=table; self}
+ end
+ def marshal_load(x)
+ @table = x.instance_variable_get("@table")
+ @table.each_key{|key| new_ostruct_member(key)}
+ end
+ end
+
+ def new_ostruct_member(name)
+ unless self.respond_to?(name)
+ self.instance_eval %{
+ extend OpenStruct::Marshaler
+ def #{name}; @table[:#{name}]; end
+ def #{name}=(x); @table[:#{name}] = x; end
+ }
+ end
+ end
+
def method_missing(mid, *args) # :nodoc:
mname = mid.id2name
len = args.length
@@ -72,10 +91,8 @@ class OpenStruct
raise TypeError, "can't modify frozen #{self.class}", caller(1)
end
mname.chop!
- if $DEBUG and self.respond_to?(mname, true)
- raise NameError, "already existing member #{mname}", caller(1)
- end
@table[mname.intern] = args[0]
+ self.new_ostruct_member(mname)
elsif len == 0
@table[mid]
else