Class: Protobuf::Field::BytesField

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

Direct Known Subclasses

StringField

Constant Summary collapse

BYTES_ENCODING =

Constants

Encoding::BINARY

Constants inherited from BaseField

Protobuf::Field::BaseField::PACKED_TYPES

Instance Attribute Summary

Attributes inherited from BaseField

#message_class, #name, #options, #rule, #tag, #type_class

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseField

#coerce!, #default, #default_value, #deprecated?, #enum?, #extension?, #getter, #initialize, #message?, #optional?, #packed?, #repeated?, #repeated_message?, #required?, #set, #setter, #to_s, #type, #warn_if_deprecated

Methods included from Logging

initialize_logger, #log_exception, #log_signature, #logger, logger, logger=, #sign_message

Constructor Details

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

Class Method Details

.defaultObject

Class Methods



17
18
19
# File 'lib/protobuf/field/bytes_field.rb', line 17

def self.default
  ''
end

Instance Method Details

#acceptable?(val) ⇒ Boolean

Public Instance Methods

Returns:

  • (Boolean)


25
26
27
# File 'lib/protobuf/field/bytes_field.rb', line 25

def acceptable?(val)
  val.nil? || val.is_a?(String) || val.is_a?(Symbol) || val.is_a?(::Protobuf::Message)
end

#decode(bytes) ⇒ Object



29
30
31
32
33
# File 'lib/protobuf/field/bytes_field.rb', line 29

def decode(bytes)
  bytes_to_decode = bytes.dup
  bytes_to_decode.force_encoding(::Protobuf::Field::BytesField::BYTES_ENCODING)
  bytes_to_decode
end

#encode(value) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/protobuf/field/bytes_field.rb', line 35

def encode(value)
  value_to_encode = value.dup
  value_to_encode = value.encode if value.is_a?(::Protobuf::Message)
  value_to_encode.force_encoding(::Protobuf::Field::BytesField::BYTES_ENCODING)

  string_size = ::Protobuf::Field::VarintField.encode(value_to_encode.size)
  string_size << value_to_encode
end

#wire_typeObject



44
45
46
# File 'lib/protobuf/field/bytes_field.rb', line 44

def wire_type
  ::Protobuf::WireType::LENGTH_DELIMITED
end