Module: Vedeu::Runtime::Router
Overview
Stores all client application controllers with their respective actions.
Class Method Summary collapse
-
.absent?(variable) ⇒ Boolean
extended
from Common
Returns a boolean indicating whether a variable is nil or empty.
-
.action_defined?(action, controller) ⇒ Boolean
private
Returns a boolean indicating whether the given action name is defined for the given controller.
-
.add_action(controller, action) ⇒ void
Registers an action to the given controller name for the client application.
-
.add_controller(controller, klass) ⇒ void
Registers a controller with the given controller name for the client application.
-
.demodulize(klass) ⇒ String
extended
from Common
Removes the module part from the expression in the string.
-
.goto(controller, action, **args) ⇒ void
(also: #action)
Instruct Vedeu to load the client application controller action with parameters.
-
.in_memory ⇒ Hash<void>
private
Returns an empty collection ready for the storing of client application controllers and actions.
-
.klass_defined?(controller) ⇒ Boolean
private
Returns a boolean indicating whether the given controller name has a class defined.
-
.klass_for(controller) ⇒ String
private
Fetch the class for the controller by name.
-
.present?(variable) ⇒ Boolean
extended
from Common
Returns a boolean indicating whether a variable has a useful value.
-
.registered?(controller) ⇒ Boolean
Returns a boolean indicating whether the given controller name is already registered.
-
.reset! ⇒ Hash<void>
(also: #reset)
Removes all stored controllers with their respective actions.
-
.route(controller, action, **args) ⇒ void
private
Instantiate the given controller by name, the call the action (method) with any given arguments.
-
.snake_case(name) ⇒ String
extended
from Common
Converts a class name to a lowercase snake case string.
-
.storage ⇒ Hash<Symbol => Hash<Symbol => String|Array<Symbol>>>
private
Returns all the stored controllers and their respective actions.
Instance Method Summary collapse
-
#absent?(variable) ⇒ Boolean
included
from Common
Returns a boolean indicating whether a variable is nil or empty.
-
#action_defined?(action, controller) ⇒ Boolean
private
Returns a boolean indicating whether the given action name is defined for the given controller.
-
#add_action(controller, action) ⇒ void
Registers an action to the given controller name for the client application.
-
#add_controller(controller, klass) ⇒ void
Registers a controller with the given controller name for the client application.
-
#demodulize(klass) ⇒ String
included
from Common
Removes the module part from the expression in the string.
-
#goto(controller, action, **args) ⇒ void
(also: #action)
Instruct Vedeu to load the client application controller action with parameters.
-
#in_memory ⇒ Hash<void>
private
Returns an empty collection ready for the storing of client application controllers and actions.
-
#klass_defined?(controller) ⇒ Boolean
private
Returns a boolean indicating whether the given controller name has a class defined.
-
#klass_for(controller) ⇒ String
private
Fetch the class for the controller by name.
-
#present?(variable) ⇒ Boolean
included
from Common
Returns a boolean indicating whether a variable has a useful value.
-
#registered?(controller) ⇒ Boolean
Returns a boolean indicating whether the given controller name is already registered.
-
#reset! ⇒ Hash<void>
(also: #reset)
Removes all stored controllers with their respective actions.
-
#route(controller, action, **args) ⇒ void
private
Instantiate the given controller by name, the call the action (method) with any given arguments.
-
#snake_case(name) ⇒ String
included
from Common
Converts a class name to a lowercase snake case string.
-
#storage ⇒ Hash<Symbol => Hash<Symbol => String|Array<Symbol>>>
private
Returns all the stored controllers and their respective actions.
Class Method Details
.absent?(variable) ⇒ Boolean Originally defined in module Common
Returns a boolean indicating whether a variable is nil or empty.
.action_defined?(action, controller) ⇒ Boolean (private)
Returns a boolean indicating whether the given action name is defined for the given controller.
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/vedeu/runtime/router.rb', line 120 def action_defined?(action, controller) if registered?(controller) return true if storage[controller][:actions].include?(action) fail Vedeu::Error::ActionNotFound, "#{action} is not registered for #{controller}.".freeze else fail Vedeu::Error::ControllerNotFound, "#{controller} is not registered.".freeze end end |
.add_action(controller, action) ⇒ void
This method returns an undefined value.
Registers an action to the given controller name for the client application.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/vedeu/runtime/router.rb', line 50 def add_action(controller, action) if present?(controller) && present?(action) Vedeu.log(type: :create, message: "Action: ':#{action}' " \ "(for ':#{controller}')".freeze) if registered?(controller) storage[controller][:actions] << action else add_controller(controller, '') add_action(controller, action) end storage else fail Vedeu::Error::MissingRequired, 'Cannot store action without a controller or name ' \ 'attribute.'.freeze end end |
.add_controller(controller, klass) ⇒ void
This method returns an undefined value.
Registers a controller with the given controller name for the client application.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/vedeu/runtime/router.rb', line 22 def add_controller(controller, klass) unless present?(controller) fail Vedeu::Error::MissingRequired, 'Cannot store controller without a name attribute.'.freeze end Vedeu.log(type: :create, message: "Controller: ':#{controller}'.freeze") if registered?(controller) storage[controller].merge!(klass: klass) else storage.store(controller, klass: klass, actions: []) end storage end |
.demodulize(klass) ⇒ String Originally defined in module Common
Removes the module part from the expression in the string.
.goto(controller, action, **args) ⇒ void Also known as: action
This method returns an undefined value.
Instruct Vedeu to load the client application controller action with parameters.
87 88 89 90 91 92 |
# File 'lib/vedeu/runtime/router.rb', line 87 def goto(controller, action, **args) Vedeu.log(type: :debug, message: "Routing: #{controller} #{action}".freeze) route(controller, action, args) if action_defined?(action, controller) end |
.in_memory ⇒ Hash<void> (private)
Returns an empty collection ready for the storing of client application controllers and actions.
184 185 186 |
# File 'lib/vedeu/runtime/router.rb', line 184 def in_memory {} end |
.klass_defined?(controller) ⇒ Boolean (private)
Returns a boolean indicating whether the given controller name has a class defined.
168 169 170 |
# File 'lib/vedeu/runtime/router.rb', line 168 def klass_defined?(controller) present?(storage[controller][:klass]) end |
.klass_for(controller) ⇒ String (private)
Fetch the class for the controller by name.
152 153 154 155 156 157 158 159 160 161 |
# File 'lib/vedeu/runtime/router.rb', line 152 def klass_for(controller) if registered?(controller) && klass_defined?(controller) storage[controller][:klass] else fail Vedeu::Error::MissingRequired, "Cannot route to #{controller} as no class defined.".freeze end end |
.present?(variable) ⇒ Boolean Originally defined in module Common
Returns a boolean indicating whether a variable has a useful value.
.registered?(controller) ⇒ Boolean
Returns a boolean indicating whether the given controller name is already registered.
100 101 102 |
# File 'lib/vedeu/runtime/router.rb', line 100 def registered?(controller) storage.key?(controller) end |
.reset! ⇒ Hash<void> Also known as: reset
Removes all stored controllers with their respective actions.
107 108 109 |
# File 'lib/vedeu/runtime/router.rb', line 107 def reset! @storage = in_memory end |
.route(controller, action, **args) ⇒ void (private)
This method returns an undefined value.
Instantiate the given controller by name, the call the action (method) with any given arguments.
141 142 143 144 |
# File 'lib/vedeu/runtime/router.rb', line 141 def route(controller, action, **args) klass = Object.const_get(klass_for(controller)).new(**args) klass.send(action) end |
.snake_case(name) ⇒ String Originally defined in module Common
Converts a class name to a lowercase snake case string.
.storage ⇒ Hash<Symbol => Hash<Symbol => String|Array<Symbol>>> (private)
Returns all the stored controllers and their respective actions.
176 177 178 |
# File 'lib/vedeu/runtime/router.rb', line 176 def storage @storage ||= in_memory end |
Instance Method Details
#absent?(variable) ⇒ Boolean Originally defined in module Common
Returns a boolean indicating whether a variable is nil or empty.
#action_defined?(action, controller) ⇒ Boolean (private)
Returns a boolean indicating whether the given action name is defined for the given controller.
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/vedeu/runtime/router.rb', line 120 def action_defined?(action, controller) if registered?(controller) return true if storage[controller][:actions].include?(action) fail Vedeu::Error::ActionNotFound, "#{action} is not registered for #{controller}.".freeze else fail Vedeu::Error::ControllerNotFound, "#{controller} is not registered.".freeze end end |
#add_action(controller, action) ⇒ void
This method returns an undefined value.
Registers an action to the given controller name for the client application.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/vedeu/runtime/router.rb', line 50 def add_action(controller, action) if present?(controller) && present?(action) Vedeu.log(type: :create, message: "Action: ':#{action}' " \ "(for ':#{controller}')".freeze) if registered?(controller) storage[controller][:actions] << action else add_controller(controller, '') add_action(controller, action) end storage else fail Vedeu::Error::MissingRequired, 'Cannot store action without a controller or name ' \ 'attribute.'.freeze end end |
#add_controller(controller, klass) ⇒ void
This method returns an undefined value.
Registers a controller with the given controller name for the client application.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/vedeu/runtime/router.rb', line 22 def add_controller(controller, klass) unless present?(controller) fail Vedeu::Error::MissingRequired, 'Cannot store controller without a name attribute.'.freeze end Vedeu.log(type: :create, message: "Controller: ':#{controller}'.freeze") if registered?(controller) storage[controller].merge!(klass: klass) else storage.store(controller, klass: klass, actions: []) end storage end |
#demodulize(klass) ⇒ String Originally defined in module Common
Removes the module part from the expression in the string.
#goto(controller, action, **args) ⇒ void Also known as: action
This method returns an undefined value.
Instruct Vedeu to load the client application controller action with parameters.
87 88 89 90 91 92 |
# File 'lib/vedeu/runtime/router.rb', line 87 def goto(controller, action, **args) Vedeu.log(type: :debug, message: "Routing: #{controller} #{action}".freeze) route(controller, action, args) if action_defined?(action, controller) end |
#in_memory ⇒ Hash<void> (private)
Returns an empty collection ready for the storing of client application controllers and actions.
184 185 186 |
# File 'lib/vedeu/runtime/router.rb', line 184 def in_memory {} end |
#klass_defined?(controller) ⇒ Boolean (private)
Returns a boolean indicating whether the given controller name has a class defined.
168 169 170 |
# File 'lib/vedeu/runtime/router.rb', line 168 def klass_defined?(controller) present?(storage[controller][:klass]) end |
#klass_for(controller) ⇒ String (private)
Fetch the class for the controller by name.
152 153 154 155 156 157 158 159 160 161 |
# File 'lib/vedeu/runtime/router.rb', line 152 def klass_for(controller) if registered?(controller) && klass_defined?(controller) storage[controller][:klass] else fail Vedeu::Error::MissingRequired, "Cannot route to #{controller} as no class defined.".freeze end end |
#present?(variable) ⇒ Boolean Originally defined in module Common
Returns a boolean indicating whether a variable has a useful value.
#registered?(controller) ⇒ Boolean
Returns a boolean indicating whether the given controller name is already registered.
100 101 102 |
# File 'lib/vedeu/runtime/router.rb', line 100 def registered?(controller) storage.key?(controller) end |
#reset! ⇒ Hash<void> Also known as: reset
Removes all stored controllers with their respective actions.
107 108 109 |
# File 'lib/vedeu/runtime/router.rb', line 107 def reset! @storage = in_memory end |
#route(controller, action, **args) ⇒ void (private)
This method returns an undefined value.
Instantiate the given controller by name, the call the action (method) with any given arguments.
141 142 143 144 |
# File 'lib/vedeu/runtime/router.rb', line 141 def route(controller, action, **args) klass = Object.const_get(klass_for(controller)).new(**args) klass.send(action) end |
#snake_case(name) ⇒ String Originally defined in module Common
Converts a class name to a lowercase snake case string.
#storage ⇒ Hash<Symbol => Hash<Symbol => String|Array<Symbol>>> (private)
Returns all the stored controllers and their respective actions.
176 177 178 |
# File 'lib/vedeu/runtime/router.rb', line 176 def storage @storage ||= in_memory end |