Module: Gin::Mountable
- Included in:
- Controller
- Defined in:
- lib/gin/mountable.rb
Overview
The Gin::Mountable module provides an interface to mount any type of object to a Gin::App route.
The Gin::Mountable module is only necessary if features such automatic route naming and defaults are needed. Gin will otherwise happily mount any object that responds to the ‘call’ method onto it’s routing tree.
Instance Method Summary collapse
-
#actions ⇒ Object
The actions available on this Mountable object.
-
#controller_name ⇒ Object
The String representing the controller.
-
#default_route_for(action) ⇒ Object
Should return a 2 item Array with the HTTP verb and request path (local to the controller) to use for a given action.
-
#display_name(action = nil) ⇒ Object
Creates a display name for the Controller (and optional action).
-
#route_name_for(action) ⇒ Object
Creates a route name used to identify a given route.
-
#verify_mount! ⇒ Object
:nodoc:.
Instance Method Details
#actions ⇒ Object
The actions available on this Mountable object. Actions can be of any object type.
Must return an Array of actions. Must be overloaded.
UserController.actions
#=> [:show, :delete, :list, :update]
37 38 39 40 41 |
# File 'lib/gin/mountable.rb', line 37 def actions raise NoMethodError, "The `#{__method__}' method must be defined on #{self} and return an\ Array of available actions for the Router to map to." end |
#controller_name ⇒ Object
The String representing the controller.
Must return a String. Must be overloaded.
UserController.controller_name
#=> 'user'
51 52 53 54 55 |
# File 'lib/gin/mountable.rb', line 51 def controller_name raise NoMethodError, "The `#{__method__}' method must be defined on #{self} and return a\ String representing the controller name." end |
#default_route_for(action) ⇒ Object
Should return a 2 item Array with the HTTP verb and request path (local to the controller) to use for a given action.
Must return a 2 item Array of Strings. Must be overloaded.
UserController.default_route_for :show
#=> ['GET', '/:id']
66 67 68 69 70 |
# File 'lib/gin/mountable.rb', line 66 def default_route_for action raise NoMethodError, "The `#{__method__}' method must be defined on #{self} and return a\ 2 item Array with the HTTP verb and local path: ['GET', '/users/:id']" end |
#display_name(action = nil) ⇒ Object
Creates a display name for the Controller (and optional action). Used for logging and error messages.
Must return a String. Must be overloaded.
UserController.display_name :show
#=> "UserController#show"
95 96 97 98 99 |
# File 'lib/gin/mountable.rb', line 95 def display_name action=nil raise NoMethodError, "The `#{__method__}' method must be defined on #{self} and return a\ String for display purposes" end |
#route_name_for(action) ⇒ Object
Creates a route name used to identify a given route. Used by helper methods.
Must return a Symbol, or nil. Must be overloaded.
UserController.route_name_for :show
#=> :show_user
80 81 82 83 84 |
# File 'lib/gin/mountable.rb', line 80 def route_name_for action raise NoMethodError, "The `#{__method__}' method must be defined on #{self} and return a\ Symbol representing the route name: :show_user" end |
#verify_mount! ⇒ Object
:nodoc:
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/gin/mountable.rb', line 11 def verify_mount! #:nodoc: controller_name.to_str test_action = actions[0] test_route = default_route_for(test_action) test_route[0].to_str test_route[1].to_str route_name_for(test_action).to_proc display_name.to_str display_name(test_action).to_str true end |