class Bytepack::Struct

Public Class Methods

classifyDataType(val) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/bytepack.rb, line 60
def testpacking(val)
  unpack(pack(val))
end