Class: Moped::Protocol::Command

Inherits:
Query
  • Object
show all
Defined in:
lib/moped/protocol/command.rb

Overview

This is a convenience class on top of Query for quickly creating a command.

Examples:

command = Moped::Protocol::Command.new :moped, ismaster: 1
socket.write command.serialize

Since:

  • 1.0.0

Direct Known Subclasses

Moped::Protocol::Commands::Authenticate

Constant Summary

Constants included from Message

Message::INT32_DECODE_STR, Message::INT64_DECODE_ARRAY_STR, Message::INT64_DECODE_STR

Instance Attribute Summary

Attributes inherited from Query

#batch_size, #collection, #database, #fields, #flags, #full_collection_name, #length, #limit, #op_code, #request_id, #selector, #skip

Instance Method Summary collapse

Methods inherited from Query

#basic_selector, #no_timeout=, #receive_replies

Methods included from Message

included, #inspect, #receive_replies, #serialize

Constructor Details

#initialize(database, command, options = {}) ⇒ Command

Instantiate the new command.

Examples:

Instantiate the new command.

Moped::Protocol::Command.new(:moped_test, ismaster: 1)

Parameters:

  • database (String, Symbol)

    The database to run the command on.

  • command (Hash)

    The command to run.

  • options (Hash) (defaults to: {})

    And additional query options.

Since:

  • 1.0.0



53
54
55
# File 'lib/moped/protocol/command.rb', line 53

def initialize(database, command, options = {})
  super(database, '$cmd', command, options.merge(limit: -1))
end

Instance Method Details

#failure?(reply) ⇒ true, false

Determine if the provided reply message is a failure with respect to a command.

Examples:

Is the reply a command failure?

command.failure?(reply)

Parameters:

  • reply (Reply)

    The reply to the command.

Returns:

  • (true, false)

    If the reply is a failure.

Since:

  • 2.0.0



25
26
27
# File 'lib/moped/protocol/command.rb', line 25

def failure?(reply)
  reply.command_failure?
end

#failure_exception(reply) ⇒ Moped::Errors::OperationFailure

Get the exception specific to a failure of this particular operation.

Examples:

Get the failure exception.

command.failure_exception(document)

Parameters:

Returns:

Since:

  • 2.0.0



39
40
41
# File 'lib/moped/protocol/command.rb', line 39

def failure_exception(reply)
  Errors::OperationFailure.new(self, reply.documents.first)
end

#log_inspectString

Provide the value that will be logged when the command runs.

Examples:

Provide the log inspection.

command.log_inspect

Returns:

  • (String)

    The string value for logging.

Since:

  • 1.0.0



65
66
67
68
# File 'lib/moped/protocol/command.rb', line 65

def log_inspect
  type = "COMMAND"
  "%-12s database=%s command=%s" % [type, database, selector.inspect]
end

#results(reply) ⇒ Hash

Take the provided reply and return the expected results to api consumers. In the case of the command it’s the first document.

Examples:

Get the expected results of the reply.

command.results(reply)

Parameters:

Returns:

  • (Hash)

    The first document in the reply.

Since:

  • 2.0.0



81
82
83
# File 'lib/moped/protocol/command.rb', line 81

def results(reply)
  reply.documents.first
end