Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 2602

Show
Ignore:
Timestamp:
10/15/05 02:24:05 (3 years ago)
Author:
ulysses
Message:

Updated whiny nil to be more concise and useful.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/CHANGELOG

    r2578 r2602  
    11*SVN* 
     2 
     3* Updated whiny nil to be more concise and useful. [Nicholas Seckar] 
    24 
    35* Added Enumerable#first_match [Nicholas Seckar] 
  • trunk/activesupport/lib/active_support/whiny_nil.rb

    r1660 r2602  
    2525  private 
    2626    def method_missing(method, *args, &block) 
    27       if @@method_class_map.include?(method) 
    28         raise_nil_warning_for @@method_class_map[method], caller 
    29       else 
    30         super 
    31       end 
     27      raise_nil_warning_for @@method_class_map[method], method, caller 
    3228    end 
    3329 
    34     def raise_nil_warning_for(klass, with_caller = nil) 
    35       raise NoMethodError, NIL_WARNING_MESSAGE % klass, with_caller || caller 
     30    def raise_nil_warning_for(klass = nil, selector = nil, with_caller = nil) 
     31      message = "You have a nil object when you didn't expect it!" 
     32      message << "\nYou might have expected an instance of #{klass}." if klass 
     33      message << "\nThe error occured while evaluating nil.#{selector}" if selector 
     34       
     35      raise NoMethodError, message, with_caller || caller 
    3636    end 
    37  
    38     NIL_WARNING_MESSAGE = <<-end_message unless const_defined?(:NIL_WARNING_MESSAGE) 
    39 WARNING:  You have a nil object when you probably didn't expect it!  Odds are you 
    40 want an instance of %s instead. 
    41  
    42 Look in the callstack to see where you're working with an object that could be nil. 
    43 Investigate your methods and make sure the object is what you expect! 
    44     end_message 
    4537end 
    4638 
  • trunk/activesupport/test/whiny_nil_test.rb

    r1507 r2602  
    1616    nil.method_thats_not_in_whiners 
    1717  rescue NoMethodError => nme 
    18     assert_match(/nil:NilClass/, nme.message) 
     18    assert_match(/nil.method_thats_not_in_whiners/, nme.message) 
    1919  end 
    2020   
     
    2323  rescue NoMethodError => nme 
    2424    assert(!(nme.message =~ /nil:NilClass/)) 
     25    assert_match(/nil\.save!/, nme.message) 
    2526  end 
    2627   
     
    2930  rescue NoMethodError => nme 
    3031    assert(!(nme.message =~ /nil:NilClass/)) 
     32    assert_match(/nil\.each/, nme.message) 
    3133  end 
    3234