summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-02-15 12:43:46 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-02-15 12:43:46 +0000
commit8d7828848b4b2f6f09d31c3e61720b9fb9e36627 (patch)
tree0eadca828f51e8d4d81bc3cc6c3fb700525b41b2
parentbc2c0258733446415107b1ba973d4a6a3fbffb3f (diff)
* lib/ostruct.rb (OpenStruct#new_ostruct_member): checks if frozen.
[ruby-talk:328195], [ruby-core:22142] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@22332 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--lib/ostruct.rb25
-rw-r--r--test/ostruct/test_ostruct.rb23
3 files changed, 43 insertions, 10 deletions
diff --git a/ChangeLog b/ChangeLog
index e98686123c..640f82dcb2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Sun Feb 15 21:43:44 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * lib/ostruct.rb (OpenStruct#new_ostruct_member): checks if frozen.
+ [ruby-talk:328195], [ruby-core:22142]
+
Sun Feb 15 21:07:05 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
* lib/ostruct.rb (OpenStruct#inspect): fixed the recursion check.
diff --git a/lib/ostruct.rb b/lib/ostruct.rb
index ba7f061a22..45ebb8083e 100644
--- a/lib/ostruct.rb
+++ b/lib/ostruct.rb
@@ -67,28 +67,33 @@ class OpenStruct
@table.each_key{|key| new_ostruct_member(key)}
end
+ def modifiable
+ if self.frozen?
+ raise TypeError, "can't modify frozen #{self.class}", caller(2)
+ end
+ @table
+ end
+ protected :modifiable
+
def new_ostruct_member(name)
name = name.to_sym
unless self.respond_to?(name)
- meta = class << self; self; end
- meta.send(:define_method, name) { @table[name] }
- meta.send(:define_method, :"#{name}=") { |x| @table[name] = x }
+ class << self; self; end.class_eval do
+ define_method(name) { @table[name] }
+ define_method("#{name}=") { |x| modifiable[name] = x }
+ end
end
+ name
end
def method_missing(mid, *args) # :nodoc:
mname = mid.id2name
len = args.length
- if mname =~ /=$/
+ if mname.chomp!('=')
if len != 1
raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
end
- if self.frozen?
- raise TypeError, "can't modify frozen #{self.class}", caller(1)
- end
- mname.chop!
- self.new_ostruct_member(mname)
- @table[mname.intern] = args[0]
+ modifiable[new_ostruct_member(mname)] = args[0]
elsif len == 0
@table[mid]
else
diff --git a/test/ostruct/test_ostruct.rb b/test/ostruct/test_ostruct.rb
index 79662c6437..4d56102462 100644
--- a/test/ostruct/test_ostruct.rb
+++ b/test/ostruct/test_ostruct.rb
@@ -2,6 +2,21 @@ require 'test/unit'
require 'ostruct'
class TC_OpenStruct < Test::Unit::TestCase
+ def assert_not_respond_to(object, method, message="")
+ _wrap_assertion do
+ full_message = build_message(message, <<EOT, object, object.class, method)
+<?>
+of type <?>
+expected not to respond_to\\?<?>.
+EOT
+ _wrap_assertion do
+ if object.respond_to?(method)
+ raise Test::Unit::AssertionFailedError, full_message, caller(5)
+ end
+ end
+ end
+ end
+
def test_equality
o1 = OpenStruct.new
o2 = OpenStruct.new
@@ -34,4 +49,12 @@ class TC_OpenStruct < Test::Unit::TestCase
foo.bar.foo = foo
assert_equal('#<OpenStruct bar=#<OpenStruct foo=#<OpenStruct ...>>>', foo.inspect)
end
+
+ def test_frozen
+ o = OpenStruct.new
+ o.a = 'a'
+ o.freeze
+ assert_raise(TypeError) {o.b = 'b'}
+ assert_not_respond_to(o, :b)
+ end
end