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, 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.1.9"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.alphabetString

Returns The alphabet used for generating IDs.

Returns:

  • (String)

    The alphabet used for generating IDs.



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

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

.id_fieldSymbol?

Returns The default field to use for storing the ID in models.

Returns:

  • (Symbol, nil)

    The default field to use for storing the ID in models.



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

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

.lengthInteger

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

Returns:

  • (Integer)

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



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

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

.max_retriesInteger

Returns The maximum number of retries when generating a unique ID.

Returns:

  • (Integer)

    The maximum number of retries when generating a unique ID.



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

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

.separatorString

Returns The separator used in generated IDs.

Returns:

  • (String)

    The separator used in generated IDs.



46
47
48
# File 'lib/cool_id.rb', line 46

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.



51
52
53
# File 'lib/cool_id.rb', line 51

def configure
  yield self
end

.generate_id(config) ⇒ String

Generates a unique ID based on the given configuration.

Parameters:

  • config (Config)

    The configuration for ID generation.

Returns:

  • (String)

    A unique ID.

Raises:



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

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 = Nanoid.generate(size: length, alphabet: alphabet)
    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.

Parameters:

  • id (String)

    The CoolId to look up.

Returns:

  • (ActiveRecord::Base, nil)

    The found record, or nil if not found.



285
286
287
# File 'lib/cool_id.rb', line 285

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

.parse(id) ⇒ Id?

Parses a CoolId into its components.

Parameters:

  • id (String)

    The CoolId to parse.

Returns:

  • (Id, nil)

    The parsed Id object, or nil if parsing fails.



292
293
294
# File 'lib/cool_id.rb', line 292

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

.registryRegistry

Returns The default registry that keeps track of which prefixes are associated with which model classes.

Returns:

  • (Registry)

    The default registry that keeps track of which prefixes are associated with which model classes.



66
67
68
# File 'lib/cool_id.rb', line 66

def registry
  @prefix_map ||= Registry.new
end

.reset_configurationvoid

This method returns an undefined value.

Resets the configuration to default values.



57
58
59
60
61
62
63
# File 'lib/cool_id.rb', line 57

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.

Parameters:

  • model_class (Class)

    The ActiveRecord model class.

Returns:

  • (Symbol)

    The field to use for storing the CoolId.



97
98
99
# File 'lib/cool_id.rb', line 97

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