Class: ROM::Changeset Abstract

Inherits:
Object
  • Object
show all
Extended by:
Dry::Core::Cache, Dry::Core::ClassAttributes, Initializer
Defined in:
changeset/lib/rom/changeset.rb,
changeset/lib/rom/changeset/pipe.rb,
changeset/lib/rom/changeset/create.rb,
changeset/lib/rom/changeset/delete.rb,
changeset/lib/rom/changeset/update.rb,
changeset/lib/rom/changeset/version.rb,
changeset/lib/rom/changeset/stateful.rb,
changeset/lib/rom/changeset/associated.rb,
changeset/lib/rom/changeset/pipe_registry.rb,
changeset/lib/rom/changeset/extensions/relation.rb

Overview

This class is abstract.

Abstract Changeset class

If you inherit from this class you need to configure additional settings

Examples:

define a custom changeset using :upsert command

class NewTag < ROM::Changeset[:tags]
  command_type :upsert
end

Direct Known Subclasses

Delete, Stateful

Defined Under Namespace

Modules: Extensions, PipeRegistry Classes: Associated, Create, Delete, Pipe, Stateful, Update

Constant Summary collapse

VERSION =
'5.3.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#command_optionsHash (readonly)

Returns Configured options for the command.

Returns:

  • (Hash)

    Configured options for the command



85
# File 'changeset/lib/rom/changeset.rb', line 85

option :command_options, default: -> { self.class.command_options }

#command_pluginsHash (readonly)

Returns Configured plugin options for the command.

Returns:

  • (Hash)

    Configured plugin options for the command



89
# File 'changeset/lib/rom/changeset.rb', line 89

option :command_plugins, default: -> { self.class.command_plugins }

#command_typeSymbol (readonly)

Returns a custom command identifier.

Returns:

  • (Symbol)

    a custom command identifier



81
# File 'changeset/lib/rom/changeset.rb', line 81

option :command_type, default: -> { self.class.command_type }

#relationRelation (readonly)

Returns The changeset relation.

Returns:



77
# File 'changeset/lib/rom/changeset.rb', line 77

param :relation

Class Method Details

.[](relation_name) ⇒ Object

Create a changeset class preconfigured for a specific relation

Examples:

class NewUserChangeset < ROM::Changeset::Create[:users]
end

users.changeset(NewUserChangeset).data(name: 'Jane')


106
107
108
109
110
# File 'changeset/lib/rom/changeset.rb', line 106

def self.[](relation_name)
  fetch_or_store([relation_name, self]) {
    Class.new(self) { relation(relation_name) }
  }
end

.command_optionsHash? .command_options(**options) ⇒ Hash

Get or set command options

Overloads:

  • .command_optionsHash?

    Return configured command_options

    Returns:

    • (Hash, nil)
  • .command_options(**options) ⇒ Hash

    Set command options

    Parameters:

    • options (Hash)

      The command options

    Returns:

    • (Hash)


47
# File 'changeset/lib/rom/changeset.rb', line 47

defines :command_options

.command_pluginsHash? .command_plugins(**options) ⇒ Hash

Get or set command plugins options

Overloads:

  • .command_pluginsHash?

    Return configured command_plugins

    Returns:

    • (Hash, nil)
  • .command_plugins(**options) ⇒ Hash

    Set command plugin options

    Parameters:

    • options (Hash)

      The command plugin options

    Returns:

    • (Hash)


60
# File 'changeset/lib/rom/changeset.rb', line 60

defines :command_plugins

.command_typeSymbol .command_type(identifier) ⇒ Symbol

Get or set changeset command type

Overloads:

  • .command_typeSymbol

    Return configured command_type

    Returns:

    • (Symbol)
  • .command_type(identifier) ⇒ Symbol

    Set relation identifier for this changeset

    Parameters:

    • identifier (Symbol)

      The command type identifier

    Returns:

    • (Symbol)


34
# File 'changeset/lib/rom/changeset.rb', line 34

defines :command_type

.relationSymbol .relation(identifier) ⇒ Symbol

Get or set changeset relation identifier

Overloads:

  • .relationSymbol

    Return configured relation identifier for this changeset

    Returns:

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

    Set relation identifier for this changeset

    Parameters:

    • identifier (Symbol)

      The relation identifier from the ROM container

    Returns:

    • (Symbol)


73
# File 'changeset/lib/rom/changeset.rb', line 73

defines :relation

.use(plugin, **options) ⇒ Object

Enable a plugin for the changeset



115
116
117
# File 'changeset/lib/rom/changeset.rb', line 115

def self.use(plugin, **options)
  ROM.plugin_registry[:changeset].fetch(plugin).apply_to(self, **options)
end

Instance Method Details

#commandROM::Command

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.

Return a command for this changeset

Returns:



161
162
163
# File 'changeset/lib/rom/changeset.rb', line 161

def command
  relation.command(command_type, **command_compiler_options)
end

#command_compiler_optionsHash

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.

Return configured command compiler options

Returns:

  • (Hash)


170
171
172
# File 'changeset/lib/rom/changeset.rb', line 170

def command_compiler_options
  command_options.merge(use: command_plugins.keys, plugins_options: command_plugins)
end

#commitHash, Array

Persist changeset

Examples:

changeset = users.changeset(name: 'Jane')
changeset.commit
# => { id: 1, name: 'Jane' }

Returns:

  • (Hash, Array)


143
144
145
# File 'changeset/lib/rom/changeset.rb', line 143

def commit
  command.call
end

#inspectString

Return string representation of the changeset

Returns:

  • (String)


152
153
154
# File 'changeset/lib/rom/changeset.rb', line 152

def inspect
  %(#<#{self.class} relation=#{relation.name.inspect}>)
end

#new(relation, **new_options) ⇒ Changeset

Return a new changeset with provided relation

New options can be provided too

Parameters:

  • relation (Relation)
  • new_options (Hash)

Returns:



129
130
131
# File 'changeset/lib/rom/changeset.rb', line 129

def new(relation, **new_options)
  self.class.new(relation, **options, **new_options)
end