Class: ActionDispatch::Reloader

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks
Defined in:
actionpack/lib/action_dispatch/middleware/reloader.rb

Overview

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 request is fully complete – that is, after #close has been called on the response body. This is important for streaming responses such as the 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 only in the development environment; specifically, when config.cache_classes is false. Callbacks may be registered even when it is not included in the middleware stack, but are executed only when ActionDispatch::Reloader.prepare! or ActionDispatch::Reloader.cleanup! are called manually.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActiveSupport::Callbacks

#run_callbacks

Methods included from ActiveSupport::Concern

#append_features, extended, #included

Constructor Details

#initialize(app, condition = nil) ⇒ Reloader

Returns a new instance of Reloader.



56
57
58
59
60
# File 'actionpack/lib/action_dispatch/middleware/reloader.rb', line 56

def initialize(app, condition=nil)
  @app = app
  @condition = condition || lambda { true }
  @validated = true
end

Class Method Details

.cleanup!Object

Execute all cleanup callbacks.



52
53
54
# File 'actionpack/lib/action_dispatch/middleware/reloader.rb', line 52

def self.cleanup!
  new(nil).cleanup!
end

.prepare!Object

Execute all prepare callbacks.



47
48
49
# File 'actionpack/lib/action_dispatch/middleware/reloader.rb', line 47

def self.prepare!
  new(nil).prepare!
end

.to_cleanup(*args, &block) ⇒ Object

Add a cleanup callback. Cleanup callbacks are run after each request is complete (after #close is called on the response body).



42
43
44
# File 'actionpack/lib/action_dispatch/middleware/reloader.rb', line 42

def self.to_cleanup(*args, &block)
  set_callback(:cleanup, *args, &block)
end

.to_prepare(*args, &block) ⇒ Object

Add a prepare callback. Prepare callbacks are run before each request, prior to ActionDispatch::Callback’s before callbacks.



36
37
38
# File 'actionpack/lib/action_dispatch/middleware/reloader.rb', line 36

def self.to_prepare(*args, &block)
  set_callback(:prepare, *args, &block)
end

Instance Method Details

#call(env) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'actionpack/lib/action_dispatch/middleware/reloader.rb', line 62

def call(env)
  @validated = @condition.call
  prepare!
  response = @app.call(env)
  response[2] = ActionDispatch::BodyProxy.new(response[2]) { cleanup! }
  response
rescue Exception
  cleanup!
  raise
end

#cleanup!Object

:nodoc:



77
78
79
80
81
# File 'actionpack/lib/action_dispatch/middleware/reloader.rb', line 77

def cleanup! #:nodoc:
  run_callbacks :cleanup if validated?
ensure
  @validated = true
end

#prepare!Object

:nodoc:



73
74
75
# File 'actionpack/lib/action_dispatch/middleware/reloader.rb', line 73

def prepare! #:nodoc:
  run_callbacks :prepare if validated?
end