Class: StatelyDB::UUID
- Inherits:
-
Object
- Object
- StatelyDB::UUID
- 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
-
#byte_string ⇒ Object
Returns the value of attribute byte_string.
Class Method Summary collapse
-
.parse(byte_string) ⇒ StatelyDB::UUID
Parses a base16 string (eg: “f4a8a24a-129d-411f-91d2-6d19d0eaa096”) into a UUID object.
-
.valid_uuid?(byte_string) ⇒ Boolean
Not all bytes values in StatelyDB are UUIDs.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
UUIDs are sorted lexigraphically by their base16 representation.
-
#==(other) ⇒ Boolean
UUIDs are equal if their byte_strings are equal.
-
#empty? ⇒ Boolean
Returns true if the UUID is empty.
-
#initialize(byte_string) ⇒ UUID
constructor
A new instance of UUID.
-
#to_base64 ⇒ String
Encodes the byte string as a url-safe base64 string with padding removed.
-
#to_s ⇒ String
to_s returns the UUID as a base16 string (eg: “f4a8a24a-129d-411f-91d2-6d19d0eaa096”).
-
#to_str ⇒ String
to_str returns the UUID as a base16 string (eg: “f4a8a24a-129d-411f-91d2-6d19d0eaa096”).
Constructor Details
#initialize(byte_string) ⇒ UUID
Returns a new instance of UUID.
12 13 14 |
# File 'lib/uuid.rb', line 12 def initialize(byte_string) @byte_string = byte_string end |
Instance Attribute Details
#byte_string ⇒ Object
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:
-
Encoded as Encoding::ASCII_8BIT (also aliased as Encoding::BINARY) and be 16 bytes long.
-
A string of the form “f4a8a24a-129d-411f-91d2-6d19d0eaa096”
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.
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.
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.
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.
53 54 55 |
# File 'lib/uuid.rb', line 53 def empty? @byte_string.empty? end |
#to_base64 ⇒ String
Encodes the byte string as a url-safe base64 string with padding removed.
32 33 34 |
# File 'lib/uuid.rb', line 32 def to_base64 [@byte_string].pack("m0").tr("=", "").tr("+/", "-_") end |
#to_s ⇒ String
to_s returns the UUID as a base16 string (eg: “f4a8a24a-129d-411f-91d2-6d19d0eaa096”)
18 19 20 |
# File 'lib/uuid.rb', line 18 def to_s to_str end |
#to_str ⇒ String
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.
26 27 28 |
# File 'lib/uuid.rb', line 26 def to_str @byte_string.unpack("H8H4H4H4H12").join("-") end |