mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
[ruby/ostruct] Revert "ostruct.rb: deferred accessors"
This reverts commits: dc38e99813 22c082fcfd b499e0f9ff 58e5876646 Add test for overriden private methods [Fixes https://bugs.ruby-lang.org/issues/12136]
This commit is contained in:
parent
1cabb216c6
commit
e026e186f4
2 changed files with 11 additions and 17 deletions
|
@ -95,6 +95,7 @@ class OpenStruct
|
|||
hash.each_pair do |k, v|
|
||||
k = k.to_sym
|
||||
@table[k] = v
|
||||
new_ostruct_member!(k)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -103,6 +104,7 @@ class OpenStruct
|
|||
def initialize_copy(orig) # :nodoc:
|
||||
super
|
||||
@table = @table.dup
|
||||
@table.each_key{|key| new_ostruct_member!(key)}
|
||||
end
|
||||
|
||||
#
|
||||
|
@ -160,6 +162,7 @@ class OpenStruct
|
|||
#
|
||||
def marshal_load(x)
|
||||
@table = x
|
||||
@table.each_key{|key| new_ostruct_member!(key)}
|
||||
end
|
||||
|
||||
#
|
||||
|
@ -183,7 +186,7 @@ class OpenStruct
|
|||
#
|
||||
def new_ostruct_member!(name) # :nodoc:
|
||||
name = name.to_sym
|
||||
unless singleton_class.method_defined?(name)
|
||||
unless respond_to?(name)
|
||||
define_singleton_method(name) { @table[name] }
|
||||
define_singleton_method("#{name}=") {|x| modifiable?[name] = x}
|
||||
end
|
||||
|
@ -191,16 +194,6 @@ class OpenStruct
|
|||
end
|
||||
private :new_ostruct_member!
|
||||
|
||||
def freeze
|
||||
@table.each_key {|key| new_ostruct_member!(key)}
|
||||
super
|
||||
end
|
||||
|
||||
def respond_to_missing?(mid, include_private = false) # :nodoc:
|
||||
mname = mid.to_s.chomp("=").to_sym
|
||||
defined?(@table) && @table.key?(mname) || super
|
||||
end
|
||||
|
||||
def method_missing(mid, *args) # :nodoc:
|
||||
len = args.length
|
||||
if mname = mid[/.*(?==\z)/m]
|
||||
|
@ -208,11 +201,7 @@ class OpenStruct
|
|||
raise ArgumentError, "wrong number of arguments (given #{len}, expected 1)", caller(1)
|
||||
end
|
||||
modifiable?[new_ostruct_member!(mname)] = args[0]
|
||||
elsif len == 0 # and /\A[a-z_]\w*\z/ =~ mid #
|
||||
if @table.key?(mid)
|
||||
new_ostruct_member!(mid) unless frozen?
|
||||
@table[mid]
|
||||
end
|
||||
elsif len == 0
|
||||
elsif @table.key?(mid)
|
||||
raise ArgumentError, "wrong number of arguments (given #{len}, expected 0)"
|
||||
else
|
||||
|
|
|
@ -179,7 +179,6 @@ class TC_OpenStruct < Test::Unit::TestCase
|
|||
def test_accessor_defines_method
|
||||
os = OpenStruct.new(foo: 42)
|
||||
assert_respond_to(os, :foo)
|
||||
assert_equal([], os.singleton_methods)
|
||||
assert_equal(42, os.foo)
|
||||
assert_equal([:foo, :foo=], os.singleton_methods.sort)
|
||||
end
|
||||
|
@ -225,4 +224,10 @@ class TC_OpenStruct < Test::Unit::TestCase
|
|||
os.foo true, true
|
||||
end
|
||||
end
|
||||
|
||||
def test_overriden_private_methods
|
||||
os = OpenStruct.new(puts: :foo, format: :bar)
|
||||
assert_equal(:foo, os.puts)
|
||||
assert_equal(:bar, os.format)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue