mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Initial revision
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
3db12e8b23
commit
62e41d3f2e
56 changed files with 13850 additions and 0 deletions
44
lib/delegate.rb
Normal file
44
lib/delegate.rb
Normal file
|
@ -0,0 +1,44 @@
|
|||
# Delegation class that delegates even methods defined in super class,
|
||||
# which can not be covered with normal method_missing hack.
|
||||
#
|
||||
# Delegater is the abstract delegation class. Need to redefine
|
||||
# `__getobj__' method in the subclass. SimpleDelegater is the
|
||||
# concrete subclass for simple delegation.
|
||||
#
|
||||
# Usage:
|
||||
# foo = Object.new
|
||||
# foo = SimpleDelegater.new(foo)
|
||||
# foo.type # => Object
|
||||
|
||||
class Delegater
|
||||
|
||||
def initialize(obj)
|
||||
preserved = ["id", "equal?", "__getobj__"]
|
||||
for t in self.type.ancestors
|
||||
preserved |= t.instance_methods
|
||||
break if t == Delegater
|
||||
end
|
||||
for method in obj.methods
|
||||
next if preserved.include? method
|
||||
eval "def self.#{method}(*args); __getobj__.send :#{method}, *args; end"
|
||||
end
|
||||
end
|
||||
|
||||
def __getobj__
|
||||
raise NotImplementError, "need to define `__getobj__'"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class SimpleDelegater<Delegater
|
||||
|
||||
def initialize(obj)
|
||||
super
|
||||
@obj = obj
|
||||
end
|
||||
|
||||
def __getobj__
|
||||
@obj
|
||||
end
|
||||
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue