Class: ROM::Command
- Inherits:
-
Object
- Object
- ROM::Command
- Extended by:
- Dry::Core::ClassAttributes, ClassInterface
- Includes:
- Commands
- Defined in:
- core/lib/rom/command.rb,
core/lib/rom/commands/class_interface.rb
Overview
Base command class with factory class-level interface and setup-related logic
Direct Known Subclasses
ROM::Commands::Create, ROM::Commands::Delete, ROM::Commands::Update
Defined Under Namespace
Modules: ClassInterface
Constant Summary collapse
- CommandType =
Types::Strict::Symbol.enum(:create, :update, :delete)
- Result =
Types::Strict::Symbol.enum(:one, :many)
Instance Attribute Summary collapse
-
#after(*hooks) ⇒ Command
readonly
Return a new command with appended after hooks.
-
#before(*hooks) ⇒ Command
readonly
Return a new command with appended before hooks.
-
#curry_args ⇒ Array
readonly
Curried args.
-
#input ⇒ Proc, #call
readonly
Tuple processing function, typically uses Relation#input_schema.
-
#relation ⇒ Relation
readonly
Command's relation.
-
#result ⇒ Symbol
readonly
Result type, either :one or :many.
-
#source ⇒ Relation
readonly
The source relation.
-
#type ⇒ Symbol
readonly
The command type, one of :create, :update or :delete.
Class Method Summary collapse
-
.[](adapter) ⇒ Class
extended
from ClassInterface
Return adapter specific sub-class based on the adapter identifier.
-
.adapter ⇒ Object
Get or set adapter identifier.
-
.after(*hooks) ⇒ Array<Hash, Symbol>
extended
from ClassInterface
Set after-execute hooks.
-
.before(*hooks) ⇒ Array<Hash, Symbol>
extended
from ClassInterface
Set before-execute hooks.
-
.build(relation, **options) ⇒ Command
extended
from ClassInterface
Build a command class for a specific relation with options.
-
.create_class(name, type) {|Class| ... } ⇒ Class, Object
extended
from ClassInterface
Create a command class with a specific type.
-
.extend_for_relation(relation) ⇒ Class
extended
from ClassInterface
Extend a command class with relation view methods.
-
.input ⇒ Object
Get or set input processing function.
-
.register_as ⇒ Object
Get or set identifier that should be used to register a command in a container.
-
.relation ⇒ Object
Get or set relation identifier.
- .restrictable ⇒ Object
-
.result ⇒ Object
Get or set result type.
-
.use(plugin, **options) ⇒ Object
extended
from ClassInterface
Use a configured plugin in this relation.
Instance Method Summary collapse
-
#after_hooks ⇒ Array
List of after hooks.
-
#before_hooks ⇒ Array
List of before hooks.
-
#call(*args, &block) ⇒ Object
(also: #[])
Call the command and return one or many tuples.
-
#combine(*others) ⇒ Command::Graph
Compose this command with other commands.
-
#curried? ⇒ TrueClass, FalseClass
Check if this command is curried.
-
#curry(*args) ⇒ Command, Lazy
Curry this command with provided args.
-
#gateway ⇒ Symbol
Return gateway of this command's relation.
-
#name ⇒ ROM::Relation::Name
Return name of this command's relation.
-
#new(new_relation) ⇒ Command
Return a new command with other source relation.
Instance Attribute Details
#after(*hooks) ⇒ Command (readonly)
Return a new command with appended after hooks
228 |
# File 'core/lib/rom/command.rb', line 228 option :after, Types::Coercible::Array, reader: false, default: -> { self.class.after } |
#before(*hooks) ⇒ Command (readonly)
Return a new command with appended before hooks
224 |
# File 'core/lib/rom/command.rb', line 224 option :before, Types::Coercible::Array, reader: false, default: -> { self.class.before } |
#curry_args ⇒ Array (readonly)
Returns Curried args.
220 |
# File 'core/lib/rom/command.rb', line 220 option :curry_args, default: -> { EMPTY_ARRAY } |
#input ⇒ Proc, #call (readonly)
Returns Tuple processing function, typically uses Relation#input_schema.
216 |
# File 'core/lib/rom/command.rb', line 216 option :input |
#relation ⇒ Relation (readonly)
Returns Command's relation.
197 |
# File 'core/lib/rom/command.rb', line 197 param :relation |
#result ⇒ Symbol (readonly)
Returns Result type, either :one or :many.
212 |
# File 'core/lib/rom/command.rb', line 212 option :result, type: Result |
#source ⇒ Relation (readonly)
Returns The source relation.
208 |
# File 'core/lib/rom/command.rb', line 208 option :source, default: -> { relation } |
#type ⇒ Symbol (readonly)
Returns The command type, one of :create, :update or :delete.
204 |
# File 'core/lib/rom/command.rb', line 204 option :type, type: CommandType, optional: true |
Class Method Details
.[](adapter) ⇒ Class Originally defined in module ClassInterface
Return adapter specific sub-class based on the adapter identifier
This is a syntax sugar to make things consistent
.adapter ⇒ Symbol .adapter(identifier) ⇒ Object
Get or set adapter identifier
59 |
# File 'core/lib/rom/command.rb', line 59 defines :adapter |
#after(hook) ⇒ Array<Hash, Symbol> #after(hook_opts) ⇒ Array<Hash, Symbol> Originally defined in module ClassInterface
Set after-execute hooks
#before(hook) ⇒ Array<Hash, Symbol> #before(hook_opts) ⇒ Array<Hash, Symbol> Originally defined in module ClassInterface
Set before-execute hooks
.build(relation, **options) ⇒ Command Originally defined in module ClassInterface
Build a command class for a specific relation with options
.create_class(name, type) {|Class| ... } ⇒ Class, Object Originally defined in module ClassInterface
Create a command class with a specific type
.extend_for_relation(relation) ⇒ Class Originally defined in module ClassInterface
Extend a command class with relation view methods
.input ⇒ Proc, #call .input(identifier) ⇒ Object
Get or set input processing function. This is typically set during setup to relation's input_schema
141 |
# File 'core/lib/rom/command.rb', line 141 defines :input |
.register_as ⇒ Symbol .register_as(identifier) ⇒ Object
Get or set identifier that should be used to register a command in a container
168 |
# File 'core/lib/rom/command.rb', line 168 defines :register_as |
.relation ⇒ Symbol .relation(identifier) ⇒ Object
Get or set relation identifier
86 |
# File 'core/lib/rom/command.rb', line 86 defines :relation |
.restrictable ⇒ FalseClass, TrueClass .restrictable(value) ⇒ Object
193 |
# File 'core/lib/rom/command.rb', line 193 defines :restrictable |
.result ⇒ Symbol .result(identifier) ⇒ Object
Get or set result type
113 |
# File 'core/lib/rom/command.rb', line 113 defines :result |
.use(plugin, **options) ⇒ Object Originally defined in module ClassInterface
Use a configured plugin in this relation
Instance Method Details
#after_hooks ⇒ Array
List of after hooks
377 378 379 |
# File 'core/lib/rom/command.rb', line 377 def after_hooks [:after] end |
#before_hooks ⇒ Array
List of before hooks
368 369 370 |
# File 'core/lib/rom/command.rb', line 368 def before_hooks [:before] end |
#call(*args, &block) ⇒ Object Also known as: []
Call the command and return one or many tuples
This method will apply before/after hooks automatically
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'core/lib/rom/command.rb', line 270 def call(*args, &block) tuples = if hooks? prepared = if curried? apply_hooks(before_hooks, *(curry_args + args)) else apply_hooks(before_hooks, *args) end result = prepared ? execute(prepared, &block) : execute(&block) if curried? if !args.empty? apply_hooks(after_hooks, result, *args) elsif curry_args.size > 1 apply_hooks(after_hooks, result, curry_args[1]) else apply_hooks(after_hooks, result) end else apply_hooks(after_hooks, result, *args[1..args.size - 1]) end else execute(*(curry_args + args), &block) end if one? tuples.first else tuples end end |
#combine(*others) ⇒ Command::Graph
Compose this command with other commands
Composed commands can handle nested input
328 329 330 |
# File 'core/lib/rom/command.rb', line 328 def combine(*others) Graph.new(self, others) end |
#curried? ⇒ TrueClass, FalseClass
Check if this command is curried
337 338 339 |
# File 'core/lib/rom/command.rb', line 337 def curried? !curry_args.empty? end |
#curry(*args) ⇒ Command, Lazy
Curry this command with provided args
Curried command can be called without args. If argument is a graph input processor, lazy command will be returned, which is used for handling nested input hashes.
313 314 315 316 317 318 319 |
# File 'core/lib/rom/command.rb', line 313 def curry(*args) if curry_args.empty? && args.first.is_a?(Graph::InputEvaluator) Lazy[self].new(self, *args) else self.class.build(relation, **, curry_args: args) end end |
#gateway ⇒ Symbol
Return gateway of this command's relation
247 248 249 |
# File 'core/lib/rom/command.rb', line 247 def gateway relation.gateway end |
#name ⇒ ROM::Relation::Name
Return name of this command's relation
238 239 240 |
# File 'core/lib/rom/command.rb', line 238 def name relation.name end |
#new(new_relation) ⇒ Command
Return a new command with other source relation
This can be used to restrict command with a specific relation
388 389 390 |
# File 'core/lib/rom/command.rb', line 388 def new(new_relation) self.class.build(new_relation, **, source: relation) end |