angeloashmore/hanami-rethinkdb

View on GitHub
lib/hanami/model/adapters/rethinkdb/command.rb

Summary

Maintainability
A
0 mins
Test Coverage
module Hanami
  module Model
    module Adapters
      module Rethinkdb
        # Execute a command for the given query.
        #
        # @see Hanami::Model::Adapters::Rethinkdb::Query
        #
        # @api private
        # @since 0.1.0
        class Command
          # Initialize a command
          #
          # @param query [Hanami::Model::Adapters::Rethinkdb::Query]
          #
          # @api private
          # @since 0.1.0
          def initialize(query)
            @collection = query.scoped
          end

          # Creates a document for the given entity.
          #
          # @param entity [Object] the entity to persist
          #
          # @see Hanami::Model::Adapters::Rethinkdb::Collection#insert
          #
          # @return the primary key of the just created document.
          #
          # @api private
          # @since 0.1.0
          def create(entity)
            @collection.insert(entity)
          end

          # Updates the corresponding document for the given entity.
          #
          # @param entity [Object] the entity to persist
          #
          # @see Hanami::Model::Adapters::Rethinkdb::Collection#update
          #
          # @api private
          # @since 0.1.0
          def update(entity)
            @collection.update(entity)
          end

          # Deletes all the documents for the current query.
          #
          # It's used to delete a single document or an entire database table.
          #
          # @see Hanami::Model::Adapters::RethinkdbAdapter#delete
          # @see Hanami::Model::Adapters::RethinkdbAdapter#clear
          #
          # @api private
          # @since 0.1.0
          def delete
            @collection.delete
          end

          alias_method :clear, :delete
        end
      end
    end
  end
end