diff options
author | José Valim <jose.valim@gmail.com> | 2011-12-12 19:23:13 +0100 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2011-12-12 19:41:17 +0100 |
commit | ede647a505fbba459d4e8529646b1f5cb59c1d43 (patch) | |
tree | 6a8eb6592645f86825b7f15dc7177ecf70d75ebb /actionpack/lib/action_dispatch/middleware | |
parent | 9a51053c1d0fe4db66f82c93454b8fd8e6d7e192 (diff) | |
download | rails-ede647a505fbba459d4e8529646b1f5cb59c1d43.tar.gz rails-ede647a505fbba459d4e8529646b1f5cb59c1d43.tar.bz2 rails-ede647a505fbba459d4e8529646b1f5cb59c1d43.zip |
Allow reloader to be configured.
Diffstat (limited to 'actionpack/lib/action_dispatch/middleware')
-rw-r--r-- | actionpack/lib/action_dispatch/middleware/reloader.rb | 52 |
1 files changed, 38 insertions, 14 deletions
diff --git a/actionpack/lib/action_dispatch/middleware/reloader.rb b/actionpack/lib/action_dispatch/middleware/reloader.rb index 29289a76b4..4f48f1c974 100644 --- a/actionpack/lib/action_dispatch/middleware/reloader.rb +++ b/actionpack/lib/action_dispatch/middleware/reloader.rb @@ -43,34 +43,58 @@ module ActionDispatch # Execute all prepare callbacks. def self.prepare! - new(nil).run_callbacks :prepare + new(nil).prepare! end # Execute all cleanup callbacks. def self.cleanup! - new(nil).run_callbacks :cleanup + new(nil).cleanup! end - def initialize(app) + def initialize(app, condition=nil) @app = app - end - - module CleanupOnClose - def close - super if defined?(super) - ensure - ActionDispatch::Reloader.cleanup! - end + @condition = condition || lambda { true } + @validated = true end def call(env) - run_callbacks :prepare + @validated = @condition.call + prepare! response = @app.call(env) - response[2].extend(CleanupOnClose) + response[2].extend(module_hook) response rescue Exception - run_callbacks :cleanup + cleanup! raise end + + def prepare! #:nodoc: + run_callbacks :prepare if validated? + end + + def cleanup! #:nodoc: + run_callbacks :cleanup if validated? + ensure + @validated = true + end + + private + + def validated? #:nodoc: + @validated + end + + def module_hook #:nodoc: + middleware = self + Module.new do + define_method :close do + begin + super() if defined?(super) + ensure + middleware.cleanup! + end + end + end + end end end |