summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authormarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-05-30 16:43:52 +0000
committermarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-05-30 16:43:52 +0000
commit89efbfe0fa7939d88fa7cce44df0c144cafd82f1 (patch)
treeb8aa821f79e62987b1675a8f29e42077d0c168a6 /lib
parent1d06ff976184b4956a58a867132061c6a5902cd6 (diff)
* lib/delegate: Delegator: combine (public|protected) methods with
those of the delegated object. [ruby-core:27224] DelegateClass: combine (public|protected) instance methods with those of the delegated superclass. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@28099 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r--lib/delegate.rb38
1 files changed, 34 insertions, 4 deletions
diff --git a/lib/delegate.rb b/lib/delegate.rb
index f3660911e6..dead782bbb 100644
--- a/lib/delegate.rb
+++ b/lib/delegate.rb
@@ -161,6 +161,32 @@ class Delegator < BasicObject
end
#
+ # Returns the methods available to this delegate object as the union
+ # of this object's and \_\_getobj\_\_ methods.
+ #
+ def methods
+ __getobj__.methods | super
+ end
+
+ #
+ # Returns the methods available to this delegate object as the union
+ # of this object's and \_\_getobj\_\_ public methods.
+ #
+ def public_methods(all=true)
+ __getobj__.public_methods(all) | super
+ end
+
+ #
+ # Returns the methods available to this delegate object as the union
+ # of this object's and \_\_getobj\_\_ protected methods.
+ #
+ def protected_methods(all=true)
+ __getobj__.protected_methods(all) | super
+ end
+
+ # Note: no need to specialize private_methods, since they are not forwarded
+
+ #
# Returns true if two objects are considered same.
#
def ==(obj)
@@ -281,10 +307,10 @@ end
#
def DelegateClass(superclass)
klass = Class.new(Delegator)
- methods = superclass.public_instance_methods(true)
+ methods = superclass.instance_methods
methods -= ::Delegator.public_api
methods -= [:to_s,:inspect,:=~,:!~,:===]
- klass.module_eval {
+ klass.module_eval do
def __getobj__ # :nodoc:
@delegate_dc_obj
end
@@ -292,12 +318,16 @@ def DelegateClass(superclass)
raise ArgumentError, "cannot delegate to self" if self.equal?(obj)
@delegate_dc_obj = obj
end
- }
- klass.module_eval do
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
+ end
+ klass.define_singleton_method :protected_instance_methods do |all=true|
+ super(all) | superclass.protected_instance_methods
+ end
return klass
end