Module: Moped::Protocol::Message::ClassMethods

Defined in:
lib/moped/protocol/message.rb

Overview

Provides a DSL for defining struct-like fields for building messages for the Mongo Wire.

Examples:

class Command
  extend Message::ClassMethods

  int32 :length
end

Command.fields # => [:length]
command = Command.new
command.length = 12
command.serialize_length("") # => "\f\x00\x00\x00"

Instance Method Summary collapse

Instance Method Details

#cstring(name) ⇒ Object

Declare a null terminated string field.

Examples:

class Query < Message
  cstring :collection
end

Parameters:

  • name (String)

    the name of this field



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/moped/protocol/message.rb', line 81

def cstring(name)
  attr_accessor name

  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def serialize_#{name}(buffer)
      buffer << #{name}
      buffer << 0
    end
  RUBY

  fields << name
end

#document(name, options = {}) ⇒ Object

Declare a BSON Document field.

Examples:

class Update < Message
  document :selector
end

optional document field

class Query < Message
  document :selector
  document :fields, optional: true
end

array of documents

class Reply < Message
  document :documents, type: :array
end

Parameters:

  • name (String)

    the name of this field

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

    the options for this field

Options Hash (options):

  • :type (:array)

    specify an array of documents

  • :optional (Boolean)

    specify this field as optional



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/moped/protocol/message.rb', line 116

def document(name, options = {})
  attr_accessor name

  if options[:optional]
    class_eval <<-RUBY, __FILE__, __LINE__ + 1
      def serialize_#{name}(buffer)
        BSON::Document.serialize(#{name}, buffer) if #{name}
      end
    RUBY
  elsif options[:type] == :array
    class_eval <<-RUBY, __FILE__, __LINE__ + 1
      def serialize_#{name}(buffer)
        #{name}.each do |document|
          BSON::Document.serialize(document, buffer)
        end
      end
    RUBY
  else
    class_eval <<-RUBY, __FILE__, __LINE__ + 1
      def serialize_#{name}(buffer)
        BSON::Document.serialize(#{name}, buffer)
      end
    RUBY
  end

  fields << name
end

#fieldsArray

Returns the fields defined for this message.

Returns:

  • (Array)

    the fields defined for this message



69
70
71
# File 'lib/moped/protocol/message.rb', line 69

def fields
  @fields ||= []
end

#finalizeObject

Declares the message class as complete, and defines its serialization method from the declared fields.



276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/moped/protocol/message.rb', line 276

def finalize
  class_eval <<-EOS, __FILE__, __LINE__ + 1
    def serialize(buffer = "")
      start = buffer.bytesize

      #{fields.map { |f| "serialize_#{f}(buffer)" }.join("\n")}

      self.length = buffer.bytesize - start
      buffer[start, 4] = serialize_length ""
      buffer
    end
    alias to_s serialize
  EOS
end

#flags(name, flag_map = {}) ⇒ Object

Declare a flag field (32 bit signed integer)

Examples:

class Update < Message
  flags :flags, upsert: 2 ** 0,
                multi:  2 ** 1
end

Parameters:

  • name (String)

    the name of this field

  • flags (Hash{Symbol => Number})

    the flags for this flag field



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/moped/protocol/message.rb', line 154

def flags(name, flag_map = {})
  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{name}
      @#{name} ||= []
    end

    def #{name}=(flags)
      if flags.is_a? Numeric
        @#{name} = #{name}_from_int(flags)
      else
        @#{name} = flags
      end
    end

    def #{name}_as_int
      bits = 0
      flags = self.#{name}
      #{flag_map.map { |flag, value| "bits |= #{value} if flags.include? #{flag.inspect}" }.join "\n"}
      bits
    end

    def #{name}_from_int(bits)
      flags = []
      #{flag_map.map { |flag, value| "flags << #{flag.inspect} if #{value} & bits == #{value}" }.join "\n"}
      flags
    end

    def serialize_#{name}(buffer)
      buffer << [#{name}_as_int].pack('l<')
    end

    def deserialize_#{name}(buffer)
      bits, = buffer.read(4).unpack('l<')

      self.#{name} = bits
    end
  RUBY

  fields << name
end

#int32(name) ⇒ Object

Declare a 32 bit signed integer field.

Examples:

class Query < Message
  int32 :length
end

Parameters:

  • name (String)

    the name of this field



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/moped/protocol/message.rb', line 203

def int32(name)
  attr_writer name

  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{name}
      @#{name} ||= 0
    end

    def serialize_#{name}(buffer)
      buffer << [#{name}].pack('l<')
    end

    def deserialize_#{name}(buffer)
      self.#{name}, = buffer.read(4).unpack('l<')
    end
  RUBY

  fields << name
end

#int64(name, options = {}) ⇒ Object

Declare a 64 bit signed integer field.

Examples:

class Query < Message
  int64 :cursor_id
end

with array type

class KillCursors < Message
  int64 :cursor_ids, type: :array
end

Parameters:

  • name (String)

    the name of this field

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

    the options for this field

Options Hash (options):

  • :type (:array)

    specify an array of 64 bit ints



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/moped/protocol/message.rb', line 238

def int64(name, options = {})
  attr_writer name

  if options[:type] == :array
    class_eval <<-RUBY, __FILE__, __LINE__ + 1
      def #{name}
        @#{name} ||= []
      end

      def serialize_#{name}(buffer)
        buffer << #{name}.pack('q*<')
      end

      def deserialize_#{name}(buffer)
        raise NotImplementedError
      end
    RUBY
  else
    class_eval <<-RUBY, __FILE__, __LINE__ + 1
      def #{name}
        @#{name} ||= 0
      end

      def serialize_#{name}(buffer)
        buffer << [#{name}].pack('q<')
      end

      def deserialize_#{name}(buffer)
        self.#{name}, = buffer.read(8).unpack('q<')
      end
    RUBY
  end

  fields << name
end