class ROM::Changeset::Update
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.
@example
users.by_pk(1).changeset(:update, name: "Jane Doe").commit
@see Changeset::Stateful
@api public
Public Instance Methods
Return if there's no diff between the original and changeset data
@return [TrueClass, FalseClass]
@api public
# File lib/rom/changeset/update.rb, line 56 def clean? diff.empty? end
Commit update changeset if there's a diff
This returns original tuple if there's no diff
@return [Hash]
@see Changeset#commit
@api public
ROM::Changeset::Stateful#commit
# File lib/rom/changeset/update.rb, line 29 def commit diff? ? super : original end
Calculate the diff between the original and changeset data
@return [Hash]
@api public
# File lib/rom/changeset/update.rb, line 65 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
Return true if there's a diff between original and changeset data
@return [TrueClass, FalseClass]
@api public
# File lib/rom/changeset/update.rb, line 47 def diff? !diff.empty? end
Return original tuple that this changeset may update
@return [Hash]
@api public
# File lib/rom/changeset/update.rb, line 38 def original @original ||= relation.one end