~ubuntu-branches/ubuntu/raring/ruby-actionpack-3.2/raring

« back to all changes in this revision

Viewing changes to lib/action_dispatch/middleware/reloader.rb

  • Committer: Package Import Robot
  • Author(s): Ondřej Surý
  • Date: 2012-04-25 09:14:01 UTC
  • Revision ID: package-import@ubuntu.com-20120425091401-3nkf83btcemhjquo
Tags: upstream-3.2.3
Import upstream version 3.2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'action_dispatch/middleware/body_proxy'
 
2
 
 
3
module ActionDispatch
 
4
  # ActionDispatch::Reloader provides prepare and cleanup callbacks,
 
5
  # intended to assist with code reloading during development.
 
6
  #
 
7
  # Prepare callbacks are run before each request, and cleanup callbacks
 
8
  # after each request. In this respect they are analogs of ActionDispatch::Callback's
 
9
  # 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
 
11
  # the response body. This is important for streaming responses such as the
 
12
  # following:
 
13
  #
 
14
  #     self.response_body = lambda { |response, output|
 
15
  #       # code here which refers to application models
 
16
  #     }
 
17
  #
 
18
  # Cleanup callbacks will not be called until after the response_body lambda
 
19
  # is evaluated, ensuring that it can refer to application models and other
 
20
  # classes before they are unloaded.
 
21
  #
 
22
  # 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
  # middleware stack, but are executed only when +ActionDispatch::Reloader.prepare!+
 
26
  # or +ActionDispatch::Reloader.cleanup!+ are called manually.
 
27
  #
 
28
  class Reloader
 
29
    include ActiveSupport::Callbacks
 
30
 
 
31
    define_callbacks :prepare, :scope => :name
 
32
    define_callbacks :cleanup, :scope => :name
 
33
 
 
34
    # Add a prepare callback. Prepare callbacks are run before each request, prior
 
35
    # to ActionDispatch::Callback's before callbacks.
 
36
    def self.to_prepare(*args, &block)
 
37
      set_callback(:prepare, *args, &block)
 
38
    end
 
39
 
 
40
    # Add a cleanup callback. Cleanup callbacks are run after each request is
 
41
    # complete (after #close is called on the response body).
 
42
    def self.to_cleanup(*args, &block)
 
43
      set_callback(:cleanup, *args, &block)
 
44
    end
 
45
 
 
46
    # Execute all prepare callbacks.
 
47
    def self.prepare!
 
48
      new(nil).prepare!
 
49
    end
 
50
 
 
51
    # Execute all cleanup callbacks.
 
52
    def self.cleanup!
 
53
      new(nil).cleanup!
 
54
    end
 
55
 
 
56
    def initialize(app, condition=nil)
 
57
      @app = app
 
58
      @condition = condition || lambda { true }
 
59
      @validated = true
 
60
    end
 
61
 
 
62
    def call(env)
 
63
      @validated = @condition.call
 
64
      prepare!
 
65
      response = @app.call(env)
 
66
      response[2] = ActionDispatch::BodyProxy.new(response[2]) { cleanup! }
 
67
      response
 
68
    rescue Exception
 
69
      cleanup!
 
70
      raise
 
71
    end
 
72
 
 
73
    def prepare! #:nodoc:
 
74
      run_callbacks :prepare if validated?
 
75
    end
 
76
 
 
77
    def cleanup! #:nodoc:
 
78
      run_callbacks :cleanup if validated?
 
79
    ensure
 
80
      @validated = true
 
81
    end
 
82
 
 
83
    private
 
84
 
 
85
    def validated? #:nodoc:
 
86
      @validated
 
87
    end
 
88
  end
 
89
end