Sha256: 8bfbcf98003322bc7a7d28999051f9d40eeb4f97551a78e306f7df4ababb69d9

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 KB

Contents

module ROM
  class Changeset
    # Changeset specialization for update commands
    #
    # @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

      # 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)
      end

      # Return diff hash sent through the pipe
      #
      # @return [Hash]
      #
      # @api public
      def to_h
        pipe.call(diff)
      end
      alias_method :to_hash, :to_h

      # Return true if there's a diff between original and changeset data
      #
      # @return [TrueClass, FalseClass]
      #
      # @api public
      def diff?
        ! diff.empty?
      end

      # Return if there's no diff between the original and changeset data
      #
      # @return [TrueClass, FalseClass]
      #
      # @api public
      def clean?
        diff.empty?
      end

      # Calculate the diff between the original and changeset data
      #
      # @return [Hash[
      #
      # @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
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rom-repository-0.3.1 lib/rom/repository/changeset/update.rb
rom-repository-0.3.0 lib/rom/repository/changeset/update.rb