Module: ROM::Relation::Commands

Included in:
ROM::Relation, Combined
Defined in:
core/lib/rom/relation/commands.rb

Overview

Extensions for relation classes which provide access to commands

Instance Method Summary collapse

Instance Method Details

#command(type, mapper: nil, use: EMPTY_ARRAY, plugins_options: EMPTY_HASH, **opts) ⇒ ROM::Command

Return a command for the relation

This method can either return an existing custom command identified by type param, or generate a command dynamically based on relation AST.

Examples:

build a simple :create command

users.command(:create)

build a command which returns multiple results

users.command(:create, result: many)

build a command which uses a specific plugin

users.command(:create, use: :timestamps)

build a command which sends results through a custom mapper

users.command(:create, mapper: :my_mapper_identifier)

return an existing custom command

users.command(:my_custom_command_identifier)

Parameters:

  • type (Symbol)

    The command type (:create, :update or :delete)

  • opts (Hash)

    Additional options

Options Hash (**opts):

  • :mapper (Symbol) — default: nil

    An optional mapper applied to the command result

  • :use (Array<Symbol>) — default: []

    A list of command plugins

  • :result (Symbol) — default: :one

    Set how many results the command should return. Can be :one or :many

Returns:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'core/lib/rom/relation/commands.rb', line 40

def command(type, mapper: nil, use: EMPTY_ARRAY, plugins_options: EMPTY_HASH, **opts)
  base_command =
    if commands.key?(type)
      commands[type]
    else
      commands[type, adapter, to_ast, use, plugins_options, opts]
    end

  command =
    if mapper
      base_command >> mappers[mapper]
    elsif auto_map?
      base_command >> self.mapper
    else
      base_command
    end

  if command.restrictible?
    command.new(self)
  else
    command
  end
end