Class: Crudboy::ID

Inherits:
Object show all
Defined in:
lib/crudboy/id.rb

Class Method Summary collapse

Class Method Details

.longObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/crudboy/id.rb', line 22

def long
  ts = (Time.now.to_f * 1000).to_i
  if ts < @last_timestamp
    raise 'Clock moved backwards.'
  end

  if ts == @last_timestamp
    @sequence = (@sequence + 1) & @sequence_mask
    if (@sequence == 0)
      ts = til_next_millis(@last_timestamp)
    end
  else
    @sequence = 0
  end
  @last_timestamp = ts

  ((ts - @id_epoch) << @timestamp_left_shift) | (@data_center_id << @data_center_id_shift) | (@worker_id << @worker_id_shift) | @sequence
end

.uuidObject



41
42
43
44
# File 'lib/crudboy/id.rb', line 41

def uuid
  require 'securerandom'
  SecureRandom.uuid.gsub('-', '')
end