mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/delegate.rb (DelegateClass): restored 1.8 behavior for
DelegateClass as well. [ruby-dev:36739] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19795 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
55ae1d90a3
commit
7a0acc7eee
2 changed files with 81 additions and 84 deletions
|
@ -1,3 +1,8 @@
|
||||||
|
Wed Oct 15 23:11:10 2008 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||||
|
|
||||||
|
* lib/delegate.rb (DelegateClass): restored 1.8 behavior for
|
||||||
|
DelegateClass as well. [ruby-dev:36739]
|
||||||
|
|
||||||
Wed Oct 15 22:19:14 2008 NAKAMURA Usaku <usa@ruby-lang.org>
|
Wed Oct 15 22:19:14 2008 NAKAMURA Usaku <usa@ruby-lang.org>
|
||||||
|
|
||||||
* array.c (RESIZE_CAPA): check whether len is longer than capacity.
|
* array.c (RESIZE_CAPA): check whether len is longer than capacity.
|
||||||
|
|
160
lib/delegate.rb
160
lib/delegate.rb
|
@ -119,86 +119,83 @@ class Delegator
|
||||||
undef_method m
|
undef_method m
|
||||||
end
|
end
|
||||||
|
|
||||||
module MethodDelegation
|
#
|
||||||
#
|
# Pass in the _obj_ to delegate method calls to. All methods supported by
|
||||||
# Pass in the _obj_ to delegate method calls to. All methods supported by
|
# _obj_ will be delegated to.
|
||||||
# _obj_ will be delegated to.
|
#
|
||||||
#
|
def initialize(obj)
|
||||||
def initialize(obj)
|
__setobj__(obj)
|
||||||
__setobj__(obj)
|
end
|
||||||
end
|
|
||||||
|
|
||||||
# Handles the magic of delegation through \_\_getobj\_\_.
|
# Handles the magic of delegation through \_\_getobj\_\_.
|
||||||
def method_missing(m, *args, &block)
|
def method_missing(m, *args, &block)
|
||||||
begin
|
begin
|
||||||
target = self.__getobj__
|
target = self.__getobj__
|
||||||
unless target.respond_to?(m)
|
unless target.respond_to?(m)
|
||||||
super(m, *args, &block)
|
super(m, *args, &block)
|
||||||
else
|
else
|
||||||
target.__send__(m, *args, &block)
|
target.__send__(m, *args, &block)
|
||||||
end
|
|
||||||
rescue Exception
|
|
||||||
$@.delete_if{|s| %r"\A#{__FILE__}:\d+:in `method_missing'\z"o =~ s}
|
|
||||||
::Kernel::raise
|
|
||||||
end
|
end
|
||||||
end
|
rescue Exception
|
||||||
|
$@.delete_if{|s| %r"\A#{__FILE__}:\d+:in `method_missing'\z"o =~ s}
|
||||||
#
|
::Kernel::raise
|
||||||
# Checks for a method provided by this the delegate object by fowarding the
|
|
||||||
# call through \_\_getobj\_\_.
|
|
||||||
#
|
|
||||||
def respond_to?(m, include_private = false)
|
|
||||||
return true if super
|
|
||||||
return self.__getobj__.respond_to?(m, include_private)
|
|
||||||
end
|
|
||||||
|
|
||||||
#
|
|
||||||
# Returns true if two objects are considered same.
|
|
||||||
#
|
|
||||||
def ==(obj)
|
|
||||||
return true if obj.equal?(self)
|
|
||||||
self.__getobj__ == obj
|
|
||||||
end
|
|
||||||
|
|
||||||
#
|
|
||||||
# This method must be overridden by subclasses and should return the object
|
|
||||||
# method calls are being delegated to.
|
|
||||||
#
|
|
||||||
def __getobj__
|
|
||||||
raise NotImplementedError, "need to define `__getobj__'"
|
|
||||||
end
|
|
||||||
|
|
||||||
#
|
|
||||||
# This method must be overridden by subclasses and change the object delegate
|
|
||||||
# to _obj_.
|
|
||||||
#
|
|
||||||
def __setobj__(obj)
|
|
||||||
raise NotImplementedError, "need to define `__setobj__'"
|
|
||||||
end
|
|
||||||
|
|
||||||
# Serialization support for the object returned by \_\_getobj\_\_.
|
|
||||||
def marshal_dump
|
|
||||||
__getobj__
|
|
||||||
end
|
|
||||||
# Reinitializes delegation from a serialized object.
|
|
||||||
def marshal_load(obj)
|
|
||||||
__setobj__(obj)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Clone support for the object returned by \_\_getobj\_\_.
|
|
||||||
def clone
|
|
||||||
new = super
|
|
||||||
new.__setobj__(__getobj__.clone)
|
|
||||||
new
|
|
||||||
end
|
|
||||||
# Duplication support for the object returned by \_\_getobj\_\_.
|
|
||||||
def dup
|
|
||||||
new = super
|
|
||||||
new.__setobj__(__getobj__.dup)
|
|
||||||
new
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
include MethodDelegation
|
|
||||||
|
#
|
||||||
|
# Checks for a method provided by this the delegate object by fowarding the
|
||||||
|
# call through \_\_getobj\_\_.
|
||||||
|
#
|
||||||
|
def respond_to?(m, include_private = false)
|
||||||
|
return true if super
|
||||||
|
return self.__getobj__.respond_to?(m, include_private)
|
||||||
|
end
|
||||||
|
|
||||||
|
#
|
||||||
|
# Returns true if two objects are considered same.
|
||||||
|
#
|
||||||
|
def ==(obj)
|
||||||
|
return true if obj.equal?(self)
|
||||||
|
self.__getobj__ == obj
|
||||||
|
end
|
||||||
|
|
||||||
|
#
|
||||||
|
# This method must be overridden by subclasses and should return the object
|
||||||
|
# method calls are being delegated to.
|
||||||
|
#
|
||||||
|
def __getobj__
|
||||||
|
raise NotImplementedError, "need to define `__getobj__'"
|
||||||
|
end
|
||||||
|
|
||||||
|
#
|
||||||
|
# This method must be overridden by subclasses and change the object delegate
|
||||||
|
# to _obj_.
|
||||||
|
#
|
||||||
|
def __setobj__(obj)
|
||||||
|
raise NotImplementedError, "need to define `__setobj__'"
|
||||||
|
end
|
||||||
|
|
||||||
|
# Serialization support for the object returned by \_\_getobj\_\_.
|
||||||
|
def marshal_dump
|
||||||
|
__getobj__
|
||||||
|
end
|
||||||
|
# Reinitializes delegation from a serialized object.
|
||||||
|
def marshal_load(obj)
|
||||||
|
__setobj__(obj)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Clone support for the object returned by \_\_getobj\_\_.
|
||||||
|
def clone
|
||||||
|
new = super
|
||||||
|
new.__setobj__(__getobj__.clone)
|
||||||
|
new
|
||||||
|
end
|
||||||
|
# Duplication support for the object returned by \_\_getobj\_\_.
|
||||||
|
def dup
|
||||||
|
new = super
|
||||||
|
new.__setobj__(__getobj__.dup)
|
||||||
|
new
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
#
|
#
|
||||||
|
@ -258,16 +255,11 @@ end
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
def DelegateClass(superclass)
|
def DelegateClass(superclass)
|
||||||
klass = Class.new
|
klass = Class.new(Delegator)
|
||||||
methods = superclass.public_instance_methods(true)
|
methods = superclass.public_instance_methods(true)
|
||||||
methods -= [
|
methods -= ::Delegator.public_instance_methods
|
||||||
:__id__, :object_id, :__send__, :public_send, :respond_to?, :send,
|
methods -= [:to_s,:inspect,:=~,:!~,:===]
|
||||||
:==, :equal?, :initialize, :method_missing, :__getobj__, :__setobj__,
|
|
||||||
:clone, :dup, :marshal_dump, :marshal_load, :instance_eval, :instance_exec,
|
|
||||||
:extend,
|
|
||||||
]
|
|
||||||
klass.module_eval {
|
klass.module_eval {
|
||||||
include Delegator::MethodDelegation
|
|
||||||
def __getobj__ # :nodoc:
|
def __getobj__ # :nodoc:
|
||||||
@delegate_dc_obj
|
@delegate_dc_obj
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue