mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
parent
73ba2c14cd
commit
44c51fc9cc
2 changed files with 32 additions and 20 deletions
|
@ -178,30 +178,32 @@ class Module
|
||||||
# whereas conceptually, from the user point of view, the delegator should
|
# whereas conceptually, from the user point of view, the delegator should
|
||||||
# be doing one call.
|
# be doing one call.
|
||||||
if allow_nil
|
if allow_nil
|
||||||
module_eval(<<-EOS, file, line - 3)
|
method_def = [
|
||||||
def #{method_prefix}#{method}(#{definition}) # def customer_name(*args, &block)
|
"def #{method_prefix}#{method}(#{definition})", # def customer_name(*args, &block)
|
||||||
_ = #{to} # _ = client
|
"_ = #{to}", # _ = client
|
||||||
if !_.nil? || nil.respond_to?(:#{method}) # if !_.nil? || nil.respond_to?(:name)
|
"if !_.nil? || nil.respond_to?(:#{method})", # if !_.nil? || nil.respond_to?(:name)
|
||||||
_.#{method}(#{definition}) # _.name(*args, &block)
|
" _.#{method}(#{definition})", # _.name(*args, &block)
|
||||||
end # end
|
"end", # end
|
||||||
end # end
|
"end" # end
|
||||||
EOS
|
].join ';'
|
||||||
else
|
else
|
||||||
exception = %(raise DelegationError, "#{self}##{method_prefix}#{method} delegated to #{to}.#{method}, but #{to} is nil: \#{self.inspect}")
|
exception = %(raise DelegationError, "#{self}##{method_prefix}#{method} delegated to #{to}.#{method}, but #{to} is nil: \#{self.inspect}")
|
||||||
|
|
||||||
module_eval(<<-EOS, file, line - 2)
|
method_def = [
|
||||||
def #{method_prefix}#{method}(#{definition}) # def customer_name(*args, &block)
|
"def #{method_prefix}#{method}(#{definition})", # def customer_name(*args, &block)
|
||||||
_ = #{to} # _ = client
|
" _ = #{to}", # _ = client
|
||||||
_.#{method}(#{definition}) # _.name(*args, &block)
|
" _.#{method}(#{definition})", # _.name(*args, &block)
|
||||||
rescue NoMethodError => e # rescue NoMethodError => e
|
"rescue NoMethodError => e", # rescue NoMethodError => e
|
||||||
if _.nil? && e.name == :#{method} # if _.nil? && e.name == :name
|
" if _.nil? && e.name == :#{method}", # if _.nil? && e.name == :name
|
||||||
#{exception} # # add helpful message to the exception
|
" #{exception}", # # add helpful message to the exception
|
||||||
else # else
|
" else", # else
|
||||||
raise # raise
|
" raise", # raise
|
||||||
end # end
|
" end", # end
|
||||||
end # end
|
"end" # end
|
||||||
EOS
|
].join ';'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
module_eval(method_def, file, line)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -245,6 +245,16 @@ class ModuleTest < ActiveSupport::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_delegation_line_number
|
||||||
|
file, line = Someone.instance_method(:foo).source_location
|
||||||
|
assert_equal Someone::FAILED_DELEGATE_LINE, line
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_delegate_line_with_nil
|
||||||
|
file, line = Someone.instance_method(:bar).source_location
|
||||||
|
assert_equal Someone::FAILED_DELEGATE_LINE_2, line
|
||||||
|
end
|
||||||
|
|
||||||
def test_delegation_exception_backtrace
|
def test_delegation_exception_backtrace
|
||||||
someone = Someone.new("foo", "bar")
|
someone = Someone.new("foo", "bar")
|
||||||
someone.foo
|
someone.foo
|
||||||
|
|
Loading…
Reference in a new issue