Class: RubyVolt::DataType::Array

Inherits:
Compound show all
Defined in:
lib/ruby_volt/data_type/compound/array.rb

Class Method Summary collapse

Methods inherited from RubyVolt::DataType

classifyDataType, testpacking, voltDataType

Class Method Details

.autodetect_dataType(array) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/ruby_volt/data_type/compound/array.rb', line 30

def autodetect_dataType(array)
  if array[0].is_a?(::Integer)
    max_int = array.select {|e| e.is_a?(::Integer)}.max
    voltDataType(max_int)
  else
    voltDataType(array[0])
  end
end

.pack(val = []) ⇒ Object

First element is a DataType indicator (DataType itself, Integer, String/Symbol)



7
8
9
10
11
12
13
14
15
16
# File 'lib/ruby_volt/data_type/compound/array.rb', line 7

def pack(val = []) # First element is a DataType indicator (DataType itself, Integer, String/Symbol)
  array = val[1..-1]
  dataType = classifyDataType(val[0])
  unless dataType  # No indicator recognized
    array = val
    dataType = autodetect_dataType(array)
  end
  countDataType = (dataType <= Byte) ? Integer : Short # The length preceding value for the TINYINT (byte) type is length preceded by a 4 byte integer instead of a 2 byte short.
  WireTypeInfo.pack(dataType) + countDataType.pack(array.size) + array.map {|e| dataType.pack(e)}.join
end

.unpack(bytes) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ruby_volt/data_type/compound/array.rb', line 18

def unpack(bytes)
  if dataType = WireTypeInfo.unpack(bytes)
    array = [dataType]
    countDataType = (dataType <= Byte) ? Integer : Short
    array_size = countDataType.unpack(bytes)
    array_size.times do
      array << dataType.unpack(bytes)
    end
    array
  end
end