Class: Middlestack::Stack

Inherits:
Object
  • Object
show all
Defined in:
lib/middlestack/stack.rb

Defined Under Namespace

Classes: Proxy

Instance Method Summary collapse

Constructor Details

#initialize(default_app = nil, &block) ⇒ Stack

Returns a new instance of Stack.



25
26
27
28
# File 'lib/middlestack/stack.rb', line 25

def initialize(default_app = nil, &block)
  @use, @run = [], default_app
  instance_eval(&block) if block_given?
end

Instance Method Details

#call(env) ⇒ Object



40
41
42
# File 'lib/middlestack/stack.rb', line 40

def call(env)
  to_app.call(env)
end

#to_app(app) ⇒ Object



34
35
36
37
38
# File 'lib/middlestack/stack.rb', line 34

def to_app(app)
  app ||= @run
  fail 'missing run statement' unless app
  @use.reverse.inject(app){|a, e| e[a] }
end

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



30
31
32
# File 'lib/middlestack/stack.rb', line 30

def use(middleware, *args, &block)
  @use << ->(app){ Proxy.new(middleware, app, *args, &block) }
end