diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2009-05-01 20:24:14 +0100 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2009-05-01 20:24:14 +0100 |
commit | 853c229bbdb32c27231df8ad0d446bb35e588586 (patch) | |
tree | f7bc77c0899c7e42254d9397945b57f7f166eb88 /actionpack/lib/action_dispatch/vendor/rack-1.1.pre/rack/builder.rb | |
parent | 432e631d5c9cc59db3e5c515c348352a4268eb7f (diff) | |
download | rails-853c229bbdb32c27231df8ad0d446bb35e588586.tar.gz rails-853c229bbdb32c27231df8ad0d446bb35e588586.tar.bz2 rails-853c229bbdb32c27231df8ad0d446bb35e588586.zip |
Rename vendor/rack to vendor/rack-1.1.pre
Diffstat (limited to 'actionpack/lib/action_dispatch/vendor/rack-1.1.pre/rack/builder.rb')
-rw-r--r-- | actionpack/lib/action_dispatch/vendor/rack-1.1.pre/rack/builder.rb | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/actionpack/lib/action_dispatch/vendor/rack-1.1.pre/rack/builder.rb b/actionpack/lib/action_dispatch/vendor/rack-1.1.pre/rack/builder.rb new file mode 100644 index 0000000000..295235e56a --- /dev/null +++ b/actionpack/lib/action_dispatch/vendor/rack-1.1.pre/rack/builder.rb @@ -0,0 +1,63 @@ +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 + # } + # + # Or + # + # app = Rack::Builder.app do + # use Rack::CommonLogger + # lambda { |env| [200, {'Content-Type' => 'text/plain'}, 'OK'] } + # 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 self.app(&block) + self.new(&block).to_app + end + + def use(middleware, *args, &block) + @ins << lambda { |app| middleware.new(app, *args, &block) } + end + + def run(app) + @ins << app #lambda { |nothing| app } + end + + def map(path, &block) + if @ins.last.kind_of? Hash + @ins.last[path] = self.class.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 |