[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
@ -95,6 +95,7 @@ class OpenStruct
|
|||||||
hash.each_pair do |k, v|
|
hash.each_pair do |k, v|
|
||||||
k = k.to_sym
|
k = k.to_sym
|
||||||
@table[k] = v
|
@table[k] = v
|
||||||
|
new_ostruct_member!(k)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -103,6 +104,7 @@ class OpenStruct
|
|||||||
def initialize_copy(orig) # :nodoc:
|
def initialize_copy(orig) # :nodoc:
|
||||||
super
|
super
|
||||||
@table = @table.dup
|
@table = @table.dup
|
||||||
|
@table.each_key{|key| new_ostruct_member!(key)}
|
||||||
end
|
end
|
||||||
|
|
||||||
#
|
#
|
||||||
@ -160,6 +162,7 @@ class OpenStruct
|
|||||||
#
|
#
|
||||||
def marshal_load(x)
|
def marshal_load(x)
|
||||||
@table = x
|
@table = x
|
||||||
|
@table.each_key{|key| new_ostruct_member!(key)}
|
||||||
end
|
end
|
||||||
|
|
||||||
#
|
#
|
||||||
@ -183,7 +186,7 @@ class OpenStruct
|
|||||||
#
|
#
|
||||||
def new_ostruct_member!(name) # :nodoc:
|
def new_ostruct_member!(name) # :nodoc:
|
||||||
name = name.to_sym
|
name = name.to_sym
|
||||||
unless singleton_class.method_defined?(name)
|
unless respond_to?(name)
|
||||||
define_singleton_method(name) { @table[name] }
|
define_singleton_method(name) { @table[name] }
|
||||||
define_singleton_method("#{name}=") {|x| modifiable?[name] = x}
|
define_singleton_method("#{name}=") {|x| modifiable?[name] = x}
|
||||||
end
|
end
|
||||||
@ -191,16 +194,6 @@ class OpenStruct
|
|||||||
end
|
end
|
||||||
private :new_ostruct_member!
|
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:
|
def method_missing(mid, *args) # :nodoc:
|
||||||
len = args.length
|
len = args.length
|
||||||
if mname = mid[/.*(?==\z)/m]
|
if mname = mid[/.*(?==\z)/m]
|
||||||
@ -208,11 +201,7 @@ class OpenStruct
|
|||||||
raise ArgumentError, "wrong number of arguments (given #{len}, expected 1)", caller(1)
|
raise ArgumentError, "wrong number of arguments (given #{len}, expected 1)", caller(1)
|
||||||
end
|
end
|
||||||
modifiable?[new_ostruct_member!(mname)] = args[0]
|
modifiable?[new_ostruct_member!(mname)] = args[0]
|
||||||
elsif len == 0 # and /\A[a-z_]\w*\z/ =~ mid #
|
elsif len == 0
|
||||||
if @table.key?(mid)
|
|
||||||
new_ostruct_member!(mid) unless frozen?
|
|
||||||
@table[mid]
|
|
||||||
end
|
|
||||||
elsif @table.key?(mid)
|
elsif @table.key?(mid)
|
||||||
raise ArgumentError, "wrong number of arguments (given #{len}, expected 0)"
|
raise ArgumentError, "wrong number of arguments (given #{len}, expected 0)"
|
||||||
else
|
else
|
||||||
|
@ -179,7 +179,6 @@ class TC_OpenStruct < Test::Unit::TestCase
|
|||||||
def test_accessor_defines_method
|
def test_accessor_defines_method
|
||||||
os = OpenStruct.new(foo: 42)
|
os = OpenStruct.new(foo: 42)
|
||||||
assert_respond_to(os, :foo)
|
assert_respond_to(os, :foo)
|
||||||
assert_equal([], os.singleton_methods)
|
|
||||||
assert_equal(42, os.foo)
|
assert_equal(42, os.foo)
|
||||||
assert_equal([:foo, :foo=], os.singleton_methods.sort)
|
assert_equal([:foo, :foo=], os.singleton_methods.sort)
|
||||||
end
|
end
|
||||||
@ -225,4 +224,10 @@ class TC_OpenStruct < Test::Unit::TestCase
|
|||||||
os.foo true, true
|
os.foo true, true
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user