Class: Bytepack::Varbinary

Inherits:
Basic show all
Defined in:
lib/bytepack/basic/varbinary.rb

Direct Known Subclasses

String, Symbol

Constant Summary collapse

DIRECTIVE =
'a'
NULL_INDICATOR =

NULL indicator for object type serializations

-1 # NULL indicator for object type serializations
LENGTH_TYPE =
AnyType

Class Method Summary collapse

Methods inherited from Basic

bytesToInt, intToBytes, preprocess

Methods inherited from Struct

classifyDataType, config, packingDataType, single_type_array?, testpacking

Class Method Details

.convert_input(val) ⇒ Object



28
29
30
# File 'lib/bytepack/basic/varbinary.rb', line 28

def convert_input(val)
  val.to_s.encode("ascii-8bit")
end

.pack(val) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/bytepack/basic/varbinary.rb', line 8

def pack(val)
  if val.nil?
    AnyType.pack(self::NULL_INDICATOR)
  else
    val = convert_input(val)
    self::LENGTH_TYPE.pack(val.bytesize) + val
  end
end

.unpack(bytes, offset = 0) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/bytepack/basic/varbinary.rb', line 17

def unpack(bytes, offset = 0)
  length, offset = *self::LENGTH_TYPE.unpack(bytes, offset)
  case length
  when self::NULL_INDICATOR then [nil, offset]
  when 0 then ["", offset]
  else
    offset, format = *preprocess(bytes, offset, self::DIRECTIVE, length)
    [bytes.unpack1(format), offset]
  end
end