class IdGenerator::Generators::TimestampedRandom

Constants

CONTEXT_PART_SIZE
COUNTER_PART_SIZE
COUNTER_START
RANDOM_PART_BYTES

Attributes

config[R]

Public Class Methods

new(config) click to toggle source
# File lib/id_generator/generators/timestamped_random.rb, line 14
def initialize(config)
  @config = config
end

Public Instance Methods

generate() click to toggle source
# File lib/id_generator/generators/timestamped_random.rb, line 18
def generate
  "#{time}-#{context_id}-#{random_number}"
end

Private Instance Methods

context_id() click to toggle source
# File lib/id_generator/generators/timestamped_random.rb, line 29
def context_id
  value_to_hex(config.context_id, CONTEXT_PART_SIZE)
end
random_number() click to toggle source
# File lib/id_generator/generators/timestamped_random.rb, line 33
def random_number
  SecureRandom.hex(RANDOM_PART_BYTES)
end
time() click to toggle source
# File lib/id_generator/generators/timestamped_random.rb, line 24
def time
  timestamp = Time.now.to_i - COUNTER_START
  value_to_hex(timestamp, COUNTER_PART_SIZE)
end
value_to_hex(value, size) click to toggle source
# File lib/id_generator/generators/timestamped_random.rb, line 37
def value_to_hex(value, size)
  format("%0#{size}x", value)
end