Sha256: fce75ddc1657ef64959104fdd00fc6ce5632dee3dcacb04ea6034703088f5210

Contents?: true

Size: 1.43 KB

Versions: 3

Compression:

Stored size: 1.43 KB

Contents

module ROM
  module Commands

    class AbstractCommand
      VALID_RESULTS = [:one, :many].freeze

      attr_reader :relation, :options, :result

      # @api private
      def initialize(relation, options)
        @relation = relation
        @options = options

        @result = options[:result] || :many

        if !VALID_RESULTS.include?(result)
          raise InvalidOptionError.new(:result, VALID_RESULTS)
        end
      end

      # Call the command and return one or many tuples
      #
      # @api public
      def call(*args)
        tuples = execute(*args)

        if result == :one
          tuples.first
        else
          tuples
        end
      end

      # Target relation on which the command will operate
      #
      # By default this is set to the relation that's passed to the constructor.
      # Specialized commands like Delete may set the target to a different relation.
      #
      # @return [Relation]
      #
      # @api public
      def target
        relation
      end

      # Assert that tuple count in the target relation corresponds to :result setting
      #
      # @raises TupleCountMismatchError
      #
      # @api private
      def assert_tuple_count
        if result == :one && target.size > 1
          raise TupleCountMismatchError, "#{inspect} expects one tuple"
        end
      end

    end

  end
end

require 'rom/commands/create'
require 'rom/commands/update'
require 'rom/commands/delete'

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rom-0.4.2 lib/rom/commands.rb
rom-0.4.1 lib/rom/commands.rb
rom-0.4.0 lib/rom/commands.rb