From 2ee6229bd8cf3a87ede0cd8b573e3faf5a15dbfa Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 4 Feb 2006 19:14:53 +0000 Subject: 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 --- activesupport/lib/active_support/core_ext/module.rb | 3 ++- .../lib/active_support/core_ext/module/delegation.rb | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 activesupport/lib/active_support/core_ext/module/delegation.rb (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/module.rb b/activesupport/lib/active_support/core_ext/module.rb index 0f46631e65..bd6d62468a 100644 --- a/activesupport/lib/active_support/core_ext/module.rb +++ b/activesupport/lib/active_support/core_ext/module.rb @@ -1,2 +1,3 @@ require File.dirname(__FILE__) + '/module/inclusion' -require File.dirname(__FILE__) + '/module/attribute_accessors' \ No newline at end of file +require File.dirname(__FILE__) + '/module/attribute_accessors' +require File.dirname(__FILE__) + '/module/delegation' \ No newline at end of file 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 -- cgit v1.2.3