Class: ROM::Changeset::Update

Inherits:
Stateful show all
Includes:
Restricted
Defined in:
lib/rom/repository/changeset/update.rb

Overview

Changeset specialization for update commands

Update changesets will only execute their commands when the data is different from the original tuple. Original tuple is fetched from changeset’s relation using ‘by_pk` relation view. This means the underlying adapter must provide this view, or you you need to implement it yourself in your relations if you want to use Update changesets.

See Also:

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ROM::Changeset::Stateful

Instance Method Details

#clean?TrueClass, FalseClass

Return if there’s no diff between the original and changeset data

Returns:

  • (TrueClass, FalseClass)


68
69
70
# File 'lib/rom/repository/changeset/update.rb', line 68

def clean?
  diff.empty?
end

#commandObject Originally defined in module Restricted

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 restricted by the changeset’s relation

#commitHash

Commit update changeset if there’s a diff

This returns original tuple if there’s no diff

Returns:

  • (Hash)

See Also:



31
32
33
# File 'lib/rom/repository/changeset/update.rb', line 31

def commit
  diff? ? super : original
end

#diffHash

Calculate the diff between the original and changeset data

Returns:

  • (Hash)


77
78
79
80
81
82
83
84
85
# File 'lib/rom/repository/changeset/update.rb', line 77

def diff
  @diff ||=
    begin
      new_tuple = __data__.to_a
      ori_tuple = original.to_a

      Hash[new_tuple - (new_tuple & ori_tuple)]
    end
end

#diff?TrueClass, FalseClass

Return true if there’s a diff between original and changeset data

Returns:

  • (TrueClass, FalseClass)


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

def diff?
  ! diff.empty?
end

#originalHash

Return original tuple that this changeset may update

Returns:

  • (Hash)


40
41
42
# File 'lib/rom/repository/changeset/update.rb', line 40

def original
  @original ||= Hash(relation.one)
end

#to_hHash Also known as: to_hash

Return diff hash sent through the pipe

Returns:

  • (Hash)


49
50
51
# File 'lib/rom/repository/changeset/update.rb', line 49

def to_h
  pipe.call(diff)
end