Class: Cuprum::Rails::Routes
- Inherits:
-
Object
- Object
- Cuprum::Rails::Routes
- Defined in:
- lib/cuprum/rails/routes.rb
Overview
Represent the routes available for a given resource.
Direct Known Subclasses
Cuprum::Rails::Routing::PluralRoutes, Cuprum::Rails::Routing::SingularRoutes
Defined Under Namespace
Classes: MissingWildcardError
Instance Attribute Summary collapse
-
#base_path ⇒ String
readonly
The relative path of the resource.
-
#wildcards ⇒ String
readonly
The url wildcards for the resource.
Class Method Summary collapse
-
.route(action_name, path) ⇒ Object
Defines a route for the resource routes.
Instance Method Summary collapse
-
#initialize(base_path:, parent_path: nil, &block) ⇒ Routes
constructor
A new instance of Routes.
-
#parent_path ⇒ String
The path to the parent resource index.
-
#root_path ⇒ String
The root path for the application.
-
#with_wildcards(wildcards) ⇒ Cuprum::Rails::Routes
A copy of the routes with the wildcards.
Constructor Details
#initialize(base_path:, parent_path: nil, &block) ⇒ Routes
Returns a new instance of Routes.
80 81 82 83 84 85 86 |
# File 'lib/cuprum/rails/routes.rb', line 80 def initialize(base_path:, parent_path: nil, &block) @base_path = base_path @parent_path = parent_path @wildcards = {} singleton_class.instance_exec(&block) if block_given? end |
Instance Attribute Details
#base_path ⇒ String (readonly)
Returns the relative path of the resource.
89 90 91 |
# File 'lib/cuprum/rails/routes.rb', line 89 def base_path @base_path end |
#wildcards ⇒ String (readonly)
Returns the url wildcards for the resource.
92 93 94 |
# File 'lib/cuprum/rails/routes.rb', line 92 def wildcards @wildcards end |
Class Method Details
.route(action_name, path) ⇒ Object
Defines a route for the resource routes.
Each defined route creates a helper method on the routes, which returns the full path of the resource route. A member action helper (if the path ends in an :id wildcard) will require passing in either the primary key of the member or the member entity.
If the base path includes wildcards, then the wildcards must be set (using #with_wildcards) before calling a route helper.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/cuprum/rails/routes.rb', line 26 def route(action_name, path) validate_action_name!(action_name) validate_path!(path) if member_route?(path) define_member_route(action_name, path) else define_collection_route(action_name, path) end end |
Instance Method Details
#parent_path ⇒ String
Returns the path to the parent resource index.
95 96 97 98 99 |
# File 'lib/cuprum/rails/routes.rb', line 95 def parent_path return '/' if @parent_path.nil? insert_wildcards(@parent_path) end |
#root_path ⇒ String
Returns the root path for the application.
102 103 104 |
# File 'lib/cuprum/rails/routes.rb', line 102 def root_path '/' end |
#with_wildcards(wildcards) ⇒ Cuprum::Rails::Routes
Returns a copy of the routes with the wildcards.
109 110 111 112 113 114 115 |
# File 'lib/cuprum/rails/routes.rb', line 109 def with_wildcards(wildcards) unless wildcards.is_a?(Hash) raise ArgumentError, 'wildcards must be a Hash' end clone.apply_wildcards(wildcards) end |