Skip to content

Instantly share code, notes, and snippets.

@elvuel
Forked from lifo/callbacks.rb
Created October 24, 2011 07:10
Show Gist options
  • Save elvuel/1308529 to your computer and use it in GitHub Desktop.
Save elvuel/1308529 to your computer and use it in GitHub Desktop.
module Rack
class Callbacks < ::Array
DUMMY_APP = proc {|env| }
def initialize(app, &block)
@app = app
instance_eval(&block) if block_given?
end
def use(middleware, *args, &block)
if block_given?
push middleware.new(DUMMY_APP, *args, &block)
else
push middleware.new(DUMMY_APP, *args)
end
end
def call(env)
each {|c| c.call(env) }
@app.call(env)
end
end
end
require 'rack/mock'
require 'rack/contrib/callbacks'
class Flame
def initialize(app)
@app = app
end
def call(env)
env['flame'] = 'F Lifo..'
@app.call(env)
end
end
class Pacify
def initialize(app, with)
@app = app
@with = with
end
def call(env)
env['peace'] = @with
@app.call(env)
end
end
context "Rack::Callbacks" do
specify "supports blocks on use" do
app = Rack::Builder.new do
use Rack::Callbacks do
use Flame
use Pacify, "with love"
end
run lambda { |env| [200, {}, env['flame'] + env['peace']] }
end.to_app
response = Rack::MockRequest.new(app).get("/")
response.body.to_s.should.equal 'F Lifo..with love'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment