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
- #draw(&proc) ⇒ Object
-
#initialize ⇒ Router
constructor
A new instance of Router.
- #match(req) ⇒ Object
- #run(req, res) ⇒ Object
Constructor Details
permalink #initialize ⇒ Router
Returns a new instance of Router.
29 30 31 |
# File 'lib/router.rb', line 29 def initialize @routes = [] end |
Instance Attribute Details
permalink #routes ⇒ Object (readonly)
Returns the value of attribute routes.
27 28 29 |
# File 'lib/router.rb', line 27 def routes @routes end |
Instance Method Details
permalink #add_route(pattern, method, controller_class, action_name) ⇒ Object
[View source]
33 34 35 |
# File 'lib/router.rb', line 33 def add_route(pattern, method, controller_class, action_name) @routes << Route.new(pattern, method, controller_class, action_name) end |
permalink #draw(&proc) ⇒ Object
[View source]
37 38 39 |
# File 'lib/router.rb', line 37 def draw(&proc) self.instance_eval(&proc) end |
permalink #match(req) ⇒ Object
[View source]
48 49 50 51 52 53 |
# File 'lib/router.rb', line 48 def match(req) @routes.each do |route| return route if route.matches?(req) end nil end |
permalink #run(req, res) ⇒ Object
[View source]
55 56 57 58 59 |
# File 'lib/router.rb', line 55 def run(req, res) route = match(req) return res.status = 404 unless route route.run(req,res) end |