2009年8月27日木曜日

[Rails][CodeReading] Rails::Initializer (22) initialize_metal

Initializer.process で22番目によばれる initialize_metal メソッド。

def initialize_metal
Rails::Rack::Metal.requested_metals = configuration.metals
Rails::Rack::Metal.metal_paths += plugin_loader.engine_metal_paths

configuration.middleware.insert_before(
:"ActionController::ParamsParser",
Rails::Rack::Metal, :if => Rails::Rack::Metal.metals.any?)
end

名前からわかるとおり、Metal の初期化をしているらしい。

そもそもMetalを知らないのですが。。。
What's New in Edge Rails: Rails Metal という記事に

So Rails Metal is a way to cut out the fat of the MVC request processing stack (and the all the niceties, too) to get the quickest path to your application logic. This is especially useful when you have a single action that you need to run as quickly as possible and can afford to throw away all the support Rails normally provides in exchange for a very fast response time.
(超意訳)Rails Metal は、重厚なMVC処理(と、その恩恵)をすっとばして、アプリケーションロジックへのショートカットを提供します。できる限り速く動作させたいアクションがあり、通常Railsが提供するサポートと引き換えてでもレスポンスの速さを求めるなら、きっと役に立つでしょう。

とあります。

こう書くらしい。

# app/metal/poller.rb
class Poller < Rails::Rack::Metal
def call(env)
if env["PATH_INFO"] =~ /^\/poller/
[200, {"Content-Type" => "application/json"}, Message.recent.to_json]
else
super
end
end
end

パスにマッチしたところでステータスコードとヘッダとレスポンスボディを並べた配列をreturnしている(なんかコード見ただけでも速そう)。

[Rails][CodeReading] Railsの初期化コードを読む (イントロ&目次)

0 件のコメント:

コメントを投稿