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