Class: ROM::Changeset Abstract

Inherits:
Object
  • Object
show all
Extended by:
Dry::Core::Cache, Dry::Core::ClassAttributes
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

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

Classes: Associated, Create, Delete, Stateful, Update

Constant Summary

DEFAULT_COMMAND_OPTS =
{ mapper: false }.freeze
VERSION =
'1.0.1'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#command_typeSymbol (readonly)

Returns a custom command identifier

Returns:

  • (Symbol)

    a custom command identifier



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

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

#relationRelation (readonly)

Returns The changeset relation

Returns:



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

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


68
69
70
71
72
# File 'changeset/lib/rom/changeset.rb', line 68

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)


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

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)


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

defines :relation

Instance Method Details

#commitHash, Array

Persist changeset

Examples:

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

Returns:

  • (Hash, Array)


98
99
100
# File 'changeset/lib/rom/changeset.rb', line 98

def commit
  command.call
end

#inspectString

Return string representation of the changeset

Returns:

  • (String)


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

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

#new(relation, new_options = EMPTY_HASH) ⇒ Changeset

Return a new changeset with provided relation

New options can be provided too

Parameters:

  • relation (Relation)
  • new_options (Hash) (defaults to: EMPTY_HASH)

Returns:



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

def new(relation, new_options = EMPTY_HASH)
  self.class.new(relation, new_options.empty? ? options : options.merge(new_options))
end