Class: WhalesDispatch::Route
- Inherits:
-
Object
- Object
- WhalesDispatch::Route
- Defined in:
- lib/whales_dispatch/route.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
- #run(req, res) ⇒ Object
Constructor Details
#initialize(pattern, http_method, controller_class, action_name) ⇒ Route
Returns a new instance of Route.
7 8 9 10 |
# File 'lib/whales_dispatch/route.rb', line 7 def initialize(pattern, http_method, controller_class, action_name) @pattern, @http_method = pattern, http_method @controller_class, @action_name = controller_class, action_name end |
Instance Attribute Details
#action_name ⇒ Object (readonly)
Returns the value of attribute action_name.
5 6 7 |
# File 'lib/whales_dispatch/route.rb', line 5 def action_name @action_name end |
#controller_class ⇒ Object (readonly)
Returns the value of attribute controller_class.
5 6 7 |
# File 'lib/whales_dispatch/route.rb', line 5 def controller_class @controller_class end |
#http_method ⇒ Object (readonly)
Returns the value of attribute http_method.
5 6 7 |
# File 'lib/whales_dispatch/route.rb', line 5 def http_method @http_method end |
#pattern ⇒ Object (readonly)
Returns the value of attribute pattern.
5 6 7 |
# File 'lib/whales_dispatch/route.rb', line 5 def pattern @pattern end |
Instance Method Details
#matches?(req) ⇒ Boolean
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/whales_dispatch/route.rb', line 12 def matches?(req) params = Params.new(req) if req.request_method == "POST" && params["_method"] method = params["_method"] else method = req.request_method end pattern =~ req.path && http_method == method.downcase.to_sym end |
#run(req, res) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/whales_dispatch/route.rb', line 23 def run(req, res) matches = pattern.match(req.path) route_params = Hash[matches.names.zip(matches.captures.map(&:to_i))] controller = controller_class.new(req, res, route_params) controller.invoke_action(action_name) end |