Class: ROM::Changeset Abstract

Inherits:
Object
  • Object
show all
Extended by:
Dry::Core::Cache, Dry::Core::ClassAttributes, Initializer
Defined in:
lib/rom/repository/changeset.rb,
lib/rom/repository/changeset/pipe.rb,
lib/rom/repository/changeset/create.rb,
lib/rom/repository/changeset/delete.rb,
lib/rom/repository/changeset/update.rb,
lib/rom/repository/changeset/stateful.rb,
lib/rom/repository/changeset/associated.rb,
lib/rom/repository/changeset/restricted.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: PipeRegistry, Restricted Classes: Associated, Create, Delete, Pipe, Stateful, Update

Constant Summary collapse

DEFAULT_COMMAND_OPTS =
{ mapper: false }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#command_compilerProc (readonly)

Returns a proc that can compile a command (typically provided by a repo).

Returns:

  • (Proc)

    a proc that can compile a command (typically provided by a repo)



56
# File 'lib/rom/repository/changeset.rb', line 56

option :command_compiler, reader: true, optional: true

#command_typeSymbol (readonly)

Returns a custom command identifier.

Returns:

  • (Symbol)

    a custom command identifier



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

option :command_type, reader: true, default: -> changeset { changeset.class.command_type }

#relationRelation (readonly)

Returns The changeset relation.

Returns:

  • (Relation)

    The changeset relation



52
# File 'lib/rom/repository/changeset.rb', line 52

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

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


71
72
73
74
75
# File 'lib/rom/repository/changeset.rb', line 71

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

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


35
# File 'lib/rom/repository/changeset.rb', line 35

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)


48
# File 'lib/rom/repository/changeset.rb', line 48

defines :relation

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:

  • (ROM::Command)


123
124
125
# File 'lib/rom/repository/changeset.rb', line 123

def command
  command_compiler.(command_type, relation_identifier, DEFAULT_COMMAND_OPTS)
end

#commitHash, Array

Persist changeset

Examples:

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

Returns:

  • (Hash, Array)


105
106
107
# File 'lib/rom/repository/changeset.rb', line 105

def commit
  command.call
end

#inspectString

Return string representation of the changeset

Returns:

  • (String)


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

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

#with(new_options) ⇒ Changeset

Return a new changeset with updated options

Examples:

class NewUser < ROM::Changeset::Create[:users]
  option :token_generator, reader: true
end

changeset = user_repo.changeset(NewUser).with(token_generator: my_token_gen)

Parameters:

  • new_options (Hash)

    The new options

Returns:



91
92
93
# File 'lib/rom/repository/changeset.rb', line 91

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