Class: Uuid
Overview
The naming scheme used for this class is as follows:
-
uuid- An instance of this class. -
value- Packed string, not human readable. 16 bytes in length. -
uuid_str- Formatted string, human readable. Formatted as xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx (8-4-4-4-12).
Universally unique identifier. Items with distinct different UUIDs should be different entities. Items with the same UUID should be the same entity.
Constant Summary collapse
- DEFAULT =
Empty UUID.
Uuid.new("\x0" * 16).freeze
Instance Attribute Summary collapse
-
#value ⇒ String
readonly
Raw, packed byte string containing the UUID’s value.
Class Method Summary collapse
-
.generate ⇒ UUID
Generates a new (and random) UUID.
-
.parse(uuid_str) ⇒ Uuid?
Creates a UUID object from its string representation.
-
.valid_str?(uuid_str) ⇒ true, false
Determines whether a string contains a valid formatted UUID.
Instance Method Summary collapse
-
#<=>(other) ⇒ -1, ...
Compares two UUIDs to each other to determine which is lower.
-
#==(other) ⇒ true, false
Checks if two UUIDs have the same value.
-
#eql?(other) ⇒ true, false
Checks for hash equality of two UUIDs.
-
#hash ⇒ Fixnum
Produces a hash value of the UUID.
-
#initialize(value) ⇒ Uuid
constructor
Creates a UUID or one from an existing value.
-
#to_s(dashes = true) ⇒ String
Produces the string representation of the UUID.
Constructor Details
#initialize(value) ⇒ Uuid
Creates a UUID or one from an existing value.
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/lazy-uuid/uuid.rb', line 21 def initialize(value) value_str = value.to_s fail ArgumentError, 'Packed UUID value must be 16 bytes.' unless value_str.length == 16 # Get a copy to prevent external processes modifying the value. @value = value_str.dup # Prevent modification. @value.freeze end |
Instance Attribute Details
#value ⇒ String (readonly)
Raw, packed byte string containing the UUID’s value.
15 16 17 |
# File 'lib/lazy-uuid/uuid.rb', line 15 def value @value end |
Class Method Details
.generate ⇒ UUID
Generates a new (and random) UUID
36 37 38 39 40 41 42 43 |
# File 'lib/lazy-uuid/uuid.rb', line 36 def generate # A built-in method from Ruby to generate a valid UUID is SecureRandom.uuid. # However, it returns it as a formatted string. # The formatted string has to be converted to a packed string before it can be used. uuid_str = SecureRandom.uuid value = pack_uuid_str(uuid_str) Uuid.new(value) end |
.parse(uuid_str) ⇒ Uuid?
Casing does not matter for a-f.
Dashes may be omitted from the string. This allows strings of 32 hexadecimal characters.
Creates a UUID object from its string representation.
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/lazy-uuid/uuid.rb', line 80 def parse(uuid_str) if valid_str?(uuid_str) # Properly formatted UUID string. # Pack the UUID into a byte string and return a new instance. value = pack_uuid_str(uuid_str) Uuid.new(value) else # Not properly formatted. nil end end |
.valid_str?(uuid_str) ⇒ true, false
Determines whether a string contains a valid formatted UUID.
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/lazy-uuid/uuid.rb', line 49 def valid_str?(uuid_str) if uuid_str.is_a? String # Check the formatting. # Note that the validity of the UUID isn't checked. !!/^\A[0-9a-f]{8}(-?)[0-9a-f]{4}\1[0-9a-f]{4}\1[0-9a-f]{4}\1[0-9a-f]{12}\z$/i.match(uuid_str) else # A string wasn't passed in. fail ArgumentError end end |
Instance Method Details
#<=>(other) ⇒ -1, ...
Compares two UUIDs to each other to determine which is lower.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/lazy-uuid/uuid.rb', line 135 def <=>(other) case other when Uuid # Compare two UUID instances. cmp_packed(other.value) when String if other.length == 16 # Compare against a packed string cmp_packed(other) else # Compare against a formatted string cmp_formatted(other) end else # Everything else can't be compared. nil end end |
#==(other) ⇒ true, false
Checks if two UUIDs have the same value.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/lazy-uuid/uuid.rb', line 107 def ==(other) case other when Uuid # Compare two UUID instances. eq_packed(other.value) when String if other.length == 16 # Compare against a packed string eq_packed(other) else # Compare against a formatted string eq_formatted(other) end else # Everything else can't be equated. false end end |
#eql?(other) ⇒ true, false
99 100 101 |
# File 'lib/lazy-uuid/uuid.rb', line 99 def eql?(other) other.is_a?(Uuid) && eq_packed(other.value) end |
#hash ⇒ Fixnum
Produces a hash value of the UUID.
159 160 161 |
# File 'lib/lazy-uuid/uuid.rb', line 159 def hash @value.hash end |
#to_s(dashes = true) ⇒ String
Produces the string representation of the UUID.
167 168 169 170 171 172 173 174 |
# File 'lib/lazy-uuid/uuid.rb', line 167 def to_s(dashes = true) bytes = @value.bytes if dashes sprintf('%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x', *bytes) else sprintf('%02x' * 16, *bytes) end end |