module Moped::Protocol::Message

The base class for building all messages needed to implement the Mongo Wire Protocol. It provides a minimal DSL for defining typed fields for serialization and deserialization over the wire.

@example

class KillCursors < Moped::Protocol::Message
  # header fields
  int32 :length
  int32 :request_id
  int32 :response_to
  int32 :op_code

  # message fields
  int32 :reserved
  int32 :number_of_cursors
  int64 :cursor_ids, type: :array

  # Customize field reader
  def number_of_cursors
    cursor_ids.length
  end
end

Note that all messages must implement the header fields required by the Mongo Wire Protocol, namely:

int32 :length
int32 :request_id
int32 :response_to
int32 :op_code

Public Instance Methods

inspect() click to toggle source

@return [String] the nicely formatted version of the message

# File lib/moped/protocol/message.rb, line 327
def inspect
  fields = self.class.fields.map do |field|
    "@#{field}=" + __send__(field).inspect
  end
  "#<#{self.class.name}\n" <<
  "  #{fields * "\n  "}>"
end
receive_replies(connection) click to toggle source

Default implementation for a message is to do nothing when receiving replies.

@example Receive replies.

message.receive_replies(connection)

@param [ Connection ] connection The connection.

@since 1.0.0

@return [ nil ] nil.

# File lib/moped/protocol/message.rb, line 314
def receive_replies(connection); end
serialize(buffer = "") click to toggle source

Serializes the message and all of its fields to a new buffer or to the provided buffer.

@param [String] buffer a buffer to serialize to @return [String] the result of serliazing this message

# File lib/moped/protocol/message.rb, line 321
def serialize(buffer = "")
  raise NotImplementedError, "This method is generated after calling #finalize on a message class"
end
Also aliased as: to_s
to_s(buffer = "") click to toggle source
Alias for: serialize