Class: IdGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/id_generator.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
'0.1.2'.freeze
COUNTER_PART_SIZE =
8
CONTEXT_PART_SIZE =
2
RANDOM_PART_BYTES =
11
COUNTER_START =
Time.new(2000).to_i

Instance Method Summary collapse

Constructor Details

#initialize(context_id) ⇒ IdGenerator

Returns a new instance of IdGenerator.

Raises:



15
16
17
18
19
# File 'lib/id_generator.rb', line 15

def initialize(context_id)
  raise(IdGenerator::Error, 'Invalid project id') unless context_id_valid?(context_id)

  @context_id = value_to_hex(context_id, CONTEXT_PART_SIZE)
end

Instance Method Details

#generateObject



21
22
23
# File 'lib/id_generator.rb', line 21

def generate
  "#{time}-#{@context_id}-#{random_number}"
end