aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_controller/vendor/rack-0.4.0/rack/builder.rb
diff options
context:
space:
mode:
authorJoshua Peek <josh@joshpeek.com>2008-11-22 14:33:00 -0600
committerJoshua Peek <josh@joshpeek.com>2008-11-22 14:33:00 -0600
commitcc67272cba35e50afa73cfec856c1677b204ae7e (patch)
tree1bdbc4862fe0ea486bf8d2476ab12184e4b71807 /actionpack/lib/action_controller/vendor/rack-0.4.0/rack/builder.rb
parent4b36f76e7a997fb03a6cccb08b8272ddccde5a3e (diff)
downloadrails-cc67272cba35e50afa73cfec856c1677b204ae7e.tar.gz
rails-cc67272cba35e50afa73cfec856c1677b204ae7e.tar.bz2
rails-cc67272cba35e50afa73cfec856c1677b204ae7e.zip
Vendor rack 0.4.0
Diffstat (limited to 'actionpack/lib/action_controller/vendor/rack-0.4.0/rack/builder.rb')
-rw-r--r--actionpack/lib/action_controller/vendor/rack-0.4.0/rack/builder.rb56
1 files changed, 56 insertions, 0 deletions
diff --git a/actionpack/lib/action_controller/vendor/rack-0.4.0/rack/builder.rb b/actionpack/lib/action_controller/vendor/rack-0.4.0/rack/builder.rb
new file mode 100644
index 0000000000..e708b52aee
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-0.4.0/rack/builder.rb
@@ -0,0 +1,56 @@
+module Rack
+ # Rack::Builder implements a small DSL to iteratively construct Rack
+ # applications.
+ #
+ # Example:
+ #
+ # app = Rack::Builder.new {
+ # use Rack::CommonLogger
+ # use Rack::ShowExceptions
+ # map "/lobster" do
+ # use Rack::Lint
+ # run Rack::Lobster.new
+ # end
+ # }
+ #
+ # +use+ adds a middleware to the stack, +run+ dispatches to an application.
+ # You can use +map+ to construct a Rack::URLMap in a convenient way.
+
+ class Builder
+ def initialize(&block)
+ @ins = []
+ instance_eval(&block) if block_given?
+ end
+
+ def use(middleware, *args, &block)
+ @ins << if block_given?
+ lambda { |app| middleware.new(app, *args, &block) }
+ else
+ lambda { |app| middleware.new(app, *args) }
+ end
+ end
+
+ def run(app)
+ @ins << app #lambda { |nothing| app }
+ end
+
+ def map(path, &block)
+ if @ins.last.kind_of? Hash
+ @ins.last[path] = Rack::Builder.new(&block).to_app
+ else
+ @ins << {}
+ map(path, &block)
+ end
+ end
+
+ def to_app
+ @ins[-1] = Rack::URLMap.new(@ins.last) if Hash === @ins.last
+ inner_app = @ins.last
+ @ins[0...-1].reverse.inject(inner_app) { |a, e| e.call(a) }
+ end
+
+ def call(env)
+ to_app.call(env)
+ end
+ end
+end