Module: CoolId

Defined in:
lib/cool_id.rb,
lib/cool_id/version.rb

Overview

The CoolId module provides functionality for generating and managing unique identifiers.

Defined Under Namespace

Modules: Model Classes: Config, DuplicatePrefixError, Id, MaxRetriesExceededError, NotConfiguredError, Registry

Constant Summary collapse

DEFAULT_SEPARATOR =

Default separator used in generated IDs.

"_"
DEFAULT_ALPHABET =

Default alphabet used for generating IDs.

"0123456789abcdefghijklmnopqrstuvwxyz"
DEFAULT_LENGTH =

Default length of the generated ID (excluding prefix and separator).

12
DEFAULT_MAX_RETRIES =

Default maximum number of retries when generating a unique ID.

1000
VERSION =
"0.2.5"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.alphabetString



49
# File 'lib/cool_id.rb', line 49

attr_accessor :separator, :alphabet, :length, :max_retries, :id_field

.id_fieldSymbol?



49
# File 'lib/cool_id.rb', line 49

attr_accessor :separator, :alphabet, :length, :max_retries, :id_field

.lengthInteger



49
# File 'lib/cool_id.rb', line 49

attr_accessor :separator, :alphabet, :length, :max_retries, :id_field

.max_retriesInteger



49
# File 'lib/cool_id.rb', line 49

attr_accessor :separator, :alphabet, :length, :max_retries, :id_field

.separatorString



49
50
51
# File 'lib/cool_id.rb', line 49

def separator
  @separator
end

Class Method Details

.configure {|self| ... } ⇒ void

This method returns an undefined value.

Configures the CoolId module.

Yields:

  • (self)

    Gives itself to the block.



54
55
56
# File 'lib/cool_id.rb', line 54

def configure
  yield self
end

.generate_id(config) ⇒ String

Generates a unique ID based on the given configuration.

Raises:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/cool_id.rb', line 77

def generate_id(config)
  alphabet = config.alphabet || @alphabet
  length = config.length || @length
  max_retries = config.max_retries || @max_retries

  retries = 0
  loop do
    nano_id = SecureRandom.alphanumeric(length, chars: alphabet.chars)
    full_id = "#{config.prefix}#{separator}#{nano_id}"
    if !config.model_class.exists?(id: full_id)
      return full_id
    end

    retries += 1
    if retries >= max_retries
      raise MaxRetriesExceededError, "Failed to generate a unique ID after #{max_retries} attempts"
    end
  end
end

.locate(id) ⇒ ActiveRecord::Base?

Locates a record by its CoolId.



302
303
304
# File 'lib/cool_id.rb', line 302

def self.locate(id)
  registry.locate(id)
end

.parse(id) ⇒ Id?

Parses a CoolId into its components.



309
310
311
# File 'lib/cool_id.rb', line 309

def self.parse(id)
  registry.parse(id)
end

.registryRegistry



69
70
71
# File 'lib/cool_id.rb', line 69

def registry
  @prefix_map ||= Registry.new
end

.reset_configurationvoid

This method returns an undefined value.

Resets the configuration to default values.



60
61
62
63
64
65
66
# File 'lib/cool_id.rb', line 60

def reset_configuration
  self.separator = DEFAULT_SEPARATOR
  self.alphabet = DEFAULT_ALPHABET
  self.length = DEFAULT_LENGTH
  self.max_retries = DEFAULT_MAX_RETRIES
  self.id_field = nil
end

.resolve_cool_id_field(model_class) ⇒ Symbol

Resolves the field (column) to use for storing the CoolId in a model.



100
101
102
# File 'lib/cool_id.rb', line 100

def resolve_cool_id_field(model_class)
  model_class.cool_id_config&.id_field || CoolId.id_field || model_class.primary_key
end