reloader.rb 2.7 KB
Newer Older
1
module ActionDispatch
2 3 4 5 6 7
  # 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
8
  # request is fully complete -- that is, after #close has been called on
J
John Firebaugh 已提交
9
  # the response body. This is important for streaming responses such as the
10 11 12 13 14 15 16 17 18 19 20
  # 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
21 22 23 24
  # 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.
25 26 27 28 29 30 31
  #
  class Reloader
    include ActiveSupport::Callbacks

    define_callbacks :prepare, :scope => :name
    define_callbacks :cleanup, :scope => :name

32 33
    # Add a prepare callback. Prepare callbacks are run before each request, prior
    # to ActionDispatch::Callback's before callbacks.
34
    def self.to_prepare(*args, &block)
J
José Valim 已提交
35
      set_callback(:prepare, *args, &block)
36 37 38 39
    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 已提交
40 41
    def self.to_cleanup(*args, &block)
      set_callback(:cleanup, *args, &block)
42 43
    end

44
    # Execute all prepare callbacks.
45
    def self.prepare!
J
José Valim 已提交
46
      new(nil).prepare!
47 48
    end

49
    # Execute all cleanup callbacks.
50
    def self.cleanup!
J
José Valim 已提交
51
      new(nil).cleanup!
52 53
    end

J
José Valim 已提交
54
    def initialize(app, condition=nil)
55
      @app = app
J
José Valim 已提交
56 57
      @condition = condition || lambda { true }
      @validated = true
58 59 60
    end

    def call(env)
J
José Valim 已提交
61 62
      @validated = @condition.call
      prepare!
63

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

67
      response
68
    rescue Exception
J
José Valim 已提交
69
      cleanup!
70
      raise
71
    end
J
José Valim 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

    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
88 89
  end
end