Class: Router
Instance Attribute Summary collapse
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Instance Method Summary collapse
- #add_route(pattern, http_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
#initialize ⇒ Router
Returns a new instance of Router.
26 27 28 |
# File 'lib/laris/router.rb', line 26 def initialize @routes = [] end |
Instance Attribute Details
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
24 25 26 |
# File 'lib/laris/router.rb', line 24 def routes @routes end |
Instance Method Details
#add_route(pattern, http_method, controller_class, action_name) ⇒ Object
30 31 32 |
# File 'lib/laris/router.rb', line 30 def add_route(pattern, http_method, controller_class, action_name) @routes << Route.new(pattern, http_method, controller_class, action_name) end |
#draw(&proc) ⇒ Object
34 35 36 |
# File 'lib/laris/router.rb', line 34 def draw(&proc) instance_eval(&proc) end |
#match(req) ⇒ Object
44 45 46 |
# File 'lib/laris/router.rb', line 44 def match(req) @routes.find { |route| route.matches?(req) } end |
#run(req, res) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/laris/router.rb', line 48 def run(req, res) route = match(req) if route.nil? res.status = 404 res.write("Oops! The requested URL #{req.path} was not not found!") else route.run(req, res) end end |