From 6ce4b4303543eed6f2c323dcaae291bacfeb4d2c Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Thu, 18 Dec 2008 16:55:03 -0800 Subject: [PATCH] Cheaper metal cascade --- railties/lib/rails/rack/cascade.rb | 31 +++++++++++++++++++++++++++++ railties/lib/rails/rack/metal.rb | 32 ++++++++++++++++++++---------- 2 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 railties/lib/rails/rack/cascade.rb diff --git a/railties/lib/rails/rack/cascade.rb b/railties/lib/rails/rack/cascade.rb new file mode 100644 index 0000000000..d5af7fc77e --- /dev/null +++ b/railties/lib/rails/rack/cascade.rb @@ -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 diff --git a/railties/lib/rails/rack/metal.rb b/railties/lib/rails/rack/metal.rb index 77d00ab091..1df31a1594 100644 --- a/railties/lib/rails/rack/metal.rb +++ b/railties/lib/rails/rack/metal.rb @@ -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