class Router
Attributes
routes[R]
Public Class Methods
new()
click to toggle source
# File lib/railz_lite/controllers/router.rb, line 30 def initialize @routes = [] end
Public Instance Methods
add_route(pattern, method, controller_class, action_name)
click to toggle source
simply adds a new route to the list of routes
# File lib/railz_lite/controllers/router.rb, line 35 def add_route(pattern, method, controller_class, action_name) @routes << Route.new(pattern, method, controller_class, action_name) end
draw(&proc)
click to toggle source
evaluate the proc in the context of the instance for syntactic sugar :)
# File lib/railz_lite/controllers/router.rb, line 41 def draw(&proc) instance_eval(&proc) end
match(req)
click to toggle source
should return the route that matches this request
# File lib/railz_lite/controllers/router.rb, line 54 def match(req) @routes.find { |route| route.matches?(req) } end
run(req, res)
click to toggle source
either throw 404 or call run on a matched route
# File lib/railz_lite/controllers/router.rb, line 59 def run(req, res) matching_route = match(req) if matching_route.nil? res.status = 404 res.write("Matching route not found for #{req.path}") else matching_route.run(req, res) end end