Class: ROM::Command

Inherits:
Object
  • Object
show all
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

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Initializer

extended

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

Parameters:

  • hooks (Array<Hash>)

    A list of after hooks configurations

Returns:



226
# File 'lib/rom/command.rb', line 226

option :after, Types::Coercible::Array, reader: false, default: -> { self.class.after }

#before(*hooks) ⇒ Command (readonly)

Return a new command with appended before hooks

Parameters:

  • hooks (Array<Hash>)

    A list of before hooks configurations

Returns:



222
# File 'lib/rom/command.rb', line 222

option :before, Types::Coercible::Array, reader: false, default: -> { self.class.before }

#curry_argsArray (readonly)

Returns Curried args.

Returns:

  • (Array)

    Curried args



218
# File 'lib/rom/command.rb', line 218

option :curry_args, default: -> { EMPTY_ARRAY }

#inputProc, #call (readonly)

Returns Tuple processing function, typically uses Relation#input_schema.

Returns:

  • (Proc, #call)

    Tuple processing function, typically uses Relation#input_schema



214
# File 'lib/rom/command.rb', line 214

option :input

#relationRelation (readonly)

Returns Command’s relation.

Returns:



195
# File 'lib/rom/command.rb', line 195

param :relation

#resultSymbol (readonly)

Returns Result type, either :one or :many.

Returns:

  • (Symbol)

    Result type, either :one or :many



210
# File 'lib/rom/command.rb', line 210

option :result, type: Result

#sourceRelation (readonly)

Returns The source relation.

Returns:



206
# File 'lib/rom/command.rb', line 206

option :source, default: -> { relation }

#typeSymbol (readonly)

Returns The command type, one of :create, :update or :delete.

Returns:

  • (Symbol)

    The command type, one of :create, :update or :delete



202
# File 'lib/rom/command.rb', line 202

option :type, type: CommandType, optional: true

Class Method Details

.adapterSymbol .adapter(identifier) ⇒ Object

Get or set adapter identifier

Overloads:

  • .adapterSymbol

    Get adapter identifier

    Examples:

    ROM::Memory::Commands::Create.adapter
    # => :memory
    

    Returns:

    • (Symbol)
  • .adapter(identifier) ⇒ Object

    Set adapter identifier. This must always match actual adapter identifier that was used to register an adapter.

    Examples:

    module MyAdapter
      class CreateCommand < ROM::Commands::Memory::Create
        adapter :my_adapter
      end
    end
    


57
# File 'lib/rom/command.rb', line 57

defines :adapter

.inputProc, #call .input(identifier) ⇒ Object

Get or set input processing function. This is typically set during setup to relation’s input_schema

Overloads:

  • .inputProc, #call

    Get input processing function

    Examples:

    class CreateUser < ROM::Commands::Create[:memory]
      input -> tuple { .. }
    end
    
    CreateUser.input
    # Your custom function
    

    Returns:

  • .input(identifier) ⇒ Object

    Set input processing function

    Examples:

    class CreateUser < ROM::Commands::Create[:memory]
      input -> tuple { .. }
    end
    


139
# File 'lib/rom/command.rb', line 139

defines :input

.register_asSymbol .register_as(identifier) ⇒ Object

Get or set identifier that should be used to register a command in a container

Overloads:

  • .register_asSymbol

    Get registration identifier

    Examples:

    class CreateUser < ROM::Commands::Create[:memory]
      register_as :create_user
    end
    
    CreateUser.register_as
    # => :create_user
    

    Returns:

    • (Symbol)
  • .register_as(identifier) ⇒ Object

    Set registration identifier

    Examples:

    class CreateUser < ROM::Commands::Create[:memory]
      register_as :create_user
    end
    


166
# File 'lib/rom/command.rb', line 166

defines :register_as

.relationSymbol .relation(identifier) ⇒ Object

Get or set relation identifier

Overloads:

  • .relationSymbol

    Get relation identifier

    Examples:

    class CreateUser < ROM::Commands::Create[:memory]
      relation :users
    end
    
    CreateUser.relation
    # => :users
    

    Returns:

    • (Symbol)
  • .relation(identifier) ⇒ Object

    Set relation identifier.

    Examples:

    class CreateUser < ROM::Commands::Create[:memory]
      relation :users
    end
    


84
# File 'lib/rom/command.rb', line 84

defines :relation

.restrictableFalseClass, TrueClass .restrictable(value) ⇒ Object

Overloads:

  • .restrictableFalseClass, TrueClass

    Check if a command class is restrictable

    Examples:

    class UpdateUser < ROM::Commands::Update[:memory]
      restrictable true
    end
    
    CreateUser.restrictable
    # => true
    

    Returns:

    • (FalseClass, TrueClass)
  • .restrictable(value) ⇒ Object

    Set if a command is restrictable

    Examples:

    class UpdateUser < ROM::Commands::Update[:memory]
      restrictable true
    end
    


191
# File 'lib/rom/command.rb', line 191

defines :restrictable

.resultSymbol .result(identifier) ⇒ Object

Get or set result type

Overloads:

  • .resultSymbol

    Get result type

    Examples:

    class CreateUser < ROM::Commands::Create[:memory]
      result :one
    end
    
    CreateUser.result
    # => :one
    

    Returns:

    • (Symbol)
  • .result(identifier) ⇒ Object

    Set result type

    Examples:

    class CreateUser < ROM::Commands::Create[:memory]
      result :one
    end
    


111
# File 'lib/rom/command.rb', line 111

defines :result

Instance Method Details

#after_hooksArray

List of after hooks

Returns:

  • (Array)


373
# File 'lib/rom/command.rb', line 373

def after_hooks = options[:after]

#before_hooksArray

List of before hooks

Returns:

  • (Array)


366
# File 'lib/rom/command.rb', line 366

def before_hooks = options[:before]

#call(*args) ⇒ Object Also known as: []

Call the command and return one or many tuples

This method will apply before/after hooks automatically

rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity



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 'lib/rom/command.rb', line 270

def call(*args, &)
  tuples =
    if hooks?
      prepared =
        if curried?
          apply_hooks(before_hooks, *(curry_args + args))
        else
          apply_hooks(before_hooks, *args)
        end

      result = prepared ? execute(prepared, &) : execute(&)

      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.drop(1))
      end
    else
      execute(*(curry_args + args), &)
    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

Returns:



330
# File 'lib/rom/command.rb', line 330

def combine(*others) = Graph.new(self, others)

#curried?TrueClass, FalseClass

Check if this command is curried

Returns:

  • (TrueClass, FalseClass)


337
# File 'lib/rom/command.rb', line 337

def curried? = !curry_args.empty?

#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.

Returns:



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, **options, curry_args: args)
  end
end

#executeArray

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.

This method is abstract.

Execute the command

Returns:

  • (Array)

    an array with inserted tuples

Raises:

  • (NotImplementedError)


256
257
258
259
260
261
# File 'lib/rom/command.rb', line 256

def execute(*)
  raise(
    NotImplementedError,
    "#{self.class}##{__method__} must be implemented"
  )
end

#gatewaySymbol

Return gateway of this command’s relation

Returns:

  • (Symbol)


245
246
247
# File 'lib/rom/command.rb', line 245

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

Returns:

  • (false)


403
# File 'lib/rom/command.rb', line 403

def graph? = false

#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

Returns:

  • (Boolean)


389
# File 'lib/rom/command.rb', line 389

def hooks? = !before_hooks.empty? || !after_hooks.empty?

#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

Returns:

  • (false)


396
# File 'lib/rom/command.rb', line 396

def lazy? = false

#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

Returns:

  • (TrueClass, FalseClass)


417
# File 'lib/rom/command.rb', line 417

def many? = result.equal?(:many)

#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



429
430
431
432
433
434
435
436
437
# File 'lib/rom/command.rb', line 429

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

#nameROM::Relation::Name

Return name of this command’s relation

Returns:



236
237
238
# File 'lib/rom/command.rb', line 236

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

Returns:



382
383
384
# File 'lib/rom/command.rb', line 382

def new(new_relation)
  self.class.build(new_relation, **options, 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

Returns:

  • (TrueClass, FalseClass)


410
# File 'lib/rom/command.rb', line 410

def one? = result.equal?(:one)

#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

Returns:

  • (TrueClass, FalseClass)


424
# File 'lib/rom/command.rb', line 424

def restrictible? = self.class.restrictable.equal?(true)