Class: Track::RouteMap

Inherits:
Object
  • Object
show all
Defined in:
lib/track/route_map.rb

Instance Method Summary collapse

Constructor Details

#initializeRouteMap

Returns a new instance of RouteMap.



5
6
7
# File 'lib/track/route_map.rb', line 5

def initialize
  @routes = []
end

Instance Method Details

#add(pattern, klass, action, methods = nil) ⇒ Object Also known as: route



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/track/route_map.rb', line 9

def add(pattern, klass, action, methods = nil)
  raise "#{klass} is not a subclass of Track::Controller" unless klass <= Track::Controller
  methods = methods.is_a?(Array) ? methods : [methods] if methods
  regex, keys = compile_regexp(pattern)
  @routes.push(
    :pattern => regex,
    :keys    => keys,
    :class   => klass,
    :action  => action.to_sym,
    :methods => methods
  )
end

#scan(path, method) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/track/route_map.rb', line 23

def scan(path, method)
  @routes.each do |route|
    md = path.match(route[:pattern])
    if md && allowed_method?(route, method)
      keys = route[:keys]
      matches = {}
      (1..md.size-1).each { |i| matches[keys[i-1]] = md[i] }
      return route.merge(:matches => matches)
    end
  end
  nil
end