Class: LibSL::LLUUID

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uuid = nil) ⇒ LLUUID

Returns a new instance of LLUUID.



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/types.rb', line 396

def initialize(uuid=nil)
  if uuid.nil?
    # Generate a random UUID
    uuid = [
      rand(0x100000000),
      rand(0x100000000),
      rand(0x100000000),
      rand(0x100000000),
    ].pack "N4"
    uuid[6] &= 0b00001111
    uuid[6] |= 48
    uuid[8] &= 0b00111111
    uuid[8] |= 0b10000000
  end

  raise "Invalid uuid format" unless uuid.is_a? String
  @uuid = if uuid.length == 36 and uuid.count("-") == 4 then
      # String representation (Guid)
      uuid.delete("-").scan(/../).map{|s| s.to_i(16)}
    else
      # Unpack from binary string
      uuid.unpack('C16')
    end
end

Instance Attribute Details

#uuidObject (readonly)

Returns the value of attribute uuid.



381
382
383
# File 'lib/types.rb', line 381

def uuid
  @uuid
end

Class Method Details

.decode(data) ⇒ Object



383
384
385
386
# File 'lib/types.rb', line 383

def self.decode(data)
  data = data.unpack 'a16a*'
  return self.new(data[0]), data[1]
end

.nullObject



388
389
390
# File 'lib/types.rb', line 388

def self.null()
  return self.new("00000000-0000-0000-0000-000000000000")
end

Instance Method Details

#encodeObject



392
393
394
# File 'lib/types.rb', line 392

def encode()
  to_bytes
end

#to_bytesObject



425
426
427
# File 'lib/types.rb', line 425

def to_bytes()
  @uuid.pack('C16')
end

#to_sObject



421
422
423
# File 'lib/types.rb', line 421

def to_s()
  sprintf('%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x', *(@uuid)).upcase
end