Class: Rlyeh::DeepOnes::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/rlyeh/deep_ones/builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app = nil, &block) ⇒ Builder

Returns a new instance of Builder.



4
5
6
7
8
# File 'lib/rlyeh/deep_ones/builder.rb', line 4

def initialize(app = nil, &block)
  @stack = []
  @runner = app
  instance_eval(&block) if block_given?
end

Class Method Details

.app(app = nil, &block) ⇒ Object



10
11
12
# File 'lib/rlyeh/deep_ones/builder.rb', line 10

def self.app(app = nil, &block)
  self.new(app, &block).to_app
end

Instance Method Details

#call(env) ⇒ Object



32
33
34
# File 'lib/rlyeh/deep_ones/builder.rb', line 32

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

#run(app) ⇒ Object



22
23
24
# File 'lib/rlyeh/deep_ones/builder.rb', line 22

def run(app)
  @runner = app
end

#to_appObject



26
27
28
29
30
# File 'lib/rlyeh/deep_ones/builder.rb', line 26

def to_app
  @stack.reverse.inject(@runner) do |inner, outer|
    outer.call inner
  end
end

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



14
15
16
# File 'lib/rlyeh/deep_ones/builder.rb', line 14

def use(middleware, *args, &block)
  @stack << proc { |app| middleware.new(app, *args, &block) }
end

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



18
19
20
# File 'lib/rlyeh/deep_ones/builder.rb', line 18

def use!(middleware, *args, &block)
  @stack.unshift lambda { |app| middleware.new(app, *args, &block) }
end