mirror of
https://github.com/ruby/ruby.git
synced 2025-09-15 16:44:01 +02:00
Fix visibility of some methods when using DelegateClass
Public instance methods added to a delegated class after the creation of the delegate class were not returned by the public_instance_methods class method of the delegate class. Protected instance methods in the delegated class when the delegate class is created were returned by the public_methods instance method of the delegate class. Patch mostly from Kenichi Kamiya <kachick1@gmail.com> in GitHub pull request 926. Minor changes to get it to apply, and to fix tests after applying by me. Fixes [Bug #11512]
This commit is contained in:
parent
1cd93f1cdf
commit
e8c710b11a
2 changed files with 42 additions and 5 deletions
|
@ -393,9 +393,11 @@ end
|
|||
#
|
||||
def DelegateClass(superclass, &block)
|
||||
klass = Class.new(Delegator)
|
||||
methods = superclass.instance_methods
|
||||
methods -= ::Delegator.public_api
|
||||
methods -= [:to_s, :inspect, :=~, :!~, :===]
|
||||
ignores = [*::Delegator.public_api, :to_s, :inspect, :=~, :!~, :===]
|
||||
protected_instance_methods = superclass.protected_instance_methods
|
||||
protected_instance_methods -= ignores
|
||||
public_instance_methods = superclass.public_instance_methods
|
||||
public_instance_methods -= ignores
|
||||
klass.module_eval do
|
||||
def __getobj__ # :nodoc:
|
||||
unless defined?(@delegate_dc_obj)
|
||||
|
@ -408,12 +410,16 @@ def DelegateClass(superclass, &block)
|
|||
__raise__ ::ArgumentError, "cannot delegate to self" if self.equal?(obj)
|
||||
@delegate_dc_obj = obj
|
||||
end
|
||||
methods.each do |method|
|
||||
protected_instance_methods.each do |method|
|
||||
define_method(method, Delegator.delegating_block(method))
|
||||
protected method
|
||||
end
|
||||
public_instance_methods.each do |method|
|
||||
define_method(method, Delegator.delegating_block(method))
|
||||
end
|
||||
end
|
||||
klass.define_singleton_method :public_instance_methods do |all=true|
|
||||
super(all) - superclass.protected_instance_methods
|
||||
super(all) | superclass.public_instance_methods
|
||||
end
|
||||
klass.define_singleton_method :protected_instance_methods do |all=true|
|
||||
super(all) | superclass.protected_instance_methods
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue