Method: Moped::Protocol::Message::ClassMethods#int64

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

#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



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/moped/protocol/message.rb', line 273

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