зеркало из https://github.com/github/ruby.git
Add instance_methods to class generated by DelegateClass
Also, make DelegateClass.instance_method fallback to superclass. Fixes [Bug #16982]
This commit is contained in:
Родитель
7a479b30b6
Коммит
ba81bc24e6
|
@ -422,6 +422,21 @@ def DelegateClass(superclass, &block)
|
|||
klass.define_singleton_method :protected_instance_methods do |all=true|
|
||||
super(all) | superclass.protected_instance_methods
|
||||
end
|
||||
klass.define_singleton_method :instance_methods do |all=true|
|
||||
super(all) | superclass.instance_methods
|
||||
end
|
||||
klass.define_singleton_method :public_instance_method do |name|
|
||||
super(name)
|
||||
rescue NameError
|
||||
raise unless self.public_instance_methods.include?(name)
|
||||
superclass.public_instance_method(name)
|
||||
end
|
||||
klass.define_singleton_method :instance_method do |name|
|
||||
super(name)
|
||||
rescue NameError
|
||||
raise unless self.instance_methods.include?(name)
|
||||
superclass.instance_method(name)
|
||||
end
|
||||
klass.module_eval(&block) if block
|
||||
return klass
|
||||
end
|
||||
|
|
|
@ -106,6 +106,10 @@ class TestDelegateClass < Test::Unit::TestCase
|
|||
protected
|
||||
|
||||
def parent_protected; end
|
||||
|
||||
private
|
||||
|
||||
def parent_private; end
|
||||
end
|
||||
|
||||
class Child < DelegateClass(Parent)
|
||||
|
@ -117,6 +121,10 @@ class TestDelegateClass < Test::Unit::TestCase
|
|||
protected
|
||||
|
||||
def parent_protected_added; end
|
||||
|
||||
private
|
||||
|
||||
def parent_private_added; end
|
||||
end
|
||||
|
||||
def test_public_instance_methods
|
||||
|
@ -131,6 +139,32 @@ class TestDelegateClass < Test::Unit::TestCase
|
|||
assert_equal([:parent_protected, :parent_protected_added], (Child.new(Parent.new).protected_methods - ignores).sort)
|
||||
end
|
||||
|
||||
def test_instance_methods
|
||||
ignores = Object.instance_methods | Delegator.instance_methods
|
||||
assert_equal([:parent_protected, :parent_protected_added, :parent_public, :parent_public_added], (Child.instance_methods - ignores).sort)
|
||||
assert_equal([:parent_protected, :parent_protected_added, :parent_public, :parent_public_added], (Child.new(Parent.new).methods - ignores).sort)
|
||||
end
|
||||
|
||||
def test_DelegateClass_instance_method
|
||||
assert_instance_of UnboundMethod, Child.instance_method(:parent_public)
|
||||
assert_instance_of UnboundMethod, Child.instance_method(:parent_public_added)
|
||||
assert_instance_of UnboundMethod, Child.instance_method(:parent_protected)
|
||||
assert_instance_of UnboundMethod, Child.instance_method(:parent_protected_added)
|
||||
assert_raise(NameError) { Child.instance_method(:parent_private) }
|
||||
assert_raise(NameError) { Child.instance_method(:parent_private_added) }
|
||||
assert_instance_of UnboundMethod, Child.instance_method(:to_s)
|
||||
end
|
||||
|
||||
def test_DelegateClass_public_instance_method
|
||||
assert_instance_of UnboundMethod, Child.public_instance_method(:parent_public)
|
||||
assert_instance_of UnboundMethod, Child.public_instance_method(:parent_public_added)
|
||||
assert_raise(NameError) { Child.public_instance_method(:parent_protected) }
|
||||
assert_raise(NameError) { Child.public_instance_method(:parent_protected_added) }
|
||||
assert_raise(NameError) { Child.instance_method(:parent_private) }
|
||||
assert_raise(NameError) { Child.instance_method(:parent_private_added) }
|
||||
assert_instance_of UnboundMethod, Child.public_instance_method(:to_s)
|
||||
end
|
||||
|
||||
class IV < DelegateClass(Integer)
|
||||
attr_accessor :var
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче