aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/module/delegation.rb
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2015-02-11 17:32:18 -0200
committerRafael Mendonça França <rafaelmfranca@gmail.com>2015-02-11 17:32:18 -0200
commitd9cd1e9222455836117f47160a43310929434ff0 (patch)
tree3f07a11944af95cb5c1b01d11d5bb6af0f95ac9b /activesupport/lib/active_support/core_ext/module/delegation.rb
parentc9cc1f47adbfe6bcdca37fb1d1338999219c7b74 (diff)
downloadrails-d9cd1e9222455836117f47160a43310929434ff0.tar.gz
rails-d9cd1e9222455836117f47160a43310929434ff0.tar.bz2
rails-d9cd1e9222455836117f47160a43310929434ff0.zip
Revert 88d08f2ec9f89ba431cba8d0c06ac9ebc204bbbb
This caused a performance regression since we were decided to do the nil check in run time not in the load time. See https://github.com/rails/rails/pull/15187#issuecomment-71760058
Diffstat (limited to 'activesupport/lib/active_support/core_ext/module/delegation.rb')
-rw-r--r--activesupport/lib/active_support/core_ext/module/delegation.rb36
1 files changed, 24 insertions, 12 deletions
diff --git a/activesupport/lib/active_support/core_ext/module/delegation.rb b/activesupport/lib/active_support/core_ext/module/delegation.rb
index 24df83800b..a5f4d03256 100644
--- a/activesupport/lib/active_support/core_ext/module/delegation.rb
+++ b/activesupport/lib/active_support/core_ext/module/delegation.rb
@@ -185,19 +185,31 @@ class Module
# On the other hand it could be that the target has side-effects,
# whereas conceptually, from the user point of view, the delegator should
# be doing one call.
-
- exception = %(raise DelegationError, "#{self}##{method_prefix}#{method} delegated to #{to}.#{method}, but #{to} is nil: \#{self.inspect}")
-
- method_def = [
- "def #{method_prefix}#{method}(#{definition})",
- " _ = #{to}",
- " if !_.nil? || nil.respond_to?(:#{method})",
- " _.#{method}(#{definition})",
- " else",
- " #{exception unless allow_nil}",
- " end",
+ if allow_nil
+ method_def = [
+ "def #{method_prefix}#{method}(#{definition})",
+ "_ = #{to}",
+ "if !_.nil? || nil.respond_to?(:#{method})",
+ " _.#{method}(#{definition})",
+ "end",
"end"
- ].join ';'
+ ].join ';'
+ else
+ exception = %(raise DelegationError, "#{self}##{method_prefix}#{method} delegated to #{to}.#{method}, but #{to} is nil: \#{self.inspect}")
+
+ method_def = [
+ "def #{method_prefix}#{method}(#{definition})",
+ " _ = #{to}",
+ " _.#{method}(#{definition})",
+ "rescue NoMethodError => e",
+ " if _.nil? && e.name == :#{method}",
+ " #{exception}",
+ " else",
+ " raise",
+ " end",
+ "end"
+ ].join ';'
+ end
module_eval(method_def, file, line)
end