1998-01-16 15:19:09 +03:00
|
|
|
# Delegation class that delegates even methods defined in super class,
|
|
|
|
# which can not be covered with normal method_missing hack.
|
|
|
|
#
|
1999-01-20 07:59:39 +03:00
|
|
|
# Delegator is the abstract delegation class. Need to redefine
|
|
|
|
# `__getobj__' method in the subclass. SimpleDelegator is the
|
1998-01-16 15:19:09 +03:00
|
|
|
# concrete subclass for simple delegation.
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
# foo = Object.new
|
1999-01-20 07:59:39 +03:00
|
|
|
# foo2 = SimpleDelegator.new(foo)
|
2001-03-13 08:45:13 +03:00
|
|
|
# foo.hash == foo2.hash # => false
|
1999-01-20 07:59:39 +03:00
|
|
|
#
|
|
|
|
# Foo = DelegateClass(Array)
|
|
|
|
#
|
|
|
|
# class ExtArray<DelegateClass(Array)
|
|
|
|
# ...
|
|
|
|
# end
|
1998-01-16 15:19:09 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
class Delegator
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
def initialize(obj)
|
2003-05-02 20:11:48 +04:00
|
|
|
preserved = ::Kernel.public_instance_methods(false)
|
1999-01-20 07:59:39 +03:00
|
|
|
preserved -= ["to_s","to_a","inspect","==","=~","==="]
|
2002-10-02 19:32:45 +04:00
|
|
|
for t in self.class.ancestors
|
2003-05-02 20:11:48 +04:00
|
|
|
preserved |= t.public_instance_methods(false)
|
|
|
|
preserved |= t.private_instance_methods(false)
|
|
|
|
preserved |= t.protected_instance_methods(false)
|
1999-01-20 07:59:39 +03:00
|
|
|
break if t == Delegator
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
2004-01-27 09:05:04 +03:00
|
|
|
preserved << "singleton_method_added"
|
1998-01-16 15:19:09 +03:00
|
|
|
for method in obj.methods
|
|
|
|
next if preserved.include? method
|
2001-11-13 11:14:27 +03:00
|
|
|
begin
|
|
|
|
eval <<-EOS
|
|
|
|
def self.#{method}(*args, &block)
|
|
|
|
begin
|
|
|
|
__getobj__.__send__(:#{method}, *args, &block)
|
|
|
|
rescue Exception
|
|
|
|
$@.delete_if{|s| /:in `__getobj__'$/ =~ s} #`
|
|
|
|
$@.delete_if{|s| /^\\(eval\\):/ =~ s}
|
2004-01-27 09:05:04 +03:00
|
|
|
::Kernel::raise
|
2001-11-13 11:14:27 +03:00
|
|
|
end
|
1999-01-20 07:59:39 +03:00
|
|
|
end
|
2001-11-13 11:14:27 +03:00
|
|
|
EOS
|
|
|
|
rescue SyntaxError
|
|
|
|
raise NameError, "invalid identifier %s" % method, caller(4)
|
|
|
|
end
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
|
|
|
end
|
2003-12-03 20:30:09 +03:00
|
|
|
alias initialize_methods initialize
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
def __getobj__
|
2003-03-23 20:58:57 +03:00
|
|
|
raise NotImplementedError, "need to define `__getobj__'"
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
|
|
|
|
2003-08-04 16:45:03 +04:00
|
|
|
def marshal_dump
|
|
|
|
__getobj__
|
|
|
|
end
|
|
|
|
def marshal_load(obj)
|
2003-12-03 20:30:09 +03:00
|
|
|
initialize_methods(obj)
|
2003-08-04 16:45:03 +04:00
|
|
|
end
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
class SimpleDelegator<Delegator
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
def initialize(obj)
|
|
|
|
super
|
2003-12-17 09:38:18 +03:00
|
|
|
@_sd_obj = obj
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def __getobj__
|
2003-12-17 09:38:18 +03:00
|
|
|
@_sd_obj
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
def __setobj__(obj)
|
2003-12-17 09:38:18 +03:00
|
|
|
@_sd_obj = obj
|
1999-01-20 07:59:39 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# backward compatibility ^_^;;;
|
|
|
|
Delegater = Delegator
|
|
|
|
SimpleDelegater = SimpleDelegator
|
|
|
|
|
|
|
|
#
|
|
|
|
def DelegateClass(superclass)
|
|
|
|
klass = Class.new
|
2002-11-14 09:18:59 +03:00
|
|
|
methods = superclass.public_instance_methods(true)
|
2003-05-21 12:48:05 +04:00
|
|
|
methods -= ::Kernel.public_instance_methods(false)
|
1999-01-20 07:59:39 +03:00
|
|
|
methods |= ["to_s","to_a","inspect","==","=~","==="]
|
|
|
|
klass.module_eval <<-EOS
|
|
|
|
def initialize(obj)
|
2003-12-17 09:38:18 +03:00
|
|
|
@_dc_obj = obj
|
1999-01-20 07:59:39 +03:00
|
|
|
end
|
2004-03-31 06:52:44 +04:00
|
|
|
def __getobj__
|
|
|
|
@_dc_obj
|
|
|
|
end
|
|
|
|
def __setobj__(obj)
|
|
|
|
@_dc_obj = obj
|
|
|
|
end
|
1999-01-20 07:59:39 +03:00
|
|
|
EOS
|
|
|
|
for method in methods
|
2001-11-13 11:14:27 +03:00
|
|
|
begin
|
|
|
|
klass.module_eval <<-EOS
|
|
|
|
def #{method}(*args, &block)
|
1999-01-20 07:59:39 +03:00
|
|
|
begin
|
2003-12-17 09:38:18 +03:00
|
|
|
@_dc_obj.__send__(:#{method}, *args, &block)
|
1999-01-20 07:59:39 +03:00
|
|
|
rescue
|
|
|
|
$@[0,2] = nil
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
end
|
2001-11-13 11:14:27 +03:00
|
|
|
EOS
|
|
|
|
rescue SyntaxError
|
|
|
|
raise NameError, "invalid identifier %s" % method, caller(3)
|
|
|
|
end
|
1999-01-20 07:59:39 +03:00
|
|
|
end
|
2004-03-31 06:52:44 +04:00
|
|
|
return klass
|
2001-11-13 11:14:27 +03:00
|
|
|
end
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
if __FILE__ == $0
|
|
|
|
class ExtArray<DelegateClass(Array)
|
|
|
|
def initialize()
|
|
|
|
super([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ary = ExtArray.new
|
2002-10-02 19:32:45 +04:00
|
|
|
p ary.class
|
1999-01-20 07:59:39 +03:00
|
|
|
ary.push 25
|
|
|
|
p ary
|
|
|
|
|
|
|
|
foo = Object.new
|
|
|
|
def foo.test
|
|
|
|
25
|
|
|
|
end
|
|
|
|
def foo.error
|
|
|
|
raise 'this is OK'
|
|
|
|
end
|
|
|
|
foo2 = SimpleDelegator.new(foo)
|
|
|
|
p foo.test == foo2.test # => true
|
|
|
|
foo2.error # raise error!
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|