1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Refactor MetalLoader and RoutesReloader to rely less on class configuration.

Signed-off-by: Carl Lerche <carllerche@mac.com>
This commit is contained in:
José Valim 2010-01-27 17:39:35 +01:00 committed by Carl Lerche
parent 252911e378
commit 226d8e745a
6 changed files with 40 additions and 35 deletions

View file

@ -9,7 +9,7 @@ module Rails
autoload :Configurable, 'rails/application/configurable' autoload :Configurable, 'rails/application/configurable'
autoload :Configuration, 'rails/application/configuration' autoload :Configuration, 'rails/application/configuration'
autoload :Finisher, 'rails/application/finisher' autoload :Finisher, 'rails/application/finisher'
autoload :Metal, 'rails/application/metal' autoload :MetalLoader, 'rails/application/metal_loader'
autoload :Railties, 'rails/application/railties' autoload :Railties, 'rails/application/railties'
autoload :RoutesReloader, 'rails/application/routes_reloader' autoload :RoutesReloader, 'rails/application/routes_reloader'
@ -51,8 +51,12 @@ module Rails
@railties ||= Railties.new(config) @railties ||= Railties.new(config)
end end
def metal_loader
@metal_laoder ||= MetalLoader.new
end
def routes_reloader def routes_reloader
@routes_reloader ||= RoutesReloader.new(config) @routes_reloader ||= RoutesReloader.new
end end
def reload_routes! def reload_routes!

View file

@ -15,9 +15,9 @@ module Rails
end end
end end
initializer :add_builtin_route do initializer :add_builtin_route do |app|
if Rails.env.development? if Rails.env.development?
Rails::Application::RoutesReloader.paths << File.join(RAILTIES_PATH, 'builtin', 'routes.rb') app.routes_reloader.paths << File.join(RAILTIES_PATH, 'builtin', 'routes.rb')
end end
end end

View file

@ -2,20 +2,34 @@ require 'action_dispatch'
module Rails module Rails
class Application class Application
class Metal class MetalLoader
def self.paths attr_reader :paths, :metals
@paths ||= []
def initialize
@paths, @metals = [], []
end end
def self.metals def build_middleware(list=nil)
@metals ||= [] load_metals!(list)
self
end end
def initialize(list=nil) def new(app)
ActionDispatch::Cascade.new(@metals, app)
end
def name
ActionDispatch::Cascade.name
end
alias :to_s :name
protected
def load_metals!(list)
metals = [] metals = []
list = Array(list || :all).map(&:to_sym) list = Array(list || :all).map(&:to_sym)
self.class.paths.each do |path| paths.each do |path|
matcher = /\A#{Regexp.escape(path)}\/(.*)\.rb\Z/ matcher = /\A#{Regexp.escape(path)}\/(.*)\.rb\Z/
Dir.glob("#{path}/**/*.rb").sort.each do |metal_path| Dir.glob("#{path}/**/*.rb").sort.each do |metal_path|
metal = metal_path.sub(matcher, '\1').to_sym metal = metal_path.sub(matcher, '\1').to_sym
@ -30,17 +44,7 @@ module Rails
end end
@metals = metals.map { |m| m.to_s.camelize.constantize } @metals = metals.map { |m| m.to_s.camelize.constantize }
self.class.metals.concat(@metals)
end end
def new(app)
ActionDispatch::Cascade.new(@metals, app)
end
def name
ActionDispatch::Cascade.name
end
alias_method :to_s, :name
end end
end end
end end

View file

@ -1,19 +1,16 @@
module Rails module Rails
class Application class Application
# TODO Write tests for this behavior extracted from Application
class RoutesReloader class RoutesReloader
def self.paths attr_reader :paths
@paths ||= []
end
def initialize(config) def initialize
@config, @last_change_at = config, nil @paths, @last_change_at = [], nil
end end
def changed_at def changed_at
routes_changed_at = nil routes_changed_at = nil
self.class.paths.each do |path| paths.each do |path|
config_changed_at = File.stat(path).mtime config_changed_at = File.stat(path).mtime
if routes_changed_at.nil? || config_changed_at > routes_changed_at if routes_changed_at.nil? || config_changed_at > routes_changed_at
@ -29,7 +26,7 @@ module Rails
routes.disable_clear_and_finalize = true routes.disable_clear_and_finalize = true
routes.clear! routes.clear!
self.class.paths.each { |path| load(path) } paths.each { |path| load(path) }
routes.finalize! routes.finalize!
nil nil

View file

@ -16,7 +16,7 @@ module Rails
middleware.use('::ActionDispatch::Cookies') middleware.use('::ActionDispatch::Cookies')
middleware.use(lambda { ActionController::Base.session_store }, lambda { ActionController::Base.session_options }) middleware.use(lambda { ActionController::Base.session_store }, lambda { ActionController::Base.session_options })
middleware.use('::ActionDispatch::Flash', :if => lambda { ActionController::Base.session_store }) middleware.use('::ActionDispatch::Flash', :if => lambda { ActionController::Base.session_store })
middleware.use(lambda { Rails::Application::Metal.new(Rails.application.config.metals) }, :if => lambda { Rails::Application::Metal.metals.any? }) middleware.use(lambda { Rails.application.metal_loader.build_middleware(Rails.application.config.metals) }, :if => lambda { Rails.application.metal_loader.metals.any? })
middleware.use('ActionDispatch::ParamsParser') middleware.use('ActionDispatch::ParamsParser')
middleware.use('::Rack::MethodOverride') middleware.use('::Rack::MethodOverride')
middleware.use('::ActionDispatch::Head') middleware.use('::ActionDispatch::Head')

View file

@ -70,9 +70,9 @@ module Rails
config.load_once_paths.freeze config.load_once_paths.freeze
end end
initializer :add_routing_paths do initializer :add_routing_paths do |app|
paths.config.routes.to_a.each do |route| paths.config.routes.to_a.each do |route|
Rails::Application::RoutesReloader.paths.unshift(route) if File.exists?(route) app.routes_reloader.paths.unshift(route) if File.exists?(route)
end end
end end
@ -98,8 +98,8 @@ module Rails
ActionMailer::Base.view_paths.unshift(*views) if defined?(ActionMailer) ActionMailer::Base.view_paths.unshift(*views) if defined?(ActionMailer)
end end
initializer :add_metals do initializer :add_metals do |app|
Rails::Application::Metal.paths.unshift(*paths.app.metals.to_a) app.metal_loader.paths.unshift(*paths.app.metals.to_a)
end end
initializer :load_application_initializers do initializer :load_application_initializers do