Class: MuchRails::Action::BaseRouter
- Inherits:
-
Object
- Object
- MuchRails::Action::BaseRouter
- Defined in:
- lib/much-rails/action/base_router.rb
Direct Known Subclasses
Defined Under Namespace
Classes: BaseURL, Definition, RequestType, RequestTypeAction, RequestTypeSet, URLSet
Constant Summary collapse
- DEFAULT_BASE_URL =
"/"
Instance Attribute Summary collapse
-
#definitions ⇒ Object
readonly
Returns the value of attribute definitions.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#request_type_set ⇒ Object
readonly
Returns the value of attribute request_type_set.
-
#url_set ⇒ Object
readonly
Returns the value of attribute url_set.
Class Method Summary collapse
-
.url_class ⇒ Object
Override as needed.
Instance Method Summary collapse
- #apply_to(to_scope) ⇒ Object
-
#base_url(value = nil) ⇒ Object
Example: AdminRouter = MuchRails::Action::Router.new(:admin) { base_url “/admin” url :users, “/users”, “Users::Index” } AdminRouter.path_for(:users) # => “/admin/users”.
- #delete(path, default_class_name = nil, called_from: caller, **request_type_class_names) ⇒ Object
-
#get(path, default_class_name = nil, called_from: caller, **request_type_class_names) ⇒ Object
Example: MyRouter = MuchRails::Action::Router.new { get “/”, “Root::Index” get “/new”, “Root::New” post “/”, “Root::Create” get “/edit”, “Root::Edit” put “/”, “Root::Upsert” patch “/”, “Root::Update” get “/remove”, “Root::Remove” delete “/”, “Root::Destroy” }.
-
#initialize(name = nil, &block) ⇒ BaseRouter
constructor
A new instance of BaseRouter.
- #patch(path, default_class_name = nil, called_from: caller, **request_type_class_names) ⇒ Object
-
#path_for(name, **kargs) ⇒ Object
Example: MyRouter = MuchRails::Action::Router.new { url :root, “/”, “Root” } MyRouter.path_for(:root) # => “/”.
- #post(path, default_class_name = nil, called_from: caller, **request_type_class_names) ⇒ Object
- #put(path, default_class_name = nil, called_from: caller, **request_type_class_names) ⇒ Object
-
#request_type(name, &constraints_lambda) ⇒ Object
Example: MyRouter = MuchRails::Action::Router.new { request_type(:mobile) do |request| mobile_user_agent?(request.user_agent) end.
- #unrouted_urls ⇒ Object
-
#url(name, path) ⇒ Object
Example: MyRouter = MuchRails::Action::Router.new { url :root, “/” get :root, “Root::Index” }.
- #url_class ⇒ Object
-
#url_for(name, **kargs) ⇒ Object
Example: MyRouter = MuchRails::Action::Router.new { url :root, “/”, “Root” } MyRouter.url_for(:root) # => “example.org/”.
- #validate! ⇒ Object
Constructor Details
#initialize(name = nil, &block) ⇒ BaseRouter
Returns a new instance of BaseRouter.
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/much-rails/action/base_router.rb', line 17 def initialize(name = nil, &block) @name = name @request_type_set = RequestTypeSet.new @url_set = URLSet.new(self) @definitions = [] @defined_urls = [] @base_url = DEFAULT_BASE_URL instance_exec(&(block || proc{})) end |
Instance Attribute Details
#definitions ⇒ Object (readonly)
Returns the value of attribute definitions.
15 16 17 |
# File 'lib/much-rails/action/base_router.rb', line 15 def definitions @definitions end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
14 15 16 |
# File 'lib/much-rails/action/base_router.rb', line 14 def name @name end |
#request_type_set ⇒ Object (readonly)
Returns the value of attribute request_type_set.
15 16 17 |
# File 'lib/much-rails/action/base_router.rb', line 15 def request_type_set @request_type_set end |
#url_set ⇒ Object (readonly)
Returns the value of attribute url_set.
15 16 17 |
# File 'lib/much-rails/action/base_router.rb', line 15 def url_set @url_set end |
Class Method Details
.url_class ⇒ Object
Override as needed.
10 11 12 |
# File 'lib/much-rails/action/base_router.rb', line 10 def self.url_class MuchRails::Action::BaseRouter::BaseURL end |
Instance Method Details
#apply_to(to_scope) ⇒ Object
56 57 58 |
# File 'lib/much-rails/action/base_router.rb', line 56 def apply_to(to_scope) raise NotImplementedError end |
#base_url(value = nil) ⇒ Object
116 117 118 119 |
# File 'lib/much-rails/action/base_router.rb', line 116 def base_url(value = nil) @base_url = value unless value.nil? @base_url end |
#delete(path, default_class_name = nil, called_from: caller, **request_type_class_names) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/much-rails/action/base_router.rb', line 213 def delete( path, default_class_name = nil, called_from: caller, **request_type_class_names) route( :delete, path, default_class_name, called_from: called_from, **request_type_class_names, ) end |
#get(path, default_class_name = nil, called_from: caller, **request_type_class_names) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/much-rails/action/base_router.rb', line 157 def get( path, default_class_name = nil, called_from: caller, **request_type_class_names) route( :get, path, default_class_name, called_from: called_from, **request_type_class_names, ) end |
#patch(path, default_class_name = nil, called_from: caller, **request_type_class_names) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/much-rails/action/base_router.rb', line 199 def patch( path, default_class_name = nil, called_from: caller, **request_type_class_names) route( :patch, path, default_class_name, called_from: called_from, **request_type_class_names, ) end |
#path_for(name, **kargs) ⇒ Object
73 74 75 |
# File 'lib/much-rails/action/base_router.rb', line 73 def path_for(name, **kargs) @url_set.path_for(name, **kargs) end |
#post(path, default_class_name = nil, called_from: caller, **request_type_class_names) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/much-rails/action/base_router.rb', line 171 def post( path, default_class_name = nil, called_from: caller, **request_type_class_names) route( :post, path, default_class_name, called_from: called_from, **request_type_class_names, ) end |
#put(path, default_class_name = nil, called_from: caller, **request_type_class_names) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/much-rails/action/base_router.rb', line 185 def put( path, default_class_name = nil, called_from: caller, **request_type_class_names) route( :put, path, default_class_name, called_from: called_from, **request_type_class_names, ) end |
#request_type(name, &constraints_lambda) ⇒ Object
105 106 107 |
# File 'lib/much-rails/action/base_router.rb', line 105 def request_type(name, &constraints_lambda) @request_type_set.add(name, constraints_lambda) end |
#unrouted_urls ⇒ Object
32 33 34 |
# File 'lib/much-rails/action/base_router.rb', line 32 def unrouted_urls @url_set.urls - @defined_urls end |
#url(name, path) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/much-rails/action/base_router.rb', line 127 def url(name, path) unless name.is_a?(::Symbol) raise( ArgumentError, "Named URLs must be defined with Symbol names, "\ "given `#{name.inspect}`.", ) end unless path.is_a?(::String) raise( ArgumentError, "Named URLs must be defined with String paths, "\ "given `#{path.inspect}`.", ) end @url_set.add(name, path) end |
#url_class ⇒ Object
28 29 30 |
# File 'lib/much-rails/action/base_router.rb', line 28 def url_class self.class.url_class end |
#url_for(name, **kargs) ⇒ Object
Example:
MyRouter =
MuchRails::Action::Router.new {
url :root, "/", "Root"
}
MyRouter.url_for(:root) # => "http://example.org/"
AdminRouter =
MuchRails::Action::Router.new(:admin) {
base_url "/admin"
url :users, "/users", "Users::Index"
}
AdminRouter.url_for(:users) # => "http://example.org/admin/users"
90 91 92 |
# File 'lib/much-rails/action/base_router.rb', line 90 def url_for(name, **kargs) @url_set.url_for(name, **kargs) end |
#validate! ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/much-rails/action/base_router.rb', line 36 def validate! definitions.each do |definition| definition.request_type_actions.each do |request_type_action| begin request_type_action.class_name.constantize rescue NameError => ex raise(NameError, ex., definition.called_from, cause: ex) end end next unless definition.has_default_action_class_name? begin definition.default_action_class_name.constantize rescue NameError => ex raise(NameError, ex., definition.called_from, cause: ex) end end end |