Class: Kontrol::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/kontrol/router.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Router

Returns a new instance of Router.



5
6
7
8
9
10
# File 'lib/kontrol/router.rb', line 5

def initialize(&block)
  @routes = []
  @map = {}

  instance_eval(&block) if block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, pattern, *args, &block) ⇒ Object



26
27
28
29
30
31
# File 'lib/kontrol/router.rb', line 26

def method_missing(name, pattern, *args, &block)
  route = Route.new(name, pattern, args.first, block)
  
  @routes << route
  @map[name] = route
end

Instance Method Details

#__find__(name) ⇒ Object



12
13
14
# File 'lib/kontrol/router.rb', line 12

def __find__(name)
  @map[name.to_sym]
end

#__recognize__(request) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/kontrol/router.rb', line 16

def __recognize__(request)
  @routes.each do |route|
    if match = route.recognize(request)
      return route, match
    end
  end
  
  return nil
end