aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/rack/metal.rb
blob: b1852272345d49eea5ed7d5c4264f733ea156047 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
require 'active_support/ordered_hash'

module Rails
  module Rack
    class Metal
      NotFoundResponse = [404, {}, []].freeze
      NotFound = lambda { NotFoundResponse }

      def self.metals
        base = "#{Rails.root}/app/metal"
        matcher = /\A#{Regexp.escape(base)}\/(.*)\.rb\Z/

        Dir["#{base}/**/*.rb"].sort.map do |file|
          file.sub!(matcher, '\1')
          require file
          file.classify.constantize
        end
      end

      def initialize(app)
        @app = app
        @metals = ActiveSupport::OrderedHash.new
        self.class.metals.each { |app| @metals[app] = true }
        freeze
      end

      def call(env)
        @metals.keys.each do |app|
          result = app.call(env)
          return result unless result[0].to_i == 404
        end
        @app.call(env)
      end
    end
  end
end