1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
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:
Rafael Mendonça França 2015-02-11 17:32:18 -02:00
parent c9cc1f47ad
commit d9cd1e9222
2 changed files with 25 additions and 13 deletions

View file

@ -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

View file

@ -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