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
Instance Attribute Summary
Attributes inherited from Stateful
Attributes inherited from ROM::Changeset
#command_options, #command_plugins, #command_type, #relation
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_compiler_options, #data, default_pipe, extend, #extend, inherited, #inspect, map, #map, pipes, #result, #to_a, #to_h
Methods inherited from ROM::Changeset
[], #command, #command_compiler_options, command_options, command_plugins, command_type, #inspect, #new, relation, use
Methods included from Initializer
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
56 57 58 |
# File 'lib/rom/changeset/update.rb', line 56 def clean? diff.empty? end |
#commit ⇒ Hash
Commit update changeset if there's a diff
This returns original tuple if there's no diff
29 30 31 |
# File 'lib/rom/changeset/update.rb', line 29 def commit diff? ? super : original end |
#diff ⇒ Hash
Calculate the diff between the original and changeset data
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/rom/changeset/update.rb', line 65 def diff @diff ||= begin source = original.to_h 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) } (new_tuple - (new_tuple & ori_tuple)).to_h end end |
#diff? ⇒ TrueClass, FalseClass
Return true if there's a diff between original and changeset data
47 48 49 |
# File 'lib/rom/changeset/update.rb', line 47 def diff? !diff.empty? end |
#original ⇒ Hash
Return original tuple that this changeset may update
38 39 40 |
# File 'lib/rom/changeset/update.rb', line 38 def original @original ||= relation.one end |