Class: Rack::Callbacks

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/contrib/callbacks.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Callbacks

Returns a new instance of Callbacks.



5
6
7
8
9
# File 'lib/rack/contrib/callbacks.rb', line 5

def initialize(&block)
  @before = []
  @after  = []
  instance_eval(&block) if block_given?
end

Instance Method Details

#after(middleware, *args, &block) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/rack/contrib/callbacks.rb', line 19

def after(middleware, *args, &block)
  if block_given?
    @after << middleware.new(*args, &block)
  else
    @after << middleware.new(*args)
  end
end

#before(middleware, *args, &block) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/rack/contrib/callbacks.rb', line 11

def before(middleware, *args, &block)
  if block_given?
    @before << middleware.new(*args, &block)
  else
    @before << middleware.new(*args)
  end
end

#call(env) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/rack/contrib/callbacks.rb', line 31

def call(env)
  @before.each {|c| c.call(env) }

  response = @app.call(env)

  @after.inject(response) {|r, c| c.call(r) }
end

#run(app) ⇒ Object



27
28
29
# File 'lib/rack/contrib/callbacks.rb', line 27

def run(app)
  @app = app
end