Class: Route
- Inherits:
-
Object
- Object
- Route
- Defined in:
- lib/railz_lite/controllers/router.rb
Instance Attribute Summary collapse
-
#action_name ⇒ Object
readonly
Returns the value of attribute action_name.
-
#controller_class ⇒ Object
readonly
Returns the value of attribute controller_class.
-
#http_method ⇒ Object
readonly
Returns the value of attribute http_method.
-
#pattern ⇒ Object
readonly
Returns the value of attribute pattern.
Instance Method Summary collapse
-
#initialize(pattern, http_method, controller_class, action_name) ⇒ Route
constructor
A new instance of Route.
-
#matches?(req) ⇒ Boolean
checks if pattern matches path and method matches request method.
-
#run(req, res) ⇒ Object
use pattern to pull out route params (save for later?) instantiate controller and call controller action.
Constructor Details
#initialize(pattern, http_method, controller_class, action_name) ⇒ Route
Returns a new instance of Route.
4 5 6 7 8 9 |
# File 'lib/railz_lite/controllers/router.rb', line 4 def initialize(pattern, http_method, controller_class, action_name) @pattern = pattern @http_method = http_method @controller_class = controller_class @action_name = action_name end |
Instance Attribute Details
#action_name ⇒ Object (readonly)
Returns the value of attribute action_name.
2 3 4 |
# File 'lib/railz_lite/controllers/router.rb', line 2 def action_name @action_name end |
#controller_class ⇒ Object (readonly)
Returns the value of attribute controller_class.
2 3 4 |
# File 'lib/railz_lite/controllers/router.rb', line 2 def controller_class @controller_class end |
#http_method ⇒ Object (readonly)
Returns the value of attribute http_method.
2 3 4 |
# File 'lib/railz_lite/controllers/router.rb', line 2 def http_method @http_method end |
#pattern ⇒ Object (readonly)
Returns the value of attribute pattern.
2 3 4 |
# File 'lib/railz_lite/controllers/router.rb', line 2 def pattern @pattern end |
Instance Method Details
#matches?(req) ⇒ Boolean
checks if pattern matches path and method matches request method
12 13 14 |
# File 'lib/railz_lite/controllers/router.rb', line 12 def matches?(req) (pattern =~ req.path) == 0 && req.request_method.downcase.to_sym == http_method end |
#run(req, res) ⇒ Object
use pattern to pull out route params (save for later?) instantiate controller and call controller action
18 19 20 21 22 23 24 |
# File 'lib/railz_lite/controllers/router.rb', line 18 def run(req, res) match_data = @pattern.match(req.path) route_params = Hash[match_data.names.zip(match_data.captures)] controller = controller_class.new(req, res, route_params) controller.invoke_action(action_name) end |