Rails 2.3.0で MiddlewareStack に積む方法
まず Rails 2.3.0 RC1 は Rack 対応なおかげで,
% rake middleware use Rack::Lock use ActionController::Failsafe use ActiveRecord::SessionStore, {:session_key=>"_session_id", :cookie_only=>false} use ActionController::RewindableInput use ActionController::ParamsParser use Rack::MethodOverride use Rack::Head use ActiveRecord::QueryCache run ActionController::Dispatcher.new
と言う感じで Rack 対応モジュールが順次実行されています.このどこかにオレオレモジュール
class DummyRack def initialize(app) @app = app end def call(env) @app.call(env) end end
を積むには config/environment.rb に下記を追記
ActionController::Dispatcher.middleware.use DummyRack
すれば,
% rake middleware use Rack::Lock use ActionController::Failsafe use ActiveRecord::SessionStore, {:cookie_only=>false, :session_key=>"_session_id"} use ActionController::RewindableInput use ActionController::ParamsParser use Rack::MethodOverride use Rack::Head use ActiveRecord::QueryCache use DummyRack run ActionController::Dispatcher.new
と言う具合に「最後」に追加される.
実装的には,ActionController::MiddlewareStack という Array を継承したクラスがあり,そこに実行すべき Rack モジュールが入っていて,この配列に
- use Module
- 最後に追加
- insert(index, Module)
- index 番目として挿入
- insert_after(index, Module)
- index 番目の次に挿入(index + 1 番目)
- swap(index, Module)
- index 番目のモジュールと交換する
な感じです.