aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_dispatch/middleware/callbacks.rb
diff options
context:
space:
mode:
authorJoshua Peek <josh@joshpeek.com>2009-05-17 13:39:44 -0500
committerJoshua Peek <josh@joshpeek.com>2009-05-17 13:39:55 -0500
commit092089015b79752c5e9d664b3eeefef9e2223e36 (patch)
tree69887f3ae3dbc479cb4f2571930f0a46024bbb46 /actionpack/lib/action_dispatch/middleware/callbacks.rb
parentedc9c226d11e6104d191ceeb6416c7062ceda54a (diff)
downloadrails-092089015b79752c5e9d664b3eeefef9e2223e36.tar.gz
rails-092089015b79752c5e9d664b3eeefef9e2223e36.tar.bz2
rails-092089015b79752c5e9d664b3eeefef9e2223e36.zip
Extract generic callbacks middleware from dispatcher
Diffstat (limited to 'actionpack/lib/action_dispatch/middleware/callbacks.rb')
-rw-r--r--actionpack/lib/action_dispatch/middleware/callbacks.rb40
1 files changed, 40 insertions, 0 deletions
diff --git a/actionpack/lib/action_dispatch/middleware/callbacks.rb b/actionpack/lib/action_dispatch/middleware/callbacks.rb
new file mode 100644
index 0000000000..0a2b4cf5f7
--- /dev/null
+++ b/actionpack/lib/action_dispatch/middleware/callbacks.rb
@@ -0,0 +1,40 @@
+module ActionDispatch
+ class Callbacks
+ include ActiveSupport::Callbacks
+ define_callbacks :prepare, :before, :after
+
+ class << self
+ # DEPRECATED
+ alias_method :prepare_dispatch, :prepare
+ alias_method :before_dispatch, :before
+ alias_method :after_dispatch, :after
+ end
+
+ # Add a preparation callback. Preparation callbacks are run before every
+ # request in development mode, and before the first request in production
+ # mode.
+ #
+ # An optional identifier may be supplied for the callback. If provided,
+ # to_prepare may be called again with the same identifier to replace the
+ # existing callback. Passing an identifier is a suggested practice if the
+ # code adding a preparation block may be reloaded.
+ def self.to_prepare(identifier = nil, &block)
+ @prepare_callbacks ||= ActiveSupport::Callbacks::CallbackChain.new
+ callback = ActiveSupport::Callbacks::Callback.new(:prepare, block, :identifier => identifier)
+ @prepare_callbacks.replace_or_append!(callback)
+ end
+
+ def initialize(app, prepare_each_request = false)
+ @app, @prepare_each_request = app, prepare_each_request
+ run_callbacks :prepare
+ end
+
+ def call(env)
+ run_callbacks :before
+ run_callbacks :prepare if @prepare_each_request
+ @app.call(env)
+ ensure
+ run_callbacks :after, :enumerator => :reverse_each
+ end
+ end
+end