Class: Isono::Rack::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/isono/rack/builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(&blk) ⇒ Builder

Returns a new instance of Builder.



6
7
8
9
10
# File 'lib/isono/rack/builder.rb', line 6

def initialize(&blk)
  @filters = []
  @app = Map.new
  instance_eval(&blk) if blk
end

Instance Method Details

#call(req, res) ⇒ Object



34
35
36
37
# File 'lib/isono/rack/builder.rb', line 34

def call(req, res)
  raise "main app is not set" if @app.nil?
  @filters.reverse.inject(@app) {|d, f| f.call(d) }.call(req, res)
end

#map(command, app = nil, &blk) ⇒ Object

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/isono/rack/builder.rb', line 22

def map(command, app=nil, &blk)
  raise ArgumentError if app && blk
  if app
    raise TypeError unless app.respond_to?(:call)
    @app.map(command, app)
  elsif blk
    @app.map(command, self.class.new(&blk))
  else
    raise ArgumentError
  end
end

#run(app) ⇒ Object

Raises:

  • (TypeError)


17
18
19
20
# File 'lib/isono/rack/builder.rb', line 17

def run(app)
  raise TypeError unless app.respond_to?(:call)
  @app.map('', app)
end

#use(decorator_class, *args) ⇒ Object

Raises:

  • (TypeError)


12
13
14
15
# File 'lib/isono/rack/builder.rb', line 12

def use(decorator_class, *args)
  raise TypeError unless decorator_class < Decorator
  @filters << lambda {|disp| decorator_class.new(disp, *args) }
end