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/lib | |
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/lib')
-rw-r--r-- | activesupport/lib/active_support/core_ext/module/delegation.rb | 12 |
1 files changed, 9 insertions, 3 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 |