1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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
This commit is contained in:
marcandre 2010-05-30 16:43:52 +00:00
parent 1d06ff9761
commit 89efbfe0fa
2 changed files with 41 additions and 4 deletions

View file

@ -1,3 +1,10 @@
Mon May 31 01:43:42 2010 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
* 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.
Sun May 30 22:18:49 2010 Marc-Andre Lafortune <ruby-core@marc-andre.ca> Sun May 30 22:18:49 2010 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
* lib/set.rb (keep_if, select!): New methods [ruby-core:29749] * lib/set.rb (keep_if, select!): New methods [ruby-core:29749]

View file

@ -160,6 +160,32 @@ class Delegator < BasicObject
r r
end 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. # Returns true if two objects are considered same.
# #
@ -281,10 +307,10 @@ end
# #
def DelegateClass(superclass) def DelegateClass(superclass)
klass = Class.new(Delegator) klass = Class.new(Delegator)
methods = superclass.public_instance_methods(true) methods = superclass.instance_methods
methods -= ::Delegator.public_api methods -= ::Delegator.public_api
methods -= [:to_s,:inspect,:=~,:!~,:===] methods -= [:to_s,:inspect,:=~,:!~,:===]
klass.module_eval { klass.module_eval do
def __getobj__ # :nodoc: def __getobj__ # :nodoc:
@delegate_dc_obj @delegate_dc_obj
end end
@ -292,12 +318,16 @@ def DelegateClass(superclass)
raise ArgumentError, "cannot delegate to self" if self.equal?(obj) raise ArgumentError, "cannot delegate to self" if self.equal?(obj)
@delegate_dc_obj = obj @delegate_dc_obj = obj
end end
}
klass.module_eval do
methods.each do |method| methods.each do |method|
define_method(method, Delegator.delegating_block(method)) define_method(method, Delegator.delegating_block(method))
end end
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 return klass
end end