diff options
author | Nicholas Seckar <nseckar@gmail.com> | 2006-11-07 20:45:36 +0000 |
---|---|---|
committer | Nicholas Seckar <nseckar@gmail.com> | 2006-11-07 20:45:36 +0000 |
commit | ad06514257e4472a986577ba7df66d9b8db8a2c2 (patch) | |
tree | be644b4178b66a967de93a59cdb2e38323acd366 | |
parent | e1140f876ef2f2760753f681564cb025292c86f0 (diff) | |
download | rails-ad06514257e4472a986577ba7df66d9b8db8a2c2.tar.gz rails-ad06514257e4472a986577ba7df66d9b8db8a2c2.tar.bz2 rails-ad06514257e4472a986577ba7df66d9b8db8a2c2.zip |
Update dependencies to delete partially loaded constants.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5464 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r-- | activesupport/CHANGELOG | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/dependencies.rb | 53 | ||||
-rw-r--r-- | activesupport/test/dependencies_test.rb | 7 |
3 files changed, 41 insertions, 21 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index e265345b35..8c5443de74 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Update dependencies to delete partially loaded constants. [Nicholas Seckar] + * Fix unicode JSON regexp for Onigurama compatibility. #6494 [whitley] * update XmlSimple to 1.0.10. Closes #6532. [nicksieger] diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb index 270496ab15..ad4e4da669 100644 --- a/activesupport/lib/active_support/dependencies.rb +++ b/activesupport/lib/active_support/dependencies.rb @@ -234,7 +234,7 @@ module Dependencies #:nodoc: raise ArgumentError, "A copy of #{from_mod} has been removed from the module tree but is still active!" end - raise ArgumentError, "Expected #{from_mod} is not missing constant #{const_name}!" if from_mod.const_defined?(const_name) + raise ArgumentError, "#{from_mod} is not missing constant #{const_name}!" if from_mod.const_defined?(const_name) qualified_name = qualified_name_for from_mod, const_name path_suffix = qualified_name.underscore @@ -301,6 +301,10 @@ module Dependencies #:nodoc: # its execution. Constants may only be regarded as 'new' once -- so if the # block calls +new_constants_in+ again, then the constants defined within the # inner call will not be reported in this one. + # + # If the provided block does not run to completion, and instead raises an + # exception, any new constants are regarded as being only partially defined + # and will be removed immediately. def new_constants_in(*descs) log_call(*descs) @@ -328,28 +332,39 @@ module Dependencies #:nodoc: constant_watch_stack.concat watch_frames - yield # Now yield to the code that is to define new constants. - - # Find the new constants. - new_constants = watch_frames.collect do |mod_name, prior_constants| - # Module still doesn't exist? Treat it as if it has no constants. - next [] unless qualified_const_defined?(mod_name) - - mod = mod_name.constantize - next [] unless mod.is_a? Module - new_constants = mod.constants - prior_constants + aborting = true + begin + yield # Now yield to the code that is to define new constants. + aborting = false + ensure + # Find the new constants. + new_constants = watch_frames.collect do |mod_name, prior_constants| + # Module still doesn't exist? Treat it as if it has no constants. + next [] unless qualified_const_defined?(mod_name) + + mod = mod_name.constantize + next [] unless mod.is_a? Module + new_constants = mod.constants - prior_constants + + # Make sure no other frames takes credit for these constants. + constant_watch_stack.each do |frame_name, constants| + constants.concat new_constants if frame_name == mod_name + end + + new_constants.collect do |suffix| + mod_name == "Object" ? suffix : "#{mod_name}::#{suffix}" + end + end.flatten - # Make sure no other frames takes credit for these constants. - constant_watch_stack.each do |frame_name, constants| - constants.concat new_constants if frame_name == mod_name - end + log "New constants: #{new_constants * ', '}" - new_constants.collect do |suffix| - mod_name == "Object" ? suffix : "#{mod_name}::#{suffix}" + if aborting + log "Error during loading, removing partially loaded constants " + new_constants.each { |name| remove_constant name } + new_constants.clear end - end.flatten + end - log "New constants: #{new_constants * ', '}" return new_constants ensure # Remove the stack frames that we added. diff --git a/activesupport/test/dependencies_test.rb b/activesupport/test/dependencies_test.rb index 813574431e..ec47eb08be 100644 --- a/activesupport/test/dependencies_test.rb +++ b/activesupport/test/dependencies_test.rb @@ -641,7 +641,8 @@ class DependenciesTest < Test::Unit::TestCase assert !defined?(::RaisesNoMethodError), "::RaisesNoMethodError is defined but it should have failed!" end end - + + ensure Object.send(:remove_const, :RaisesNoMethodError) if defined?(::RaisesNoMethodError) end @@ -653,7 +654,8 @@ class DependenciesTest < Test::Unit::TestCase assert !defined?(::RaisesNoMethodError), "::RaisesNoMethodError is defined but it should have failed!" end end - + + ensure Object.send(:remove_const, :RaisesNoMethodError) if defined?(::RaisesNoMethodError) end @@ -677,6 +679,7 @@ class DependenciesTest < Test::Unit::TestCase end end + ensure Object.send(:remove_const, :RaisesNameError) if defined?(::RaisesNameError) end end |