Class: Dispatcher
Overview
This class provides an interface for dispatching a CGI (or CGI-like) request to the appropriate controller and action. It also takes care of resetting the environment (when Dependencies.load? is true) after each request.
Class Method Summary collapse
-
.dispatch(cgi = nil, session_options = ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS, output = $stdout) ⇒ Object
Dispatch the given CGI request, using the given session options, and emitting the output via the given output.
-
.reset_application! ⇒ Object
Reset the application by clearing out loaded controllers, views, actions, mailers, and so forth.
-
.to_prepare(identifier = nil, &block) ⇒ Object
Add a preparation callback.
Class Method Details
.dispatch(cgi = nil, session_options = ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS, output = $stdout) ⇒ Object
Dispatch the given CGI request, using the given session options, and emitting the output via the given output. If you dispatch with your own CGI object be sure to handle the exceptions it raises on multipart requests (EOFError and ArgumentError).
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/dispatcher.rb', line 35 def dispatch(cgi = nil, = ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS, output = $stdout) controller = nil if cgi ||= new_cgi(output) request, response = ActionController::CgiRequest.new(cgi, ), ActionController::CgiResponse.new(cgi) prepare_application controller = ActionController::Routing::Routes.recognize(request) controller.process(request, response).out(output) end rescue Exception => exception # errors from CGI dispatch failsafe_response(output, '500 Internal Server Error', exception) do controller ||= (ApplicationController rescue ActionController::Base) controller.process_with_exception(request, response, exception).out(output) end ensure # Do not give a failsafe response here. reset_after_dispatch end |
.reset_application! ⇒ Object
Reset the application by clearing out loaded controllers, views, actions, mailers, and so forth. This allows them to be loaded again without having to restart the server (WEBrick, FastCGI, etc.).
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/dispatcher.rb', line 56 def reset_application! ActiveRecord::Base.reset_subclasses if defined?(ActiveRecord) Dependencies.clear ActiveSupport::Deprecation.silence do # TODO: Remove after 1.2 Class.remove_class(*Reloadable.reloadable_classes) end ActiveRecord::Base.clear_reloadable_connections! if defined?(ActiveRecord) end |
.to_prepare(identifier = nil, &block) ⇒ Object
Add a preparation callback. Preparation callbacks are run before every request in development mode, and before the first request in production mode.
An optional identifier may be supplied for the callback. If provided, to_prepare may be called again with the same identifier to replace the existing callback. Passing an identifier is a suggested practice if the code adding a preparation block may be reloaded.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/dispatcher.rb', line 75 def to_prepare(identifier = nil, &block) unless identifier.nil? callback = preparation_callbacks.detect { |ident, _| ident == identifier } if callback # Already registered: update the existing callback callback[-1] = block return end end preparation_callbacks << [identifier, block] return end |