123456789_123456789_123456789_123456789_123456789_

Class: ActionDispatch::Reloader

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: Object
Defined in: actionpack/lib/action_dispatch/middleware/reloader.rb

Overview

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, 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 .prepare! or .cleanup! are called manually.

Constant Summary

::ActiveSupport::Callbacks - Included

CALLBACK_FILTER_TYPES

Class Method Summary

::ActiveSupport::DescendantsTracker - self

clear, descendants, direct_descendants,
store_inherited

This is the only method that is not thread safe, but is only ever called during the eager loading phase.

Instance Attribute Summary

::ActiveSupport::Deprecation::Reporting - Included

#gem_name

Name of gem where method is deprecated.

#silenced

Whether to print a message (silent mode).

Instance Method Summary

::ActiveSupport::Deprecation::Reporting - Included

#deprecation_warning,
#silence

Silence deprecation warnings within the block.

#warn

Outputs a deprecation warning to the output configured by ActiveSupport::Deprecation.behavior.

::ActiveSupport::Callbacks - Included

#run_callbacks

Runs the callbacks for the given event.

Constructor Details

.new(app, condition = nil) ⇒ Reloader

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/middleware/reloader.rb', line 63

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

Class Method Details

.cleanup!

Execute all cleanup callbacks.

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/middleware/reloader.rb', line 59

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

.prepare!

Execute all prepare callbacks.

[ GitHub ]

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

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

.to_cleanup(*args, &block)

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

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/middleware/reloader.rb', line 46

def self.to_cleanup(*args, &block)
  unless block_given?
    warn "to_cleanup without a block is deprecated. Please use a block"
  end
  set_callback(:cleanup, *args, &block)
end

.to_prepare(*args, &block)

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

[ GitHub ]

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

def self.to_prepare(*args, &block)
  unless block_given?
    warn "to_prepare without a block is deprecated. Please use a block"
  end
  set_callback(:prepare, *args, &block)
end

Instance Method Details

#call(env)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/middleware/reloader.rb', line 69

def call(env)
  @validated = @condition.call
  prepare!

  response = @app.call(env)
  response[2] = ::Rack::BodyProxy.new(response[2]) { cleanup! }

  response
rescue Exception
  cleanup!
  raise
end