diff options
author | Daniel Schierbeck <daniel.schierbeck@gmail.com> | 2011-08-29 13:07:03 +0200 |
---|---|---|
committer | Daniel Schierbeck <dasch@zendesk.com> | 2012-04-12 13:47:52 +0200 |
commit | 1bac04e854b42fc0e47162e251105434d356d2b4 (patch) | |
tree | 6efbf7e413dd4dd7346ed4945283efd6d0fb15c8 /activesupport/lib | |
parent | 1db54dffaffb83c7a1dacb4db7e4204c7bd1ddba (diff) | |
download | rails-1bac04e854b42fc0e47162e251105434d356d2b4.tar.gz rails-1bac04e854b42fc0e47162e251105434d356d2b4.tar.bz2 rails-1bac04e854b42fc0e47162e251105434d356d2b4.zip |
Optimize the performance of #delegate
Remove the use of #__send__ in order to boost performance. This also
means that you can no longer delegate to private methods on the
target object.
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/core_ext/module/delegation.rb | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/activesupport/lib/active_support/core_ext/module/delegation.rb b/activesupport/lib/active_support/core_ext/module/delegation.rb index af92b869fd..0ea58d4224 100644 --- a/activesupport/lib/active_support/core_ext/module/delegation.rb +++ b/activesupport/lib/active_support/core_ext/module/delegation.rb @@ -124,23 +124,27 @@ class Module file, line = caller.first.split(':', 2) line = line.to_i - if allow_nil - methods.each do |method| + methods.each do |method| + method = method.to_s + + # Attribute writer methods only accept one argument. Makes sure []= + # methods still accept two arguments. + definition = (method =~ /[^\]]=$/) ? "arg" : "*args, &block" + + if allow_nil module_eval(<<-EOS, file, line - 2) - def #{method_prefix}#{method}(*args, &block) # def customer_name(*args, &block) + def #{method_prefix}#{method}(#{definition}) # def customer_name(*args, &block) if #{to} || #{to}.respond_to?(:#{method}) # if client || client.respond_to?(:name) - #{to}.__send__(:#{method}, *args, &block) # client.__send__(:name, *args, &block) + #{to}.#{method}(#{definition}) # client.name(*args, &block) end # end end # end EOS - end - else - methods.each do |method| + else exception = %(raise "#{self}##{method_prefix}#{method} delegated to #{to}.#{method}, but #{to} is nil: \#{self.inspect}") module_eval(<<-EOS, file, line - 1) - def #{method_prefix}#{method}(*args, &block) # def customer_name(*args, &block) - #{to}.__send__(:#{method}, *args, &block) # client.__send__(:name, *args, &block) + def #{method_prefix}#{method}(#{definition}) # def customer_name(*args, &block) + #{to}.#{method}(#{definition}) # client.name(*args, &block) rescue NoMethodError # rescue NoMethodError if #{to}.nil? # if client.nil? #{exception} # # add helpful message to the exception |