2010-11-23 21:04:05 -05:00
|
|
|
module ActionDispatch
|
2010-12-20 17:55:21 -05:00
|
|
|
# ActionDispatch::Reloader provides prepare and cleanup callbacks,
|
|
|
|
# intended to assist with code reloading during development.
|
|
|
|
#
|
|
|
|
# Prepare callbacks are run before each request, and cleanup callbacks
|
|
|
|
# after each request. In this respect they are analogs of ActionDispatch::Callback's
|
|
|
|
# before and after callbacks. However, cleanup callbacks are not called until the
|
2010-11-23 21:04:05 -05:00
|
|
|
# request is fully complete -- that is, after #close has been called on
|
2010-12-21 22:32:34 -05:00
|
|
|
# the response body. This is important for streaming responses such as the
|
2010-11-23 21:04:05 -05:00
|
|
|
# following:
|
|
|
|
#
|
|
|
|
# self.response_body = lambda { |response, output|
|
|
|
|
# # code here which refers to application models
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# Cleanup callbacks will not be called until after the response_body lambda
|
|
|
|
# is evaluated, ensuring that it can refer to application models and other
|
|
|
|
# classes before they are unloaded.
|
|
|
|
#
|
|
|
|
# By default, ActionDispatch::Reloader is included in the middleware stack
|
2012-04-28 01:46:45 -04:00
|
|
|
# only in the development environment; specifically, when +config.cache_classes+
|
2010-12-20 17:55:21 -05:00
|
|
|
# is false. Callbacks may be registered even when it is not included in the
|
2012-04-28 00:33:56 -04:00
|
|
|
# middleware stack, but are executed only when <tt>ActionDispatch::Reloader.prepare!</tt>
|
|
|
|
# or <tt>ActionDispatch::Reloader.cleanup!</tt> are called manually.
|
2010-11-23 21:04:05 -05:00
|
|
|
#
|
|
|
|
class Reloader
|
|
|
|
include ActiveSupport::Callbacks
|
|
|
|
|
|
|
|
define_callbacks :prepare, :scope => :name
|
|
|
|
define_callbacks :cleanup, :scope => :name
|
|
|
|
|
2010-12-20 17:55:21 -05:00
|
|
|
# Add a prepare callback. Prepare callbacks are run before each request, prior
|
|
|
|
# to ActionDispatch::Callback's before callbacks.
|
2010-11-23 21:04:05 -05:00
|
|
|
def self.to_prepare(*args, &block)
|
2010-12-20 06:41:49 -05:00
|
|
|
set_callback(:prepare, *args, &block)
|
2010-11-23 21:04:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Add a cleanup callback. Cleanup callbacks are run after each request is
|
|
|
|
# complete (after #close is called on the response body).
|
2010-12-20 06:41:49 -05:00
|
|
|
def self.to_cleanup(*args, &block)
|
|
|
|
set_callback(:cleanup, *args, &block)
|
2010-11-23 21:04:05 -05:00
|
|
|
end
|
|
|
|
|
2010-12-20 17:55:21 -05:00
|
|
|
# Execute all prepare callbacks.
|
2010-11-23 21:04:05 -05:00
|
|
|
def self.prepare!
|
2011-12-12 13:23:13 -05:00
|
|
|
new(nil).prepare!
|
2010-11-23 21:04:05 -05:00
|
|
|
end
|
|
|
|
|
2010-12-20 17:55:21 -05:00
|
|
|
# Execute all cleanup callbacks.
|
2010-11-23 21:04:05 -05:00
|
|
|
def self.cleanup!
|
2011-12-12 13:23:13 -05:00
|
|
|
new(nil).cleanup!
|
2010-11-23 21:04:05 -05:00
|
|
|
end
|
|
|
|
|
2011-12-12 13:23:13 -05:00
|
|
|
def initialize(app, condition=nil)
|
2010-11-23 21:04:05 -05:00
|
|
|
@app = app
|
2011-12-12 13:23:13 -05:00
|
|
|
@condition = condition || lambda { true }
|
|
|
|
@validated = true
|
2010-11-23 21:04:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
2011-12-12 13:23:13 -05:00
|
|
|
@validated = @condition.call
|
|
|
|
prepare!
|
2012-01-16 06:35:44 -05:00
|
|
|
|
2010-11-23 21:04:05 -05:00
|
|
|
response = @app.call(env)
|
2012-01-16 06:35:44 -05:00
|
|
|
response[2] = ::Rack::BodyProxy.new(response[2]) { cleanup! }
|
|
|
|
|
2010-11-23 21:04:05 -05:00
|
|
|
response
|
2010-12-23 13:17:02 -05:00
|
|
|
rescue Exception
|
2011-12-12 13:23:13 -05:00
|
|
|
cleanup!
|
2010-12-23 13:17:02 -05:00
|
|
|
raise
|
2010-11-23 21:04:05 -05:00
|
|
|
end
|
2011-12-12 13:23:13 -05:00
|
|
|
|
|
|
|
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
|
2010-11-23 21:04:05 -05:00
|
|
|
end
|
|
|
|
end
|