lib/rom/repository/changeset/update.rb in rom-repository-1.0.0.beta3 vs lib/rom/repository/changeset/update.rb in rom-repository-1.0.0.rc1

- old
+ new

@@ -1,14 +1,19 @@ module ROM class Changeset # 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. + # # @api public - class Update < Changeset - # @!attribute [r] primary_key - # @return [Symbol] The name of the relation's primary key attribute - option :primary_key, reader: true + class Update < Stateful + command_type :update # Commit update changeset if there's a diff # # This returns original tuple if there's no diff # @@ -19,35 +24,17 @@ # @api public def commit diff? ? super : original end - # Return true - # - # @return [TrueClass] - # - # @api public - def update? - true - end - - # Return false - # - # @return [FalseClass] - # - # @api public - def create? - false - end - # Return original tuple that this changeset may update # # @return [Hash] # # @api public def original - @original ||= relation.fetch(primary_key) + @original ||= Hash(relation.one) end # Return diff hash sent through the pipe # # @return [Hash] @@ -76,30 +63,20 @@ diff.empty? end # Calculate the diff between the original and changeset data # - # @return [Hash[ + # @return [Hash, Array] # # @api public def diff @diff ||= begin new_tuple = __data__.to_a ori_tuple = original.to_a Hash[new_tuple - (new_tuple & ori_tuple)] end - end - - # @api private - def command - command_compiler.(command_type, relation, mapper: false).curry(to_h) if diff? - end - - # @api private - def default_command_type - :update end end end end