Add before_remove_const callback to ActiveSupport::Dependencies.remove_unloadable_constants!

Signed-off-by: Xavier Noria <fxn@hashref.com>
This commit is contained in:
Andrew White
2010-09-01 14:12:06 +01:00
committed by Xavier Noria
parent 2ce57fd0d9
commit f1966337fa
3 changed files with 19 additions and 2 deletions

View File

@@ -1,6 +1,6 @@
*Rails 3.1.0 (unreleased)*
* No changes
* Added before_remove_const callback to ActiveSupport::Dependencies.remove_unloadable_constants! [Andrew White]
*Rails 3.0.0 (August 29, 2010)*

View File

@@ -511,7 +511,12 @@ module ActiveSupport #:nodoc:
end
# Remove the constants that have been autoloaded, and those that have been
# marked for unloading.
# marked for unloading. Before each constant is removed a callback is sent
# to its class/module if it implements +before_remove_const+.
#
# The callback implementation should be restricted to cleaning up caches, etc.
# as the enviroment will be in an inconsistent state, e.g. other constants
# may have already been unloaded and not accessible.
def remove_unloadable_constants!
autoloaded_constants.each { |const| remove_constant const }
autoloaded_constants.clear
@@ -636,6 +641,7 @@ module ActiveSupport #:nodoc:
parent = Inflector.constantize(names * '::')
log "removing constant #{const}"
constantize(const).before_remove_const if constantize(const).respond_to?(:before_remove_const)
parent.instance_eval { remove_const to_remove }
return true

View File

@@ -574,6 +574,17 @@ class DependenciesTest < Test::Unit::TestCase
end
end
def test_unloadable_constants_should_receive_callback
Object.const_set :C, Class.new
C.unloadable
C.expects(:before_remove_const).once
assert C.respond_to?(:before_remove_const)
ActiveSupport::Dependencies.clear
assert !defined?(C)
ensure
Object.class_eval { remove_const :C } if defined?(C)
end
def test_new_contants_in_without_constants
assert_equal [], (ActiveSupport::Dependencies.new_constants_in(Object) { })
assert ActiveSupport::Dependencies.constant_watch_stack.all? {|k,v| v.empty? }