module Uuidx

The Uuidx module contains a simple API to generate v4, v6, and v7 UUIDs without needing to create generators manually.

The simple API is exposed as a set of methods ::v4, ::v6, and ::v7 which handle thread-safety and generator creation.

Uuidx.v4 # => "2b54639d-e43e-489f-9c64-30ecdcac3c95"
Uuidx.v6 # => "1eda9761-9f6f-6414-8c5f-fd61f1239907"
Uuidx.v7 # => "01863d24-6d1e-78ba-92ee-6e80c79c4e28"

See the Version4, Version6, and Version7 classes for details on how to create generators manually.

Monotonic Batching

The simple API also provides thread-safe monotonic batch methods which expect an amount.

Uuidx.batch_v4(10) # => ["2b54639d-e43e-489f-9c64-30ecdcac3c95", ...]
Uuidx.batch_v6(10) # => ["1eda9761-9f6f-6414-8c5f-fd61f1239907", ...]
Uuidx.batch_v7(10) # => ["01863d24-6d1e-78ba-92ee-6e80c79c4e28", ...]

Monotonicity has little meaning with UUID v4, but the batches are ordered for consistency.

A Note on Clock Timings

This library uses the Process::CLOCK_REALTIME clock ID to obtain the current time. While the specification allows for implementations to manipulate time values, this library does not. Any system-based smearing or drift will appear within the timestamp values.

See the Version6 and Version7 documentation for manifestation details.

Constants

VERSION

The gem version.

Public Class Methods

batch(generator, amount) click to toggle source

Create a batch of UUIDs from a generator.

This can be useful for custom Version8 generators.

# File lib/uuidx.rb, line 111
def self.batch(generator, amount)
  s = Set.new
  s << generator.generate while s.length < amount
  s.sort
end
batch_v4(amount) click to toggle source

Create a batch of UUID v4 values using the default generator.

# File lib/uuidx.rb, line 76
def self.batch_v4(amount)
  @lock4.synchronize { batch(@uuid4, amount) }
end
batch_v6(amount) click to toggle source

Create a batch of UUID v6 values using the default generator.

# File lib/uuidx.rb, line 81
def self.batch_v6(amount)
  @lock6.synchronize { batch(@uuid6, amount) }
end
batch_v7(amount) click to toggle source

Create a batch of UUID v7 values using the default generator.

# File lib/uuidx.rb, line 86
def self.batch_v7(amount)
  @lock7.synchronize { batch(@uuid7, amount) }
end
max_uuid() click to toggle source

The maximum UUID as defined by §5.10 of RFC 4122 BIS-01.

This UUID is written as ffffffff-ffff-ffff-ffff-ffffffffffff and is greater than all other UUIDs in comparisons.

# File lib/uuidx.rb, line 56
def self.max_uuid
  "ffffffff-ffff-ffff-ffff-ffffffffffff"
end
nil_uuid() click to toggle source

The nil UUID as defined by §5.10 of RFC 4122 BIS-01.

This UUID is written as 00000000-0000-0000-0000-000000000000 and is less than all other UUIDs in comparisons. It does not act as nil.

# File lib/uuidx.rb, line 48
def self.nil_uuid
  "00000000-0000-0000-0000-000000000000"
end
reset_v4!() click to toggle source

Reset the UUID v4 default generator.

# File lib/uuidx.rb, line 91
def self.reset_v4!
  @lock4 = Mutex.new
  @uuid4 = Version4.new
end
reset_v6!() click to toggle source

Reset the UUID v6 default generator.

# File lib/uuidx.rb, line 97
def self.reset_v6!
  @lock6 = Mutex.new
  @uuid6 = Version6.new
end
reset_v7!() click to toggle source

Reset the UUID v7 default generator.

# File lib/uuidx.rb, line 103
def self.reset_v7!
  @lock7 = Mutex.new
  @uuid7 = Version7.new
end
v4() click to toggle source

Generate a new UUID v4 value using the default generator.

# File lib/uuidx.rb, line 61
def self.v4
  @lock4.synchronize { @uuid4.generate }
end
v6() click to toggle source

Generate a new UUID v6 value using the default generator.

# File lib/uuidx.rb, line 66
def self.v6
  @lock6.synchronize { @uuid6.generate }
end
v7() click to toggle source

Generate a new UUID v7 value using the default generator.

# File lib/uuidx.rb, line 71
def self.v7
  @lock7.synchronize { @uuid7.generate }
end