Class: Router
- Inherits:
-
Object
- Object
- Router
- Defined in:
- lib/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.
36 37 38 |
# File 'lib/router.rb', line 36 def initialize @routes = [] end |
Instance Attribute Details
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
34 35 36 |
# File 'lib/router.rb', line 34 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
41 42 43 |
# File 'lib/router.rb', line 41 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 :)
47 48 49 |
# File 'lib/router.rb', line 47 def draw(&proc) instance_eval(&proc) end |
#match(req) ⇒ Object
should return the route that matches this request
60 61 62 63 64 65 |
# File 'lib/router.rb', line 60 def match(req) routes.each do |route| return route if route.matches?(req) end nil end |
#run(req, res) ⇒ Object
either throw 404 or call run on a matched route
68 69 70 71 72 73 74 75 76 |
# File 'lib/router.rb', line 68 def run(req, res) route = match(req) if route route.run(req, res) else res.status = 404 res.body = ["Sorry, Charlie. That page doesn't exist."] end end |