Class: VIN::Generator

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

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:) ⇒ Generator

Returns a new instance of Generator.



8
9
10
# File 'lib/vin/generator.rb', line 8

def initialize(config:)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/vin/generator.rb', line 6

def config
  @config
end

#countObject (readonly)

Returns the value of attribute count.



6
7
8
# File 'lib/vin/generator.rb', line 6

def count
  @count
end

#custom_timestampObject (readonly)

Returns the value of attribute custom_timestamp.



6
7
8
# File 'lib/vin/generator.rb', line 6

def custom_timestamp
  @custom_timestamp
end

#data_typeObject (readonly)

Returns the value of attribute data_type.



6
7
8
# File 'lib/vin/generator.rb', line 6

def data_type
  @data_type
end

Instance Method Details

#generate_id(data_type) ⇒ Object



12
13
14
15
16
# File 'lib/vin/generator.rb', line 12

def generate_id(data_type)
  validate_data_type!(data_type)

  generate_single_batch(data_type, 1, nil).first
end

#generate_ids(data_type, count) ⇒ Object



18
19
20
21
22
23
# File 'lib/vin/generator.rb', line 18

def generate_ids(data_type, count)
  validate_data_type!(data_type)
  validate_count!(count)

  generate_single_batch(data_type, count, nil)
end

#generate_ids_from_timestamps(data_type, timestamps:) ⇒ Object

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/vin/generator.rb', line 25

def generate_ids_from_timestamps(data_type, timestamps:)
  validate_data_type!(data_type)
  raise(ArgumentError, "timestamps must be an array") unless timestamps.is_a?(Array)
  raise(ArgumentError, "timestamps array cannot be empty") if timestamps.empty?

  timestamps.each { |ts| validate_timestamp!(ts) }

  # Check for potential duplicate ID issue
  timestamp_counts = timestamps.tally
  max_count = timestamp_counts.values.max
  if max_count > config.max_sequence
    raise ArgumentError,
          "Cannot generate #{max_count} IDs for the same timestamp. " \
          "Maximum allowed is #{config.max_sequence} (2^#{config.sequence_bits} - 1). " \
          "Requesting more would produce duplicate IDs."
  end

  generate_ids_for_multiple_timestamps(data_type, timestamps)
end