diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2008-12-18 16:55:03 -0800 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2008-12-18 16:55:03 -0800 |
commit | 6ce4b4303543eed6f2c323dcaae291bacfeb4d2c (patch) | |
tree | 24d3a3fa77d40b85286d218abf58e9771cf73235 /railties/lib/rails | |
parent | 03f6ecc6c650404c668638c07cdf7631df794b1a (diff) | |
download | rails-6ce4b4303543eed6f2c323dcaae291bacfeb4d2c.tar.gz rails-6ce4b4303543eed6f2c323dcaae291bacfeb4d2c.tar.bz2 rails-6ce4b4303543eed6f2c323dcaae291bacfeb4d2c.zip |
Cheaper metal cascade
Diffstat (limited to 'railties/lib/rails')
-rw-r--r-- | railties/lib/rails/rack/cascade.rb | 31 | ||||
-rw-r--r-- | railties/lib/rails/rack/metal.rb | 30 |
2 files changed, 51 insertions, 10 deletions
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 + module Metal + NotFoundResponse = [404, {}, []].freeze + NotFound = lambda { NotFoundResponse } + + class << self + def new(app) + Cascade.new(builtins + [app]) end - apps << app - ::Rack::Cascade.new(apps) - end - NotFound = lambda { |env| - [404, {"Content-Type" => "text/html"}, "Not Found"] - } + 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 |