Class: Isono::Rack::Map

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&blk) ⇒ Map

Returns a new instance of Map.



13
14
15
16
# File 'lib/isono/rack/map.rb', line 13

def initialize(&blk)
  @table = {}
  instance_eval(&blk) if blk
end

Class Method Details

.build(&blk) ⇒ Object



7
8
9
10
11
# File 'lib/isono/rack/map.rb', line 7

def self.build(&blk)
  n = self.new
  blk.call(n)
  n
end

Instance Method Details

#call(req, res) ⇒ Object

Raises:



48
49
50
51
52
53
# File 'lib/isono/rack/map.rb', line 48

def call(req, res)
  mapped_app = @table[req.command.to_s] || @table['']
  raise UnknownMethodError if mapped_app.nil?
  
  mapped_app.call(req, res)
end

#default(&blk) ⇒ Object



44
45
46
# File 'lib/isono/rack/map.rb', line 44

def default(&blk)
  map('', blk)
end

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

Examples:

map :xxxx do
  response.response('xxxxx')
end
map :xxxx, A.new do
  puts self # A.new
end
map :xxxx, App.new


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/isono/rack/map.rb', line 28

def map(command, app=nil, &blk)
  command = command.to_s
  
  if app && blk
    @table[command]=Rack::Proc.new(app, &blk)
  elsif app && !blk
    raise TypeError unless app.respond_to?(:call)
    @table[command]=app
  elsif !app && blk
    @table[command]=Rack::Proc.new(&blk)
  else
    raise ArgumentError
  end
  self
end