Class: StatelyDB::UUID

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

Overview

UUID is a helper class for working with UUIDs in StatelyDB. The ruby version of a StatelyDB is a binary string, and this class provides convenience methods for converting to the base16 representation specified in RFC 9562. Internally this class uses the byte_string attribute to store the UUID as a string with the Encoding::ASCII_8BIT encoding.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(byte_string) ⇒ UUID

Returns a new instance of UUID.

Parameters:

  • byte_string (String)

    A binary-encoded string (eg: Encoding::ASCII_8BIT encoding)



12
13
14
# File 'lib/uuid.rb', line 12

def initialize(byte_string)
  @byte_string = byte_string
end

Instance Attribute Details

#byte_stringObject

Returns the value of attribute byte_string.



9
10
11
# File 'lib/uuid.rb', line 9

def byte_string
  @byte_string
end

Class Method Details

.parse(byte_string) ⇒ StatelyDB::UUID

Parses a base16 string (eg: “f4a8a24a-129d-411f-91d2-6d19d0eaa096”) into a UUID object. The string can be the following:

  1. Encoded as Encoding::ASCII_8BIT (also aliased as Encoding::BINARY) and be 16 bytes long.

  2. A string of the form “f4a8a24a-129d-411f-91d2-6d19d0eaa096”

Parameters:

  • byte_string (String)

    A binary-encoded string (eg: Encoding::ASCII_8BIT encoding) that is 16 bytes in length, or a base16-formatted UUID string.

Returns:



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/uuid.rb', line 64

def self.parse(byte_string)
  return byte_string if byte_string.is_a?(StatelyDB::UUID)

  if valid_uuid?(byte_string)
    return new(byte_string)
  elsif byte_string.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i)
    return new([byte_string.delete("-")].pack("H*"))
  end

  raise "Invalid UUID"
end

.valid_uuid?(byte_string) ⇒ Boolean

Not all bytes values in StatelyDB are UUIDs. This method checks if a byte string is a valid UUID.

Parameters:

  • byte_string (String)

    A binary-encoded string (eg: Encoding::ASCII_8BIT encoding)

Returns:

  • (Boolean)


79
80
81
# File 'lib/uuid.rb', line 79

def self.valid_uuid?(byte_string)
  byte_string.encoding == Encoding::BINARY && byte_string.bytesize == 16
end

Instance Method Details

#<=>(other) ⇒ Integer

UUIDs are sorted lexigraphically by their base16 representation.

Parameters:

Returns:

  • (Integer)


47
48
49
# File 'lib/uuid.rb', line 47

def <=>(other)
  to_s <=> other.to_s
end

#==(other) ⇒ Boolean

UUIDs are equal if their byte_strings are equal.

Parameters:

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/uuid.rb', line 39

def ==(other)
  self.class == other.class &&
    @byte_string == other.byte_string
end

#empty?Boolean

Returns true if the UUID is empty.

Returns:

  • (Boolean)


53
54
55
# File 'lib/uuid.rb', line 53

def empty?
  @byte_string.empty?
end

#to_base64String

Encodes the byte string as a url-safe base64 string with padding removed.

Returns:

  • (String)


32
33
34
# File 'lib/uuid.rb', line 32

def to_base64
  [@byte_string].pack("m0").tr("=", "").tr("+/", "-_")
end

#to_sString

to_s returns the UUID as a base16 string (eg: “f4a8a24a-129d-411f-91d2-6d19d0eaa096”)

Returns:

  • (String)


18
19
20
# File 'lib/uuid.rb', line 18

def to_s
  to_str
end

#to_strString

to_str returns the UUID as a base16 string (eg: “f4a8a24a-129d-411f-91d2-6d19d0eaa096”)

Note: to_str is a type coercion method that is called by Ruby when an object is coerced to a string.

Returns:

  • (String)


26
27
28
# File 'lib/uuid.rb', line 26

def to_str
  @byte_string.unpack("H8H4H4H4H12").join("-")
end