Skip to content
Browse files

Cheaper metal cascade

  • Loading branch information...
1 parent 03f6ecc commit 6ce4b4303543eed6f2c323dcaae291bacfeb4d2c @jeremy jeremy committed Dec 18, 2008
Showing with 51 additions and 10 deletions.
  1. +31 −0 railties/lib/rails/rack/cascade.rb
  2. +20 −10 railties/lib/rails/rack/metal.rb
View
31 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
View
30 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

0 comments on commit 6ce4b43

Please sign in to comment.
Something went wrong with that request. Please try again.