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
-
.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
Returns The alphabet used for generating IDs.
46 |
# File 'lib/cool_id.rb', line 46 attr_accessor :separator, :alphabet, :length, :max_retries, :id_field |
.id_field ⇒ Symbol?
Returns 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 |
.length ⇒ Integer
Returns 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_retries ⇒ Integer
Returns 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 |
.separator ⇒ String
Returns 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.
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.
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.
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.
292 293 294 |
# File 'lib/cool_id.rb', line 292 def self.parse(id) registry.parse(id) end |
.registry ⇒ Registry
Returns 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_configuration ⇒ void
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 |