Created
December 8, 2008 00:56
-
-
Save mischa/33292 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# activesupport/lib/active_support/core_ext/module/delegation.rb | |
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 as the last argument (e.g. delegate :hello, :to => :greeter)." | |
end | |
if options[:prefix] == true && options[:to].to_s =~ /^[^a-z_]/ | |
raise ArgumentError, "Can only automatically set the delegation prefix when delegating to a method." | |
end | |
prefix = options[:prefix] && "#{options[:prefix] == true ? to : options[:prefix]}_" | |
methods.each do |method| | |
module_eval(<<-EOS, "(__DELEGATION__)", 1) | |
def #{prefix}#{method}(*args, &block) | |
#{to}.__send__(#{method.inspect}, *args, &block) | |
end | |
EOS | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment