Module: UUIDv7::Generator
- Defined in:
- lib/uuidv7/generator.rb
Class Method Summary collapse
- .build(timestamp, rand_a, rand_b) ⇒ Object
- .generate(&clock) ⇒ Object
- .generate_compact(&clock) ⇒ Object
- .timestamp(uuid) ⇒ Object
Class Method Details
.build(timestamp, rand_a, rand_b) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/uuidv7/generator.rb', line 34 def build(, rand_a, rand_b) rand_a &= 0xFFF msb = ( << 16) | rand_a lsb = rand_b msb = (msb & 0xFFFFFFFFFFFF0FFF) | 0x0000000000007000 lsb = (lsb & 0x3FFFFFFFFFFFFFFF) | 0x8000000000000000 format_uuid(msb, lsb) end |
.generate(&clock) ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/uuidv7/generator.rb', line 8 def generate(&clock) = clock ? clock.call : (Time.now.to_f * 1000).to_i rand_a = rand(4096) rand_b = SecureRandom.random_number(2**62) build(, rand_a, rand_b) end |
.generate_compact(&clock) ⇒ Object
16 17 18 |
# File 'lib/uuidv7/generator.rb', line 16 def generate_compact(&clock) Base62.encode(generate(&clock)) end |
.timestamp(uuid) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/uuidv7/generator.rb', line 20 def (uuid) raise InvalidUUIDError, 'UUID cannot be nil' if uuid.nil? hex = uuid.delete('-') raise InvalidUUIDError, 'Invalid UUID format' unless hex.match?(/\A[0-9a-f]{32}\z/i) msb = hex[0, 16].to_i(16) version = (msb >> 12) & 0xF raise InvalidUUIDError, "UUID is not version 7 (got version #{version})" unless version == 7 msb >> 16 end |