mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Revert 88d08f2ec9
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
This commit is contained in:
parent
c9cc1f47ad
commit
d9cd1e9222
2 changed files with 25 additions and 13 deletions
|
@ -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
|
||||
|
|
|
@ -78,7 +78,7 @@ Product = Struct.new(:name) do
|
|||
|
||||
def type
|
||||
@type ||= begin
|
||||
:thing_without_same_method_name_as_delegated.name
|
||||
nil.type_name
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue