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

Changeset 5464

Show
Ignore:
Timestamp:
11/07/06 20:45:36 (2 years ago)
Author:
ulysses
Message:

Update dependencies to delete partially loaded constants.

Files:

Legend:

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

    r5432 r5464  
    11*SVN* 
     2 
     3* Update dependencies to delete partially loaded constants. [Nicholas Seckar] 
    24 
    35* Fix unicode JSON regexp for Onigurama compatibility.  #6494 [whitley] 
  • trunk/activesupport/lib/active_support/dependencies.rb

    r5386 r5464  
    235235    end 
    236236     
    237     raise ArgumentError, "Expected #{from_mod} is not missing constant #{const_name}!" if from_mod.const_defined?(const_name) 
     237    raise ArgumentError, "#{from_mod} is not missing constant #{const_name}!" if from_mod.const_defined?(const_name) 
    238238     
    239239    qualified_name = qualified_name_for from_mod, const_name 
     
    302302  # block calls +new_constants_in+ again, then the constants defined within the 
    303303  # inner call will not be reported in this one. 
     304  #  
     305  # If the provided block does not run to completion, and instead raises an 
     306  # exception, any new constants are regarded as being only partially defined 
     307  # and will be removed immediately. 
    304308  def new_constants_in(*descs) 
    305309    log_call(*descs) 
     
    329333    constant_watch_stack.concat watch_frames 
    330334     
    331     yield # Now yield to the code that is to define new constants. 
    332      
    333     # Find the new constants. 
    334     new_constants = watch_frames.collect do |mod_name, prior_constants| 
    335       # Module still doesn't exist? Treat it as if it has no constants. 
    336       next [] unless qualified_const_defined?(mod_name) 
     335    aborting = true 
     336    begin 
     337      yield # Now yield to the code that is to define new constants. 
     338      aborting = false 
     339    ensure 
     340      # Find the new constants. 
     341      new_constants = watch_frames.collect do |mod_name, prior_constants| 
     342        # Module still doesn't exist? Treat it as if it has no constants. 
     343        next [] unless qualified_const_defined?(mod_name) 
     344         
     345        mod = mod_name.constantize 
     346        next [] unless mod.is_a? Module 
     347        new_constants = mod.constants - prior_constants 
     348         
     349        # Make sure no other frames takes credit for these constants. 
     350        constant_watch_stack.each do |frame_name, constants| 
     351          constants.concat new_constants if frame_name == mod_name 
     352        end 
     353         
     354        new_constants.collect do |suffix| 
     355          mod_name == "Object" ? suffix : "#{mod_name}::#{suffix}" 
     356        end 
     357      end.flatten 
    337358       
    338       mod = mod_name.constantize 
    339       next [] unless mod.is_a? Module 
    340       new_constants = mod.constants - prior_constants 
     359      log "New constants: #{new_constants * ', '}" 
    341360       
    342       # Make sure no other frames takes credit for these constants. 
    343       constant_watch_stack.each do |frame_name, constants| 
    344         constants.concat new_constants if frame_name == mod_name 
    345       end 
    346        
    347       new_constants.collect do |suffix| 
    348         mod_name == "Object" ? suffix : "#{mod_name}::#{suffix}" 
    349       end 
    350     end.flatten 
    351      
    352     log "New constants: #{new_constants * ', '}" 
     361      if aborting 
     362        log "Error during loading, removing partially loaded constants " 
     363        new_constants.each { |name| remove_constant name } 
     364        new_constants.clear 
     365      end 
     366    end 
     367     
    353368    return new_constants 
    354369  ensure 
  • trunk/activesupport/test/dependencies_test.rb

    r5463 r5464  
    642642      end 
    643643    end 
    644  
     644   
     645  ensure 
    645646    Object.send(:remove_const, :RaisesNoMethodError) if defined?(::RaisesNoMethodError) 
    646647  end 
     
    654655      end 
    655656    end 
    656  
     657   
     658  ensure 
    657659    Object.send(:remove_const, :RaisesNoMethodError) if defined?(::RaisesNoMethodError) 
    658660  end 
     
    678680    end 
    679681 
     682  ensure 
    680683    Object.send(:remove_const, :RaisesNameError) if defined?(::RaisesNameError) 
    681684  end