Railsで学ぶフレームワーク作り - Dispatcher編 その3 route設定の初期化部分
route設定の初期化してるところから読んできますか。routing, initあたりでackで調べると、それっぽいメソッドありますね。以下のメソッドのようですね
railties/lib/initializer.rb
def initialize_routing return unless configuration.frameworks.include?(:action_controller) ActionController::Routing.controller_paths += configuration.controller_paths ActionController::Routing::Routes.add_configuration_file(configuration.routes_configuration_file) ActionController::Routing::Routes.reload end
Routesなんてクラスない!と思ったら、
actionpack/lib/action_controller/routing.rb
371: Routes = RouteSet.new
ActionController::Routing::RouteSetのようですね。
reloadメソッド探すと、以下のようになってます。
routesが更新されてるかチェックしてからloadメソッドよんでますね。
def reload if configuration_files.any? && @routes_last_modified if routes_changed_at == @routes_last_modified return # routes didn't change, don't reload else @routes_last_modified = routes_changed_at end end load! end
load読んでみると、最終的にload_routes!呼んでますね。これが肝ですかね。
def load! Routing.use_controllers!(nil) # Clear the controller cache so we may discover new ones clear! load_routes! end
ようやくconfiguration fileをloadするところまできました。
def load_routes! if configuration_files.any? configuration_files.each { |config| load(config) } @routes_last_modified = routes_changed_at else add_route ":controller/:action/:id" end end