Module: Bones::RPC::Protocol::BinaryHelper::ClassMethods

Defined in:
lib/bones/rpc/protocol/binary_helper.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

#binary(name) ⇒ Object

Declare a binary field.

Examples:

class Query < Message
  binary :collection
end

Parameters:

  • name (String)

    the name of this field


110
111
112
113
114
115
116
117
118
119
120
# File 'lib/bones/rpc/protocol/binary_helper.rb', line 110

def binary(name)
  attr_accessor name

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

  fields << name
end

#cstring(name) ⇒ Object

Declare a null terminated string field.

Examples:

class Query < Message
  cstring :collection
end

Parameters:

  • name (String)

    the name of this field


130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/bones/rpc/protocol/binary_helper.rb', line 130

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


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
# File 'lib/bones/rpc/protocol/binary_helper.rb', line 165

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

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

  fields << name
end

#fieldsArray

Returns the fields defined for this message.

Returns:

  • (Array)

    the fields defined for this message


98
99
100
# File 'lib/bones/rpc/protocol/binary_helper.rb', line 98

def fields
  @fields ||= []
end

#finalizeObject

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


409
410
411
412
413
414
415
416
417
# File 'lib/bones/rpc/protocol/binary_helper.rb', line 409

def finalize
  class_eval <<-EOS, __FILE__, __LINE__ + 1
    def serialize(buffer = "", adapter = nil)
      #{fields.map { |f| "serialize_#{f}(buffer)" }.join("\n")}
      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


203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/bones/rpc/protocol/binary_helper.rb', line 203

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


308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/bones/rpc/protocol/binary_helper.rb', line 308

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


371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/bones/rpc/protocol/binary_helper.rb', line 371

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

#int8(name) ⇒ Object

Declare a 8 bit signed integer field.

Examples:

class Query < Message
  int8 :length
end

Parameters:

  • name (String)

    the name of this field


280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/bones/rpc/protocol/binary_helper.rb', line 280

def int8(name)
  attr_writer name

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

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

    def deserialize_#{name}(buffer)
      self.#{name}, = buffer.read(1).unpack('c')
    end
  RUBY

  fields << name
end

#uint32(name) ⇒ Object

Declare a 32 bit unsigned integer field.

Examples:

class Query < Message
  uint32 :length
end

Parameters:

  • name (String)

    the name of this field


336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/bones/rpc/protocol/binary_helper.rb', line 336

def uint32(name)
  attr_writer name

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

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

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

  fields << name
end

#uint8(name) ⇒ Object

Declare a 8 bit unsigned integer field.

Examples:

class Query < Message
  uint8 :length
end

Parameters:

  • name (String)

    the name of this field


252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/bones/rpc/protocol/binary_helper.rb', line 252

def uint8(name)
  attr_writer name

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

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

    def deserialize_#{name}(buffer)
      self.#{name}, = buffer.read(1).unpack('C')
    end
  RUBY

  fields << name
end