2008-12-19 12:04:27 -05:00
|
|
|
require 'active_support/ordered_hash'
|
2009-04-22 19:10:49 -04:00
|
|
|
require 'active_support/core_ext/class/attribute_accessors'
|
2009-05-04 21:11:24 -04:00
|
|
|
require 'active_support/dependencies'
|
2008-12-18 19:55:03 -05:00
|
|
|
|
2008-12-16 14:11:51 -05:00
|
|
|
module Rails
|
|
|
|
module Rack
|
2008-12-19 12:04:27 -05:00
|
|
|
class Metal
|
2008-12-18 19:55:03 -05:00
|
|
|
NotFoundResponse = [404, {}, []].freeze
|
|
|
|
NotFound = lambda { NotFoundResponse }
|
|
|
|
|
2009-02-22 14:23:04 -05:00
|
|
|
cattr_accessor :metal_paths
|
|
|
|
self.metal_paths = ["#{Rails.root}/app/metal"]
|
2009-03-03 13:39:19 -05:00
|
|
|
cattr_accessor :requested_metals
|
2009-07-22 23:21:06 -04:00
|
|
|
|
|
|
|
cattr_accessor :pass_through_on
|
|
|
|
self.pass_through_on = 404
|
2009-02-22 14:23:04 -05:00
|
|
|
|
2008-12-19 12:04:27 -05:00
|
|
|
def self.metals
|
2009-02-22 14:23:04 -05:00
|
|
|
matcher = /#{Regexp.escape('/app/metal/')}(.*)\.rb\Z/
|
|
|
|
metal_glob = metal_paths.map{ |base| "#{base}/**/*.rb" }
|
2009-03-03 13:39:19 -05:00
|
|
|
all_metals = {}
|
2008-12-19 12:04:27 -05:00
|
|
|
|
2009-03-05 19:50:52 -05:00
|
|
|
metal_glob.each do |glob|
|
|
|
|
Dir[glob].sort.map do |file|
|
|
|
|
file = file.match(matcher)[1]
|
2009-03-15 19:41:21 -04:00
|
|
|
all_metals[file.camelize] = file
|
2009-03-05 19:50:52 -05:00
|
|
|
end
|
2008-12-17 10:53:56 -05:00
|
|
|
end
|
2009-03-03 13:39:19 -05:00
|
|
|
|
|
|
|
load_list = requested_metals || all_metals.keys
|
|
|
|
|
|
|
|
load_list.map do |requested_metal|
|
|
|
|
if metal = all_metals[requested_metal]
|
2009-05-04 21:11:24 -04:00
|
|
|
require_dependency metal
|
2009-03-03 13:39:19 -05:00
|
|
|
requested_metal.constantize
|
|
|
|
end
|
|
|
|
end.compact
|
2008-12-19 12:04:27 -05:00
|
|
|
end
|
2008-12-17 10:53:56 -05:00
|
|
|
|
2008-12-19 12:04:27 -05:00
|
|
|
def initialize(app)
|
|
|
|
@app = app
|
2009-07-22 23:21:06 -04:00
|
|
|
@pass_through_on = {}
|
|
|
|
[*self.class.pass_through_on].each { |status| @pass_through_on[status] = true }
|
|
|
|
|
2008-12-19 12:04:27 -05:00
|
|
|
@metals = ActiveSupport::OrderedHash.new
|
|
|
|
self.class.metals.each { |app| @metals[app] = true }
|
|
|
|
freeze
|
|
|
|
end
|
2008-12-18 19:55:03 -05:00
|
|
|
|
2008-12-19 12:04:27 -05:00
|
|
|
def call(env)
|
|
|
|
@metals.keys.each do |app|
|
|
|
|
result = app.call(env)
|
2009-07-22 23:21:06 -04:00
|
|
|
return result unless @pass_through_on.include?(result[0].to_i)
|
2008-12-18 19:55:03 -05:00
|
|
|
end
|
2008-12-19 12:04:27 -05:00
|
|
|
@app.call(env)
|
2008-12-18 19:55:03 -05:00
|
|
|
end
|
2008-12-16 14:11:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|