aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/module/delegation.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2006-02-04 19:14:53 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2006-02-04 19:14:53 +0000
commit2ee6229bd8cf3a87ede0cd8b573e3faf5a15dbfa (patch)
tree6448472533f1bc97162e3d498281a306030a932a /activesupport/lib/active_support/core_ext/module/delegation.rb
parent5cbc062c8ad82271de11ae245d2bdaeffc0c4da7 (diff)
downloadrails-2ee6229bd8cf3a87ede0cd8b573e3faf5a15dbfa.tar.gz
rails-2ee6229bd8cf3a87ede0cd8b573e3faf5a15dbfa.tar.bz2
rails-2ee6229bd8cf3a87ede0cd8b573e3faf5a15dbfa.zip
Added delegation support to Module that allows multiple delegations at once (unlike Forwardable in the stdlib) [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3535 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib/active_support/core_ext/module/delegation.rb')
-rw-r--r--activesupport/lib/active_support/core_ext/module/delegation.rb16
1 files changed, 16 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/module/delegation.rb b/activesupport/lib/active_support/core_ext/module/delegation.rb
new file mode 100644
index 0000000000..95173007ca
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/module/delegation.rb
@@ -0,0 +1,16 @@
+class Module
+ def delegate(*methods)
+ options = methods.pop
+ unless options.is_a?(Hash) && to = options[:to]
+ raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key"
+ end
+
+ methods.each do |method|
+ module_eval(<<-EOS, "(__DELEGATION__)", 1)
+ def #{method}(*args, &block)
+ #{to}.__send__(#{method.inspect}, *args, &block)
+ end
+ EOS
+ end
+ end
+end \ No newline at end of file