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.



3
4
5
6
7
# File 'lib/rack/contrib/callbacks.rb', line 3

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

Instance Method Details

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



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

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



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

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

#call(env) ⇒ Object



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

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



25
26
27
# File 'lib/rack/contrib/callbacks.rb', line 25

def run(app)
  @app = app
end