Module: Moped::Protocol::Message

Included in:
Delete, GetMore, Insert, KillCursors, Query, Reply, Update
Defined in:
lib/moped/protocol/message.rb

Overview

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.

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

Examples:


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

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Extends the including class with ClassMethods.

Parameters:

  • subclass (Class)

    the inheriting class



43
44
45
46
47
# File 'lib/moped/protocol/message.rb', line 43

def included(base)
  super

  base.extend ClassMethods
end

Instance Method Details

#inspectString

Returns the nicely formatted version of the message.

Returns:

  • (String)

    the nicely formatted version of the message



327
328
329
330
331
332
333
# 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) ⇒ nil

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

Examples:

Receive replies.

message.receive_replies(connection)

Parameters:

Returns:

  • (nil)

    nil.

Since:

  • 1.0.0



314
# File 'lib/moped/protocol/message.rb', line 314

def receive_replies(connection); end

#serialize(buffer = "") ⇒ String Also known as: to_s

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

Parameters:

  • buffer (String) (defaults to: "")

    a buffer to serialize to

Returns:

  • (String)

    the result of serliazing this message

Raises:

  • (NotImplementedError)


321
322
323
# 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