Class: Tep::Router

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



91
92
93
# File 'lib/tep/router.rb', line 91

def initialize
  @routes = [Route.new("", "", Handler.new)]   # type-seed sentinel
end

Instance Attribute Details

#routesObject

Returns the value of attribute routes.



89
90
91
# File 'lib/tep/router.rb', line 89

def routes
  @routes
end

Instance Method Details

#add(verb, pattern, handler) ⇒ Object



95
96
97
# File 'lib/tep/router.rb', line 95

def add(verb, pattern, handler)
  @routes.push(Route.new(verb, pattern, handler))
end

#index_of(route) ⇒ Object



126
127
128
129
130
131
132
133
134
135
# File 'lib/tep/router.rb', line 126

def index_of(route)
  i = 0
  while i < @routes.length
    if @routes[i] == route
      return i
    end
    i += 1
  end
  -1
end

#match(req) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/tep/router.rb', line 99

def match(req)
  i = 1                       # skip the seed at index 0
  while i < @routes.length
    r = @routes[i]
    if r.matches?(req.verb, req.path)
      return r
    end
    i += 1
  end
  nil
end

#match_after(req, start_idx) ⇒ Object

Find the next matching route after ‘start_idx` (1-based; the seed at 0 is skipped). Used by `pass` to step to the next candidate. Returns the Route + its index, or nil + -1.



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/tep/router.rb', line 114

def match_after(req, start_idx)
  i = start_idx + 1
  while i < @routes.length
    r = @routes[i]
    if r.matches?(req.verb, req.path)
      return r
    end
    i += 1
  end
  nil
end