Class: Bytepack::Struct

Inherits:
Object
  • Object
show all
Defined in:
lib/bytepack.rb

Direct Known Subclasses

AnyType, Basic, Complex, Compound, CustomData

Class Method Summary collapse

Class Method Details

.classifyDataType(val) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bytepack.rb', line 48

def classifyDataType(val)
  case val
  when Class then val if val < Struct
  when ::String, ::Symbol then
    begin
      Bytepack.const_get("#{val}")
    rescue
      nil
    end
  end
end

.config(const, value) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/bytepack.rb', line 5

def config(const, value)
  begin
    remove_const(const) if const_defined?(const)
  rescue NameError
  end
  const_set(const, value)
end

.packingDataType(val) ⇒ Object

Ruby data type conversion



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bytepack.rb', line 13

def packingDataType(val) # Ruby data type conversion
  case val
  when ::Array then single_type_array?(val) ? SingleTypeArray : Array
  when ::Hash then Hash
  when ::NilClass then Null # Byte::NULL_INDICATOR
  when ::Integer then
    case val.bit_length
    when (0..7) then Byte
    when (8..15) then Short
    when (16..31) then Integer
    else
      Long if val.bit_length >= 32
    end
  when ::Float then Float
  when ::String then
    val.encoding.name == "UTF-8" ? String : Varbinary # See "sometext".encoding
  when ::Symbol then Symbol
  when ::Time then Timestamp
  when ::BigDecimal then Decimal
  else # CustomData
    CustomData.struct_by_ruby_type(val)
  end
end

.single_type_array?(array) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
# File 'lib/bytepack.rb', line 37

def single_type_array?(array)
  first_type = array[0].class
  begin
    array.each {|e| raise(Exception) unless e.is_a?(first_type)}
  rescue Exception
    false
  else
    true
  end
end

.testpacking(val) ⇒ Object



60
61
62
# File 'lib/bytepack.rb', line 60

def testpacking(val)
  unpack(pack(val))
end