Class: Protobuf::Field::BytesField

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

Overview

Field class for +bytes+ type.

Direct Known Subclasses

StringField

Instance Attribute Summary

Attributes inherited from BaseField

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseField

descriptor, #descriptor, #initialize, #initialized?, #max, #merge, #min, #optional?, #packed?, #ready?, #repeated?, #required?, #set, #to_s

Constructor Details

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

Class Method Details

.defaultObject



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

def self.default
  ''
end

Instance Method Details

#acceptable?(val) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (TypeError)


335
336
337
338
# File 'lib/protobuf/message/field.rb', line 335

def acceptable?(val)
  raise TypeError unless val.instance_of?(String)
  true
end

#decode(bytes) ⇒ Object



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

def decode(bytes)
  bytes.force_encoding('ASCII-8BIT') if bytes.respond_to?(:force_encoding)
  bytes
end

#encode(value) ⇒ Object



345
346
347
348
349
350
# File 'lib/protobuf/message/field.rb', line 345

def encode(value)
  value = value.dup
  value.force_encoding('ASCII-8BIT') if value.respond_to?(:force_encoding)
  string_size = VarintField.encode(value.size)
  string_size << value
end

#wire_typeObject



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

def wire_type
  WireType::LENGTH_DELIMITED
end