reloader.rb 3.0 KB
Newer Older
1 2
require 'active_support/deprecation/reporting'

3
module ActionDispatch
4 5 6 7 8 9
  # 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
10
  # request is fully complete -- that is, after #close has been called on
J
John Firebaugh 已提交
11
  # the response body. This is important for streaming responses such as the
12 13
  # following:
  #
14
  #     self.response_body = -> (response, output) do
15
  #       # code here which refers to application models
16
  #     end
17 18 19 20 21 22
  #
  # 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
23
  # only in the development environment; specifically, when +config.cache_classes+
24
  # is false. Callbacks may be registered even when it is not included in the
25 26
  # middleware stack, but are executed only when <tt>ActionDispatch::Reloader.prepare!</tt>
  # or <tt>ActionDispatch::Reloader.cleanup!</tt> are called manually.
27 28 29
  #
  class Reloader
    include ActiveSupport::Callbacks
30
    include ActiveSupport::Deprecation::Reporting
31

A
Aaron Patterson 已提交
32 33
    define_callbacks :prepare
    define_callbacks :cleanup
34

35 36
    # Add a prepare callback. Prepare callbacks are run before each request, prior
    # to ActionDispatch::Callback's before callbacks.
37
    def self.to_prepare(*args, &block)
38 39 40
      unless block_given?
        warn "to_prepare without a block is deprecated. Please use a block"
      end
J
José Valim 已提交
41
      set_callback(:prepare, *args, &block)
42 43 44 45
    end

    # Add a cleanup callback. Cleanup callbacks are run after each request is
    # complete (after #close is called on the response body).
J
José Valim 已提交
46
    def self.to_cleanup(*args, &block)
47 48 49
      unless block_given?
        warn "to_cleanup without a block is deprecated. Please use a block"
      end
J
José Valim 已提交
50
      set_callback(:cleanup, *args, &block)
51 52
    end

53
    # Execute all prepare callbacks.
54
    def self.prepare!
J
José Valim 已提交
55
      new(nil).prepare!
56 57
    end

58
    # Execute all cleanup callbacks.
59
    def self.cleanup!
J
José Valim 已提交
60
      new(nil).cleanup!
61 62
    end

J
José Valim 已提交
63
    def initialize(app, condition=nil)
64
      @app = app
J
José Valim 已提交
65 66
      @condition = condition || lambda { true }
      @validated = true
67 68 69
    end

    def call(env)
J
José Valim 已提交
70 71
      @validated = @condition.call
      prepare!
72

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

76
      response
77
    rescue Exception
J
José Valim 已提交
78
      cleanup!
79
      raise
80
    end
J
José Valim 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

    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
97 98
  end
end