diff options
author | Jeremy Evans <code@jeremyevans.net> | 2019-05-23 21:10:40 -0700 |
---|---|---|
committer | Jeremy Evans <code@jeremyevans.net> | 2019-05-30 18:34:45 -0700 |
commit | e8c710b11a02c6ab82b358fc671a14f378cb1974 (patch) | |
tree | 591a8d416acc81ec2306064fdebbd001b45806ed /lib/delegate.rb | |
parent | 1cd93f1cdfbe6f7e71b05b3f8e707f21d70e94ba (diff) | |
download | ruby-e8c710b11a02c6ab82b358fc671a14f378cb1974.tar.gz |
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]
Diffstat (limited to 'lib/delegate.rb')
-rw-r--r-- | lib/delegate.rb | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/lib/delegate.rb b/lib/delegate.rb index 859cc2ed84..1e4ab5f145 100644 --- a/lib/delegate.rb +++ b/lib/delegate.rb @@ -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 |