summaryrefslogtreecommitdiff
path: root/lib/delegate.rb
diff options
context:
space:
mode:
author(no author) <(no author)@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1998-01-16 12:19:22 +0000
committer(no author) <(no author)@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1998-01-16 12:19:22 +0000
commitf12baed5df6d3c213dd75d2f0d9f36bb179fb843 (patch)
treec39f9c14cd6f8bcf85b6b842a2774cafb102bc2e /lib/delegate.rb
parent22ab6d39643b1bbc997e15b763d2b12996b1f1b9 (diff)
This commit was manufactured by cvs2svn to create branch 'RUBY'.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/RUBY@9 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/delegate.rb')
-rw-r--r--lib/delegate.rb44
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/delegate.rb b/lib/delegate.rb
new file mode 100644
index 0000000000..e5943cead8
--- /dev/null
+++ b/lib/delegate.rb
@@ -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