Class: ROM::Changeset::Update
- Inherits:
-
Stateful
- Object
- ROM::Changeset
- Stateful
- ROM::Changeset::Update
- 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.
Constant Summary
Constants inherited from Stateful
Constants inherited from ROM::Changeset
Instance Attribute Summary
Attributes inherited from Stateful
Attributes inherited from ROM::Changeset
Instance Method Summary collapse
-
#clean? ⇒ TrueClass, FalseClass
Return if there’s no diff between the original and changeset data.
-
#commit ⇒ Hash
Commit update changeset if there’s a diff.
-
#diff ⇒ Hash
Calculate the diff between the original and changeset data.
-
#diff? ⇒ TrueClass, FalseClass
Return true if there’s a diff between original and changeset data.
-
#original ⇒ Hash
Return original tuple that this changeset may update.
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
54 55 56 |
# File 'lib/rom/changeset/update.rb', line 54 def clean? diff.empty? end |
#commit ⇒ Hash
Commit update changeset if there’s a diff
This returns original tuple if there’s no diff
27 28 29 |
# File 'lib/rom/changeset/update.rb', line 27 def commit diff? ? super : original end |
#diff ⇒ Hash
Calculate the diff between the original and changeset data
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
45 46 47 |
# File 'lib/rom/changeset/update.rb', line 45 def diff? ! diff.empty? end |
#original ⇒ Hash
Return original tuple that this changeset may update
36 37 38 |
# File 'lib/rom/changeset/update.rb', line 36 def original @original ||= relation.one end |