diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2009-05-14 09:28:11 +0800 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2009-05-16 00:29:56 +0800 |
commit | 0ab6b6c96dc43bec8a02d72ad5c6b944ad30df91 (patch) | |
tree | eb5691d2d9d3aa410f88af3519ae7d14e32ab145 /lib/rack/cascade.rb | |
parent | 01c17705195577c2df955a606ccd9f4f4026756d (diff) | |
download | rack-0ab6b6c96dc43bec8a02d72ad5c6b944ad30df91.tar.gz |
Allow empty cascades. Reduce #call object allocation overhead. Speed up #include?
Signed-off-by: Joshua Peek <josh@joshpeek.com>
Diffstat (limited to 'lib/rack/cascade.rb')
-rw-r--r-- | lib/rack/cascade.rb | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/lib/rack/cascade.rb b/lib/rack/cascade.rb index a038aa11..14c3e54d 100644 --- a/lib/rack/cascade.rb +++ b/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 |