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

Changeset 4779

Show
Ignore:
Timestamp:
08/16/06 20:54:25 (2 years ago)
Author:
ulysses
Message:

Fix const_missing to behave responsibly when called within anonymous modules

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/lib/active_support/dependencies.rb

    r4778 r4779  
    6666    # infinite loop with mutual dependencies. 
    6767    loaded << expanded 
    68  
     68     
    6969    if load? 
    7070      log "loading #{file_name}" 
     
    179179  def load_missing_constant(from_mod, const_name) 
    180180    log_call from_mod, const_name 
     181     
     182    # If we have an anonymous module, all we can do is attempt to load from Object. 
     183    from_mod = Object if from_mod.name.empty? 
     184     
     185    raise ArgumentError, "Expected #{from_mod} is not missing constant #{const_name}!" if from_mod.const_defined?(const_name) 
    181186     
    182187    qualified_name = qualified_name_for from_mod, const_name 
  • trunk/activesupport/test/dependencies_test.rb

    r4778 r4779  
    347347   
    348348  def test_const_missing_should_not_double_load 
     349    $counting_loaded_times = 0 
    349350    with_loading 'autoloading_fixtures' do 
    350351      require_dependency '././counting_loader' 
    351352      assert_equal 1, $counting_loaded_times 
    352       Dependencies.load_missing_constant Object, :CountingLoader 
     353      assert_raises(ArgumentError) { Dependencies.load_missing_constant Object, :CountingLoader } 
    353354      assert_equal 1, $counting_loaded_times 
    354355    end 
    355356  end 
    356357   
     358  def test_const_missing_within_anonymous_module 
     359    $counting_loaded_times = 0 
     360    m = Module.new 
     361    m.module_eval "def a() CountingLoader; end" 
     362    extend m 
     363    kls = nil     
     364    with_loading 'autoloading_fixtures' do 
     365      kls = nil 
     366      assert_nothing_raised { kls = a } 
     367      assert_equal "CountingLoader", kls.name 
     368      assert_equal 1, $counting_loaded_times 
     369       
     370      assert_nothing_raised { kls = a } 
     371      assert_equal 1, $counting_loaded_times 
     372    end 
     373  end 
     374   
    357375end