2008-04-04 23:52:58 -04:00
|
|
|
# Extensions to +nil+ which allow for more helpful error messages for people who
|
|
|
|
# are new to Rails.
|
2005-06-20 07:15:46 -04:00
|
|
|
#
|
2008-04-04 23:52:58 -04:00
|
|
|
# Ruby raises NoMethodError if you invoke a method on an object that does not
|
|
|
|
# respond to it:
|
|
|
|
#
|
|
|
|
# $ ruby -e nil.destroy
|
|
|
|
# -e:1: undefined method `destroy' for nil:NilClass (NoMethodError)
|
|
|
|
#
|
|
|
|
# With these extensions, if the method belongs to the public interface of the
|
|
|
|
# classes in NilClass::WHINERS the error message suggests which could be the
|
|
|
|
# actual intended class:
|
|
|
|
#
|
2010-02-06 11:21:05 -05:00
|
|
|
# $ rails runner nil.destroy
|
2008-04-04 23:52:58 -04:00
|
|
|
# ...
|
|
|
|
# You might have expected an instance of ActiveRecord::Base.
|
|
|
|
# ...
|
|
|
|
#
|
|
|
|
# NilClass#id exists in Ruby 1.8 (though it is deprecated). Since +id+ is a fundamental
|
|
|
|
# method of Active Record models NilClass#id is redefined as well to raise a RuntimeError
|
|
|
|
# and warn the user. She probably wanted a model database identifier and the 4
|
|
|
|
# returned by the original method could result in obscure bugs.
|
|
|
|
#
|
|
|
|
# The flag <tt>config.whiny_nils</tt> determines whether this feature is enabled.
|
|
|
|
# By default it is on in development and test modes, and it is off in production
|
|
|
|
# mode.
|
2005-06-20 07:15:46 -04:00
|
|
|
class NilClass
|
2008-05-04 22:11:03 -04:00
|
|
|
METHOD_CLASS_MAP = Hash.new
|
2007-05-23 03:03:31 -04:00
|
|
|
|
2010-03-07 09:24:30 -05:00
|
|
|
def self.add_whiner(klass)
|
2005-06-20 07:15:46 -04:00
|
|
|
methods = klass.public_instance_methods - public_instance_methods
|
2007-05-23 03:03:31 -04:00
|
|
|
class_name = klass.name
|
2008-05-04 22:11:03 -04:00
|
|
|
methods.each { |method| METHOD_CLASS_MAP[method.to_sym] = class_name }
|
2005-06-20 07:15:46 -04:00
|
|
|
end
|
2007-05-23 03:03:31 -04:00
|
|
|
|
2010-03-07 09:24:30 -05:00
|
|
|
add_whiner ::Array
|
|
|
|
|
2008-04-04 23:52:58 -04:00
|
|
|
# Raises a RuntimeError when you attempt to call +id+ on +nil+.
|
2005-06-21 12:36:40 -04:00
|
|
|
def id
|
2011-03-06 10:23:31 -05:00
|
|
|
raise RuntimeError, "Called id for nil, which would mistakenly be #{object_id} -- if you really wanted the id of nil, use object_id", caller
|
2005-06-21 12:36:40 -04:00
|
|
|
end
|
2005-06-20 07:15:46 -04:00
|
|
|
|
|
|
|
private
|
2011-04-29 14:15:19 -04:00
|
|
|
def method_missing(method, *args)
|
2009-12-25 02:48:35 -05:00
|
|
|
if klass = METHOD_CLASS_MAP[method]
|
2009-11-13 15:15:49 -05:00
|
|
|
raise_nil_warning_for klass, method, caller
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
2005-06-20 07:15:46 -04:00
|
|
|
end
|
|
|
|
|
2008-04-04 23:52:58 -04:00
|
|
|
# Raises a NoMethodError when you attempt to call a method on +nil+.
|
2007-05-23 03:03:31 -04:00
|
|
|
def raise_nil_warning_for(class_name = nil, selector = nil, with_caller = nil)
|
2005-10-14 22:24:05 -04:00
|
|
|
message = "You have a nil object when you didn't expect it!"
|
2007-05-23 03:03:31 -04:00
|
|
|
message << "\nYou might have expected an instance of #{class_name}." if class_name
|
2006-07-10 15:41:59 -04:00
|
|
|
message << "\nThe error occurred while evaluating nil.#{selector}" if selector
|
2007-05-23 03:03:31 -04:00
|
|
|
|
2005-10-14 22:24:05 -04:00
|
|
|
raise NoMethodError, message, with_caller || caller
|
2005-06-20 07:15:46 -04:00
|
|
|
end
|
|
|
|
end
|