Class: ROM::Changeset::Update

Inherits:
Stateful show all
Defined in:
lib/rom/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 ‘one` method.

Examples:

users.by_pk(1).changeset(:update, name: "Jane Doe").commit

See Also:

Constant Summary

Constants inherited from Stateful

Stateful::EMPTY_PIPE

Constants inherited from ROM::Changeset

DEFAULT_COMMAND_OPTS, VERSION

Instance Attribute Summary

Attributes inherited from Stateful

#__data__, #pipe

Attributes inherited from ROM::Changeset

#command_type, #relation

Instance Method Summary collapse

Methods inherited from Stateful

#associate, #command, #data, default_pipe, #extend, extend, inherited, #inspect, #map, map, pipes, #result, #to_a, #to_h

Methods inherited from ROM::Changeset

[], #command, command_type, #inspect, #new, relation

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)


54
55
56
# File 'lib/rom/changeset/update.rb', line 54

def clean?
  diff.empty?
end

#commitHash

Commit update changeset if there’s a diff

This returns original tuple if there’s no diff

Returns:

  • (Hash)

See Also:



27
28
29
# File 'lib/rom/changeset/update.rb', line 27

def commit
  diff? ? super : original
end

#diffHash

Calculate the diff between the original and changeset data

Returns:

  • (Hash)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rom/changeset/update.rb', line 63

def diff
  @diff ||=
    begin
      source = Hash(original)
      data = pipe.for_diff(__data__)
      data_tuple = data.to_a
      data_keys = data.keys & source.keys

      new_tuple = data_tuple.to_a.select { |k, _| data_keys.include?(k) }
      ori_tuple = source.to_a.select { |k, _| data_keys.include?(k) }

      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)


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

def diff?
  ! diff.empty?
end

#originalHash

Return original tuple that this changeset may update

Returns:

  • (Hash)


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

def original
  @original ||= relation.one
end