class Protobuf::Field::StringField

Constants

ENCODING

Constants

Public Instance Methods

acceptable?(val) click to toggle source

Public Instance Methods

# File lib/protobuf/field/string_field.rb, line 17
def acceptable?(val)
  val.is_a?(String) || val.nil? || val.is_a?(Symbol)
end
coerce!(value) click to toggle source
# File lib/protobuf/field/string_field.rb, line 21
def coerce!(value)
  if value.nil?
    nil
  elsif acceptable?(value)
    value.to_s
  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/string_field.rb, line 31
def decode(bytes)
  bytes.force_encoding(::Protobuf::Field::StringField::ENCODING)
  bytes
end
encode(value) click to toggle source
# File lib/protobuf/field/string_field.rb, line 36
def encode(value)
  value_to_encode = "" + value # dup is slower
  unless value_to_encode.encoding == ENCODING
    value_to_encode.encode!(::Protobuf::Field::StringField::ENCODING, :invalid => :replace, :undef => :replace, :replace => "")
  end
  value_to_encode.force_encoding(::Protobuf::Field::BytesField::BYTES_ENCODING)

  "#{::Protobuf::Field::VarintField.encode(value_to_encode.bytesize)}#{value_to_encode}"
end
json_encode(value, options={}) click to toggle source
# File lib/protobuf/field/string_field.rb, line 46
def json_encode(value, options={})
  value
end