Class: ROM::Command
- Inherits:
-
Object
- Object
- ROM::Command
- Extended by:
- Dry::Core::ClassAttributes, ClassInterface, Initializer
- Includes:
- Commands, Pipeline::Operator
- Defined in:
- lib/rom/command.rb,
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 ⇒ Object
Get or set adapter identifier.
-
.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.
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.
-
#execute ⇒ Array
abstract
private
Execute the command.
-
#gateway ⇒ Symbol
Return gateway of this command’s relation.
-
#graph? ⇒ false
private
Check if this command is a graph.
-
#hooks? ⇒ Boolean
private
Check if this command has any hooks.
-
#lazy? ⇒ false
private
Check if this command is lazy.
-
#many? ⇒ TrueClass, FalseClass
private
Check if this command returns many tuples.
-
#map_input_tuples(tuples, &mapper) ⇒ Object
private
Yields tuples for insertion or return an enumerator.
-
#name ⇒ ROM::Relation::Name
Return name of this command’s relation.
-
#new(new_relation) ⇒ Command
Return a new command with other source relation.
-
#one? ⇒ TrueClass, FalseClass
private
Check if this command returns a single tuple.
-
#restrictible? ⇒ TrueClass, FalseClass
private
Check if this command is restrictible through relation.
Methods included from Initializer
Methods included from ClassInterface
adapter_namespace, build, create_class, default_name, extend_for_relation, extended, inherited, options, relation_methods_mod, set_hooks, use
Methods included from Pipeline::Operator
Instance Attribute Details
#after(*hooks) ⇒ Command (readonly)
Return a new command with appended after hooks
230 |
# File 'lib/rom/command.rb', line 230 option :after, Types::Coercible::Array, reader: false, default: -> { self.class.after } |
#before(*hooks) ⇒ Command (readonly)
Return a new command with appended before hooks
226 |
# File 'lib/rom/command.rb', line 226 option :before, Types::Coercible::Array, reader: false, default: -> { self.class.before } |
#curry_args ⇒ Array (readonly)
Returns Curried args.
222 |
# File 'lib/rom/command.rb', line 222 option :curry_args, default: -> { EMPTY_ARRAY } |
#input ⇒ Proc, #call (readonly)
Returns Tuple processing function, typically uses Relation#input_schema.
218 |
# File 'lib/rom/command.rb', line 218 option :input |
#relation ⇒ Relation (readonly)
Returns Command’s relation.
199 |
# File 'lib/rom/command.rb', line 199 param :relation |
#result ⇒ Symbol (readonly)
Returns Result type, either :one or :many.
214 |
# File 'lib/rom/command.rb', line 214 option :result, type: Result |
#source ⇒ Relation (readonly)
Returns The source relation.
210 |
# File 'lib/rom/command.rb', line 210 option :source, default: -> { relation } |
#type ⇒ Symbol (readonly)
Returns The command type, one of :create, :update or :delete.
206 |
# File 'lib/rom/command.rb', line 206 option :type, type: CommandType, optional: true |
Class Method Details
.adapter ⇒ Symbol .adapter(identifier) ⇒ Object
Get or set adapter identifier
61 |
# File 'lib/rom/command.rb', line 61 defines :adapter |
.input ⇒ Proc, #call .input(identifier) ⇒ Object
Get or set input processing function. This is typically set during setup to relation’s input_schema
143 |
# File 'lib/rom/command.rb', line 143 defines :input |
.register_as ⇒ Symbol .register_as(identifier) ⇒ Object
Get or set identifier that should be used to register a command in a container
170 |
# File 'lib/rom/command.rb', line 170 defines :register_as |
.relation ⇒ Symbol .relation(identifier) ⇒ Object
Get or set relation identifier
88 |
# File 'lib/rom/command.rb', line 88 defines :relation |
.restrictable ⇒ FalseClass, TrueClass .restrictable(value) ⇒ Object
195 |
# File 'lib/rom/command.rb', line 195 defines :restrictable |
.result ⇒ Symbol .result(identifier) ⇒ Object
Get or set result type
115 |
# File 'lib/rom/command.rb', line 115 defines :result |
Instance Method Details
#after_hooks ⇒ Array
List of after hooks
379 380 381 |
# File 'lib/rom/command.rb', line 379 def after_hooks [:after] end |
#before_hooks ⇒ Array
List of before hooks
370 371 372 |
# File 'lib/rom/command.rb', line 370 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
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 303 304 |
# File 'lib/rom/command.rb', line 272 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.size > 0 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
330 331 332 |
# File 'lib/rom/command.rb', line 330 def combine(*others) Graph.new(self, others) end |
#curried? ⇒ TrueClass, FalseClass
Check if this command is curried
339 340 341 |
# File 'lib/rom/command.rb', line 339 def curried? curry_args.size > 0 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.
315 316 317 318 319 320 321 |
# File 'lib/rom/command.rb', line 315 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 |
#execute ⇒ Array
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Execute the command
260 261 262 263 264 265 |
# File 'lib/rom/command.rb', line 260 def execute(*) raise( NotImplementedError, "#{self.class}##{__method__} must be implemented" ) end |
#gateway ⇒ Symbol
Return gateway of this command’s relation
249 250 251 |
# File 'lib/rom/command.rb', line 249 def gateway relation.gateway end |
#graph? ⇒ false
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if this command is a graph
415 416 417 |
# File 'lib/rom/command.rb', line 415 def graph? false end |
#hooks? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if this command has any hooks
397 398 399 |
# File 'lib/rom/command.rb', line 397 def hooks? before_hooks.size > 0 || after_hooks.size > 0 end |
#lazy? ⇒ false
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if this command is lazy
406 407 408 |
# File 'lib/rom/command.rb', line 406 def lazy? false end |
#many? ⇒ TrueClass, FalseClass
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if this command returns many tuples
433 434 435 |
# File 'lib/rom/command.rb', line 433 def many? result.equal?(:many) end |
#map_input_tuples(tuples, &mapper) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Yields tuples for insertion or return an enumerator
449 450 451 452 453 454 455 456 457 |
# File 'lib/rom/command.rb', line 449 def map_input_tuples(tuples, &mapper) return enum_for(:with_input_tuples, tuples) unless mapper if tuples.respond_to? :merge mapper[tuples] else tuples.map(&mapper) end end |
#name ⇒ ROM::Relation::Name
Return name of this command’s relation
240 241 242 |
# File 'lib/rom/command.rb', line 240 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
390 391 392 |
# File 'lib/rom/command.rb', line 390 def new(new_relation) self.class.build(new_relation, {**, source: relation}) end |
#one? ⇒ TrueClass, FalseClass
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if this command returns a single tuple
424 425 426 |
# File 'lib/rom/command.rb', line 424 def one? result.equal?(:one) end |
#restrictible? ⇒ TrueClass, FalseClass
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if this command is restrictible through relation
442 443 444 |
# File 'lib/rom/command.rb', line 442 def restrictible? self.class.restrictable.equal?(true) end |