Class: CoolId::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/cool_id.rb

Overview

Configuration class for CoolId generation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix:, model_class:, length: nil, alphabet: nil, max_retries: nil, id_field: nil) ⇒ Config

Initializes a new Config instance.

Parameters:

  • prefix (String)

    The prefix for generated IDs.

  • model_class (Class)

    The ActiveRecord model class.

  • length (Integer, nil) (defaults to: nil)

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

  • alphabet (String, nil) (defaults to: nil)

    The alphabet to use for generating IDs.

  • max_retries (Integer, nil) (defaults to: nil)

    The maximum number of retries when generating a unique ID.

  • id_field (Symbol, nil) (defaults to: nil)

    The field to use for storing the ID in the model.



188
189
190
191
192
193
194
195
# File 'lib/cool_id.rb', line 188

def initialize(prefix:, model_class:, length: nil, alphabet: nil, max_retries: nil, id_field: nil)
  @prefix = validate_prefix(prefix)
  @length = length
  @alphabet = validate_alphabet(alphabet)
  @max_retries = max_retries
  @model_class = model_class
  @id_field = id_field
end

Instance Attribute Details

#alphabetString? (readonly)

Returns The alphabet to use for generating IDs.

Returns:

  • (String, nil)

    The alphabet to use for generating IDs.



170
171
172
# File 'lib/cool_id.rb', line 170

def alphabet
  @alphabet
end

#id_fieldSymbol? (readonly)

Returns The field to use for storing the ID in the model.

Returns:

  • (Symbol, nil)

    The field to use for storing the ID in the model.



179
180
181
# File 'lib/cool_id.rb', line 179

def id_field
  @id_field
end

#lengthInteger? (readonly)

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

Returns:

  • (Integer, nil)

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



167
168
169
# File 'lib/cool_id.rb', line 167

def length
  @length
end

#max_retriesInteger? (readonly)

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

Returns:

  • (Integer, nil)

    The maximum number of retries when generating a unique ID.



173
174
175
# File 'lib/cool_id.rb', line 173

def max_retries
  @max_retries
end

#model_classClass (readonly)

Returns The ActiveRecord model class associated with this configuration.

Returns:

  • (Class)

    The ActiveRecord model class associated with this configuration.



176
177
178
# File 'lib/cool_id.rb', line 176

def model_class
  @model_class
end

#prefixString (readonly)

Returns The prefix for generated IDs.

Returns:

  • (String)

    The prefix for generated IDs.



164
165
166
# File 'lib/cool_id.rb', line 164

def prefix
  @prefix
end