Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow empty cascades. Reduce #call object allocation overhead. Speed …

…up #include?

Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information...
commit 0ab6b6c96dc43bec8a02d72ad5c6b944ad30df91 1 parent 01c1770
@jeremy jeremy authored josh committed
Showing with 20 additions and 17 deletions.
  1. +17 −12 lib/rack/cascade.rb
  2. +3 −5 test/spec_rack_cascade.rb
View
29 lib/rack/cascade.rb
@@ -4,31 +4,36 @@ module Rack
# status codes).
class Cascade
+ NotFound = [404, {}, []]
+
attr_reader :apps
def initialize(apps, catch=404)
- @apps = apps
- @catch = [*catch]
+ @apps = []; @has_app = {}
+ apps.each { |app| add app }
+
+ @catch = {}
+ [*catch].each { |status| @catch[status] = true }
end
def call(env)
- status = headers = body = nil
- raise ArgumentError, "empty cascade" if @apps.empty?
- @apps.each { |app|
- begin
- status, headers, body = app.call(env)
- break unless @catch.include?(status.to_i)
- end
- }
- [status, headers, body]
+ result = NotFound
+
+ @apps.each do |app|
+ result = app.call(env)
+ break unless @catch.include?(result[0].to_i)
+ end
+
+ result
end
def add app
+ @has_app[app] = true
@apps << app
end
def include? app
- @apps.include? app
+ @has_app.include? app
end
alias_method :<<, :add
View
8 test/spec_rack_cascade.rb
@@ -28,15 +28,13 @@
Rack::MockRequest.new(cascade).get("/cgi/../bla").should.be.not_found
end
- specify "should fail if empty" do
- lambda { Rack::MockRequest.new(Rack::Cascade.new([])).get("/") }.
- should.raise(ArgumentError)
+ specify "should return 404 if empty" do
+ Rack::MockRequest.new(Rack::Cascade.new([])).get('/').should.be.not_found
end
specify "should append new app" do
cascade = Rack::Cascade.new([], [404, 403])
- lambda { Rack::MockRequest.new(cascade).get('/cgi/test') }.
- should.raise(ArgumentError)
+ Rack::MockRequest.new(cascade).get('/').should.be.not_found
cascade << app2
Rack::MockRequest.new(cascade).get('/cgi/test').should.be.not_found
Rack::MockRequest.new(cascade).get('/cgi/../bla').should.be.not_found
Please sign in to comment.
Something went wrong with that request. Please try again.