diff options
-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 |