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.
33 34 35 |
# File 'lib/router.rb', line 33 def initialize @routes = [] end |
Instance Attribute Details
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
31 32 33 |
# File 'lib/router.rb', line 31 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
38 39 40 |
# File 'lib/router.rb', line 38 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 :)
44 45 46 |
# File 'lib/router.rb', line 44 def draw(&proc) instance_eval(&proc) end |
#match(req) ⇒ Object
should return the route that matches this request
57 58 59 60 61 62 |
# File 'lib/router.rb', line 57 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
65 66 67 68 69 70 71 72 73 |
# File 'lib/router.rb', line 65 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 |