diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2009-06-18 20:00:24 -0400 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2009-06-18 20:00:35 -0400 |
commit | 63cc6b7242993a5c4d96df4479a4ad769f911576 (patch) | |
tree | f8ac4a33c5edd42cd883c973fb572b99552464ee /activesupport | |
parent | 50fe928c1539756cb1e3c5e7faf99c2b61979c08 (diff) | |
download | rails-63cc6b7242993a5c4d96df4479a4ad769f911576.tar.gz rails-63cc6b7242993a5c4d96df4479a4ad769f911576.tar.bz2 rails-63cc6b7242993a5c4d96df4479a4ad769f911576.zip |
Friendlier runtime exception if delegatee is nil
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/core_ext/module/delegation.rb | 12 | ||||
-rw-r--r-- | activesupport/test/core_ext/module_test.rb | 2 |
2 files changed, 10 insertions, 4 deletions
diff --git a/activesupport/lib/active_support/core_ext/module/delegation.rb b/activesupport/lib/active_support/core_ext/module/delegation.rb index cea3fb7f57..11e01437aa 100644 --- a/activesupport/lib/active_support/core_ext/module/delegation.rb +++ b/activesupport/lib/active_support/core_ext/module/delegation.rb @@ -108,15 +108,21 @@ class Module prefix = options[:prefix] && "#{options[:prefix] == true ? to : options[:prefix]}_" - allow_nil = options[:allow_nil] && "#{to} && " - file, line = caller.first.split(':', 2) line = line.to_i methods.each do |method| + on_nil = + if options[:allow_nil] + 'return' + else + %(raise "#{self}##{prefix}#{method} delegated to #{to}.#{method}, but #{to} is nil: \#{self.inspect}") + end + module_eval(<<-EOS, file, line) def #{prefix}#{method}(*args, &block) # def customer_name(*args, &block) - #{allow_nil}#{to}.__send__(#{method.inspect}, *args, &block) # client && client.__send__(:name, *args, &block) + #{on_nil} if #{to}.nil? + #{to}.__send__(#{method.inspect}, *args, &block) # client && client.__send__(:name, *args, &block) end # end EOS end diff --git a/activesupport/test/core_ext/module_test.rb b/activesupport/test/core_ext/module_test.rb index c3c696f93a..f8387ae4ab 100644 --- a/activesupport/test/core_ext/module_test.rb +++ b/activesupport/test/core_ext/module_test.rb @@ -142,7 +142,7 @@ class ModuleTest < Test::Unit::TestCase def test_delegation_without_allow_nil_and_nil_value david = Someone.new("David") - assert_raise(NoMethodError) { david.street } + assert_raise(RuntimeError) { david.street } end def test_parent |