Class: RubyVolt::DataType::SerializableException

Inherits:
Basic show all
Extended by:
Extensions::CodeValuesHash
Defined in:
lib/ruby_volt/data_type/complex/serializable_exception.rb

Constant Summary collapse

DIRECTIVE =
'a'
NULL_INDICATOR =

The ordinal will not be present if the exception’s length is 0.

0

Instance Attribute Summary

Attributes included from Extensions::CodeValuesHash

#code_values, #codes

Class Method Summary collapse

Methods included from Extensions::CodeValuesHash

hash_codes, inherited

Methods inherited from Basic

bytesToInt, intToBytes

Methods inherited from RubyVolt::DataType

classifyDataType, testpacking, voltDataType

Class Method Details

.pack(val, body = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ruby_volt/data_type/complex/serializable_exception.rb', line 13

def pack(val, body = nil)
  if val.nil?
    Integer.pack(self::NULL_INDICATOR)
  else
    ordinal = val.is_a?(::Integer) ? val : code_values[val]
    if body
      Integer.pack(1 + body.bytesize) + Byte.pack(ordinal) + body # Exception ordinal (1 byte) + opaque length
    else
      Integer.pack(1) + Byte.pack(ordinal) # Exception ordinal (1 byte)
    end
  end
end

.unpack(bytes) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruby_volt/data_type/complex/serializable_exception.rb', line 26

def unpack(bytes)
  if (length = Integer.unpack(bytes)) && length > 0
    ordinal = Byte.unpack(bytes)
    body_size = length - 1
    if body_size == 0 # Ordinal only, no body
      [codes[ordinal], nil]
    else
      [codes[ordinal], bytes.read(body_size).unpack1("#{self::DIRECTIVE}#{body_size}")]
    end
  else
    [nil, nil]
  end
end