mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Cheaper metal cascade
This commit is contained in:
parent
03f6ecc6c6
commit
6ce4b43035
2 changed files with 52 additions and 11 deletions
31
railties/lib/rails/rack/cascade.rb
Normal file
31
railties/lib/rails/rack/cascade.rb
Normal file
|
@ -0,0 +1,31 @@
|
|||
require 'active_support/ordered_hash'
|
||||
|
||||
module Rails
|
||||
module Rack
|
||||
# Try a request on several apps; return the first non-404 response.
|
||||
class Cascade
|
||||
attr_reader :apps
|
||||
|
||||
def initialize(apps)
|
||||
@apps = ActiveSupport::OrderedHash.new
|
||||
apps.each { |app| add app }
|
||||
end
|
||||
|
||||
def call(env)
|
||||
@apps.keys.each do |app|
|
||||
result = app.call(env)
|
||||
return result unless result[0].to_i == 404
|
||||
end
|
||||
Metal::NotFoundResponse
|
||||
end
|
||||
|
||||
def add(app)
|
||||
@apps[app] = true
|
||||
end
|
||||
|
||||
def include?(app)
|
||||
@apps.include?(app)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,17 +1,27 @@
|
|||
require 'rails/rack/cascade'
|
||||
|
||||
module Rails
|
||||
module Rack
|
||||
class Metal
|
||||
def self.new(app)
|
||||
apps = Dir["#{Rails.root}/app/metal/*.rb"].map do |file|
|
||||
File.basename(file, '.rb').camelize.constantize
|
||||
end
|
||||
apps << app
|
||||
::Rack::Cascade.new(apps)
|
||||
end
|
||||
module Metal
|
||||
NotFoundResponse = [404, {}, []].freeze
|
||||
NotFound = lambda { NotFoundResponse }
|
||||
|
||||
NotFound = lambda { |env|
|
||||
[404, {"Content-Type" => "text/html"}, "Not Found"]
|
||||
}
|
||||
class << self
|
||||
def new(app)
|
||||
Cascade.new(builtins + [app])
|
||||
end
|
||||
|
||||
def builtins
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue