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

Changeset 4681

Show
Ignore:
Timestamp:
08/05/06 22:52:15 (2 years ago)
Author:
ulysses
Message:

Raise fully qualified names upon name errors. Closes #5533.

Files:

Legend:

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

    r4679 r4681  
    11*SVN* 
     2 
     3* Raise fully qualified names upon name errors. #5533 [lars@pinds.com, Nicholas Seckar] 
    24 
    35* Add extention to obtain the missing constant from NameError instances. [Nicholas Seckar] 
  • trunk/activesupport/lib/active_support/core_ext/name_error.rb

    r4679 r4681  
    1010  # Was this exception raised because the given name was missing? 
    1111  def missing_name?(name) 
    12     missing_name == name.to_s 
     12    if name.is_a? Symbol 
     13      last_name = (missing_name || '').split('::').last 
     14      last_name == name.to_s 
     15    else 
     16      missing_name == name.to_s 
     17    end 
    1318  end 
    1419   
  • trunk/activesupport/lib/active_support/dependencies.rb

    r4604 r4681  
    7070  end 
    7171   
     72  # Return the a constant path for the provided parent and constant name 
     73  def constant_path_for(mod, name) 
     74    ([Object, Kernel].include? mod) ? name.to_s : "#{mod}::#{name}" 
     75  end 
     76   
    7277  class LoadingModule 
    7378    # Old style environment.rb referenced this method directly.  Please note, it doesn't 
     
    121126      end 
    122127       
    123       raise NameError.new("uninitialized constant #{class_id}").copy_blame!(e) 
     128      qualified_name = Dependencies.constant_path_for self, class_id 
     129      raise NameError.new("uninitialized constant #{qualified_name}").copy_blame!(e) 
    124130    end 
    125131  end 
     
    131137      super 
    132138    else 
    133       parent.send :const_missing, class_id 
     139      begin 
     140        parent.send :const_missing, class_id 
     141      rescue NameError => e 
     142        # Make sure that the name we are missing is the one that caused the error 
     143        parent_qualified_name = Dependencies.constant_path_for parent, class_id 
     144        raise unless e.missing_name? parent_qualified_name 
     145        qualified_name = Dependencies.constant_path_for self, class_id 
     146        raise NameError.new("uninitialized constant #{qualified_name}").copy_blame!(e) 
     147      end 
    134148    end 
    135149  end 
  • trunk/activesupport/test/core_ext/name_error_test.rb

    r4679 r4681  
    99      flunk "?!?!" 
    1010    rescue NameError => exc 
    11       assert_equal "SomeNameThatNobodyWillUse____Really", exc.missing_name 
     11      assert_equal "NameErrorTest::SomeNameThatNobodyWillUse____Really", exc.missing_name 
    1212      assert exc.missing_name?(:SomeNameThatNobodyWillUse____Really) 
    13       assert exc.missing_name?("SomeNameThatNobodyWillUse____Really") 
     13      assert exc.missing_name?("NameErrorTest::SomeNameThatNobodyWillUse____Really") 
    1414    end 
    1515  end 
  • trunk/activesupport/test/dependencies_test.rb

    r4595 r4681  
    156156    end 
    157157  end 
     158   
     159  def test_non_existing_const_raises_name_error_with_fully_qualified_name 
     160    with_loading 'autoloading_fixtures' do 
     161      begin 
     162        A::DoesNotExist 
     163        flunk "No raise!!" 
     164      rescue NameError => e 
     165        assert_equal "uninitialized constant A::DoesNotExist", e.message 
     166      end 
     167      begin 
     168        A::B::DoesNotExist 
     169        flunk "No raise!!" 
     170      rescue NameError => e 
     171        assert_equal "uninitialized constant A::B::DoesNotExist", e.message 
     172      end 
     173    end 
     174  end 
     175   
     176  def test_smart_name_error_strings 
     177    begin 
     178      Object.module_eval "ImaginaryObject" 
     179      flunk "No raise!!" 
     180    rescue NameError => e 
     181      assert e.message.include?("uninitialized constant ImaginaryObject") 
     182    end 
     183  end 
     184   
    158185end