2010-01-17 07:23:14 -05:00
|
|
|
require 'action_dispatch'
|
|
|
|
|
|
|
|
module Rails
|
2010-01-26 09:27:40 -05:00
|
|
|
class Application
|
2010-01-27 11:39:35 -05:00
|
|
|
class MetalLoader
|
|
|
|
attr_reader :paths, :metals
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@paths, @metals = [], []
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_middleware(list=nil)
|
|
|
|
load_metals!(list)
|
|
|
|
self
|
2010-01-24 08:48:00 -05:00
|
|
|
end
|
|
|
|
|
2010-01-27 11:39:35 -05:00
|
|
|
def new(app)
|
|
|
|
ActionDispatch::Cascade.new(@metals, app)
|
|
|
|
end
|
|
|
|
|
|
|
|
def name
|
|
|
|
ActionDispatch::Cascade.name
|
2010-01-26 09:27:40 -05:00
|
|
|
end
|
2010-01-27 11:39:35 -05:00
|
|
|
alias :to_s :name
|
2010-01-26 09:27:40 -05:00
|
|
|
|
2010-01-27 11:39:35 -05:00
|
|
|
protected
|
|
|
|
|
|
|
|
def load_metals!(list)
|
2010-01-24 08:48:00 -05:00
|
|
|
metals = []
|
2010-01-27 11:39:35 -05:00
|
|
|
list = Array(list || :all).map(&:to_sym)
|
2010-01-24 08:48:00 -05:00
|
|
|
|
2010-01-27 11:39:35 -05:00
|
|
|
paths.each do |path|
|
2010-01-24 08:48:00 -05:00
|
|
|
matcher = /\A#{Regexp.escape(path)}\/(.*)\.rb\Z/
|
|
|
|
Dir.glob("#{path}/**/*.rb").sort.each do |metal_path|
|
|
|
|
metal = metal_path.sub(matcher, '\1').to_sym
|
|
|
|
next unless list.include?(metal) || list.include?(:all)
|
|
|
|
require_dependency metal
|
|
|
|
metals << metal
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
metals = metals.sort_by do |m|
|
|
|
|
[list.index(m) || list.index(:all), m.to_s]
|
|
|
|
end
|
2010-01-17 07:23:14 -05:00
|
|
|
|
2010-01-24 08:48:00 -05:00
|
|
|
@metals = metals.map { |m| m.to_s.camelize.constantize }
|
2010-01-17 07:23:14 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|