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
-
.alphabet ⇒ String
The alphabet used for generating IDs.
-
.id_field ⇒ Symbol?
The default field to use for storing the ID in models.
-
.length ⇒ Integer
The length of the generated ID (excluding prefix and separator).
-
.max_retries ⇒ Integer
The maximum number of retries when generating a unique ID.
-
.separator ⇒ String
The separator used in generated IDs.
Class Method Summary collapse
-
.configure {|self| ... } ⇒ void
Configures the CoolId module.
-
.generate_id(config) ⇒ String
Generates a unique ID based on the given configuration.
-
.locate(id) ⇒ ActiveRecord::Base?
Locates a record by its CoolId.
-
.parse(id) ⇒ Id?
Parses a CoolId into its components.
-
.registry ⇒ Registry
The default registry that keeps track of which prefixes are associated with which model classes.
-
.reset_configuration ⇒ void
Resets the configuration to default values.
-
.resolve_cool_id_field(model_class) ⇒ Symbol
Resolves the field (column) to use for storing the CoolId in a model.
Class Attribute Details
.alphabet ⇒ String
49 |
# File 'lib/cool_id.rb', line 49 attr_accessor :separator, :alphabet, :length, :max_retries, :id_field |
.id_field ⇒ Symbol?
49 |
# File 'lib/cool_id.rb', line 49 attr_accessor :separator, :alphabet, :length, :max_retries, :id_field |
.length ⇒ Integer
49 |
# File 'lib/cool_id.rb', line 49 attr_accessor :separator, :alphabet, :length, :max_retries, :id_field |
.max_retries ⇒ Integer
49 |
# File 'lib/cool_id.rb', line 49 attr_accessor :separator, :alphabet, :length, :max_retries, :id_field |
.separator ⇒ String
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.
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.
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 |
.registry ⇒ Registry
69 70 71 |
# File 'lib/cool_id.rb', line 69 def registry @prefix_map ||= Registry.new end |
.reset_configuration ⇒ void
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 |