class Protobuf::Field::BytesField

Constants

BYTES_ENCODING

Constants

Public Class Methods

default() click to toggle source

Class Methods

# File lib/protobuf/field/bytes_field.rb, line 17
def self.default
  ''
end

Public Instance Methods

acceptable?(val) click to toggle source

Public Instance Methods

# File lib/protobuf/field/bytes_field.rb, line 25
def acceptable?(val)
  val.is_a?(String) || val.nil? || val.is_a?(Symbol) || val.is_a?(::Protobuf::Message)
end
coerce!(value) click to toggle source
# File lib/protobuf/field/bytes_field.rb, line 53
def coerce!(value)
  case value
  when String, Symbol
    value.to_s
  when NilClass
    nil
  when ::Protobuf::Message
    value.dup
  else
    fail TypeError, "Unacceptable value #{value} for field #{name} of type #{type_class}"
  end
end
decode(bytes) click to toggle source
# 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) click to toggle source
# File lib/protobuf/field/bytes_field.rb, line 35
def encode(value)
  value_to_encode =
    if value.is_a?(::Protobuf::Message)
      value.encode
    else
      value.dup
    end

  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_type() click to toggle source
# File lib/protobuf/field/bytes_field.rb, line 49
def wire_type
  ::Protobuf::WireType::LENGTH_DELIMITED
end