Class: Router
- Inherits:
-
Object
- Object
- Router
- Defined in:
- lib/railz_lite/controllers/router.rb
Instance Attribute Summary collapse
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Instance Method Summary collapse
-
#add_route(pattern, method, controller_class, action_name) ⇒ Object
simply adds a new route to the list of routes.
-
#draw(&proc) ⇒ Object
evaluate the proc in the context of the instance for syntactic sugar :).
-
#initialize ⇒ Router
constructor
A new instance of Router.
-
#match(req) ⇒ Object
should return the route that matches this request.
-
#run(req, res) ⇒ Object
either throw 404 or call run on a matched route.
Constructor Details
#initialize ⇒ Router
Returns a new instance of Router.
30 31 32 |
# File 'lib/railz_lite/controllers/router.rb', line 30 def initialize @routes = [] end |
Instance Attribute Details
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
28 29 30 |
# File 'lib/railz_lite/controllers/router.rb', line 28 def routes @routes end |
Instance Method Details
#add_route(pattern, method, controller_class, action_name) ⇒ Object
simply adds a new route to the list of routes
35 36 37 |
# 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) ⇒ Object
evaluate the proc in the context of the instance for syntactic sugar :)
41 42 43 |
# File 'lib/railz_lite/controllers/router.rb', line 41 def draw(&proc) instance_eval(&proc) end |
#match(req) ⇒ Object
should return the route that matches this request
54 55 56 |
# File 'lib/railz_lite/controllers/router.rb', line 54 def match(req) @routes.find { |route| route.matches?(req) } end |
#run(req, res) ⇒ Object
either throw 404 or call run on a matched route
59 60 61 62 63 64 65 66 67 |
# 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 |