Class: VIN::Generator
- Inherits:
-
Object
- Object
- VIN::Generator
- Defined in:
- lib/vin/generator.rb
Overview
rubocop:disable Metrics/ClassLength
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#custom_timestamp ⇒ Object
readonly
Returns the value of attribute custom_timestamp.
-
#data_type ⇒ Object
readonly
Returns the value of attribute data_type.
Instance Method Summary collapse
- #generate_id(data_type) ⇒ Object
- #generate_ids(data_type, count) ⇒ Object
- #generate_ids_from_timestamps(data_type, timestamps:) ⇒ Object
-
#initialize(config:) ⇒ Generator
constructor
A new instance of Generator.
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
#config ⇒ Object (readonly)
Returns the value of attribute config.
6 7 8 |
# File 'lib/vin/generator.rb', line 6 def config @config end |
#count ⇒ Object (readonly)
Returns the value of attribute count.
6 7 8 |
# File 'lib/vin/generator.rb', line 6 def count @count end |
#custom_timestamp ⇒ Object (readonly)
Returns the value of attribute custom_timestamp.
6 7 8 |
# File 'lib/vin/generator.rb', line 6 def end |
#data_type ⇒ Object (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
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 (data_type, timestamps:) validate_data_type!(data_type) raise(ArgumentError, "timestamps must be an array") unless .is_a?(Array) raise(ArgumentError, "timestamps array cannot be empty") if .empty? .each { |ts| (ts) } # Check for potential duplicate ID issue = .tally max_count = .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 (data_type, ) end |