Class: Protobuf::Field::VarintField

Inherits:
BaseField
  • Object
show all
Defined in:
lib/protobuf/message/field.rb

Instance Attribute Summary

Attributes inherited from BaseField

#default, #message_class, #name, #rule, #tag, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseField

#clear, #default_value, #define_accessor, #define_getter, #define_setter, #descriptor, descriptor, #error_message, #get, #initialize, #initialized?, #max, #merge, #merge_array, #merge_value, #min, #optional?, #ready?, #repeated?, #required?, #set, #set_array, #to_s, #typed_default_value

Constructor Details

This class inherits a constructor from Protobuf::Field::BaseField

Class Method Details

.defaultObject



329
# File 'lib/protobuf/message/field.rb', line 329

def default; 0 end

.get_bytes(value) ⇒ Object



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/protobuf/message/field.rb', line 345

def self.get_bytes(value)
  # TODO should refactor using unpack('w*')
  #return [value].pack('w*').unpack('C*')
  return [0].pack('C') if value == 0
  bytes = []
  until value == 0
    byte = 0
    7.times do |i|
      byte |= (value & 1) << i
      value >>= 1
    end
    byte |= 0b10000000
    bytes << byte
  end
  #bytes[0] &= 0b01111111
  #bytes
  bytes[bytes.size - 1] &= 0b01111111
  bytes.pack('C*')
end

Instance Method Details

#acceptable?(val) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (TypeError)


369
370
371
372
373
# File 'lib/protobuf/message/field.rb', line 369

def acceptable?(val)
  raise TypeError.new(val.class.name) unless val.is_a? Integer
  raise RangeError.new(val) if val < min or max < val
  true
end

#get_bytes(value) ⇒ Object



365
366
367
# File 'lib/protobuf/message/field.rb', line 365

def get_bytes(value)
  self.class.get_bytes value
end

#set_bytes(message_instance, bytes) ⇒ Object



336
337
338
339
340
341
342
343
# File 'lib/protobuf/message/field.rb', line 336

def set_bytes(message_instance, bytes)
  # TODO should refactor using pack('w*')
  value = 0
  bytes.each_with_index do |byte, index|
    value |= byte << (7 * index)
  end
  message_instance.send("#{name}=", value)
end

#wire_typeObject



332
333
334
# File 'lib/protobuf/message/field.rb', line 332

def wire_type
  Protobuf::WireType::VARINT
end