Merged updates from trunk into stable branch
[feedcatcher.git] / vendor / rails / railties / lib / rails / rack / metal.rb
1 require 'active_support/ordered_hash'
2
3 module Rails
4 module Rack
5 class Metal
6 NotFoundResponse = [404, {}, []].freeze
7 NotFound = lambda { NotFoundResponse }
8
9 cattr_accessor :metal_paths
10 self.metal_paths = ["#{Rails.root}/app/metal"]
11 cattr_accessor :requested_metals
12
13 def self.metals
14 matcher = /#{Regexp.escape('/app/metal/')}(.*)\.rb\Z/
15 metal_glob = metal_paths.map{ |base| "#{base}/**/*.rb" }
16 all_metals = {}
17
18 metal_glob.each do |glob|
19 Dir[glob].sort.map do |file|
20 file = file.match(matcher)[1]
21 all_metals[file.camelize] = file
22 end
23 end
24
25 load_list = requested_metals || all_metals.keys
26
27 load_list.map do |requested_metal|
28 if metal = all_metals[requested_metal]
29 require metal
30 requested_metal.constantize
31 end
32 end.compact
33 end
34
35 def initialize(app)
36 @app = app
37 @metals = ActiveSupport::OrderedHash.new
38 self.class.metals.each { |app| @metals[app] = true }
39 freeze
40 end
41
42 def call(env)
43 @metals.keys.each do |app|
44 result = app.call(env)
45 return result unless result[0].to_i == 404
46 end
47 @app.call(env)
48 end
49 end
50 end
51 end