Class: WhalesDispatch::Router
- Inherits:
-
Object
- Object
- WhalesDispatch::Router
- Defined in:
- lib/whales_dispatch/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
- #build_resources(controller_noun, controller_actions) ⇒ Object
- #draw(&proc) ⇒ Object
-
#initialize ⇒ Router
constructor
A new instance of Router.
- #match(req) ⇒ Object
- #resources(controller_noun, scope, **action_restrictions) ⇒ Object
- #run(req, res) ⇒ Object
Constructor Details
#initialize ⇒ Router
Returns a new instance of Router.
6 7 8 9 |
# File 'lib/whales_dispatch/router.rb', line 6 def initialize @routes = [] @last_parent_route = "" end |
Instance Attribute Details
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
4 5 6 |
# File 'lib/whales_dispatch/router.rb', line 4 def routes @routes end |
Instance Method Details
#add_route(pattern, method, controller_class, action_name) ⇒ Object
11 12 13 |
# File 'lib/whales_dispatch/router.rb', line 11 def add_route(pattern, method, controller_class, action_name) @routes << Route.new(pattern, method, controller_class, action_name) end |
#build_resources(controller_noun, controller_actions) ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/whales_dispatch/router.rb', line 31 def build_resources(controller_noun, controller_actions) controller_actions.actions.each do |action_name, action_hash| resource = Resource.new( controller_noun, action_hash[:suffix], @last_parent_route ) send action_hash[:method], resource.pattern, resource.classify, action_name end end |
#draw(&proc) ⇒ Object
15 16 17 |
# File 'lib/whales_dispatch/router.rb', line 15 def draw(&proc) instance_eval(&proc) end |
#match(req) ⇒ Object
48 49 50 |
# File 'lib/whales_dispatch/router.rb', line 48 def match(req) routes.select { |route| route.matches?(req) }.first end |
#resources(controller_noun, scope, **action_restrictions) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/whales_dispatch/router.rb', line 19 def resources(controller_noun, scope, **action_restrictions) @last_parent_route = "" if scope == :parent controller_actions = DefaultActions.new(controller_noun) controller_actions.parse_action_restrictions(action_restrictions) build_resources(controller_noun, controller_actions) if block_given? @last_parent_route = controller_noun.to_s yield end end |
#run(req, res) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/whales_dispatch/router.rb', line 52 def run(req, res) route = match(req) if route route.run(req, res) else res.status = 404 res.body = "Could not find matching route." end end |