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.



171
172
173
174
175
176
177
178
# File 'lib/cool_id.rb', line 171

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.



153
154
155
# File 'lib/cool_id.rb', line 153

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.



162
163
164
# File 'lib/cool_id.rb', line 162

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).



150
151
152
# File 'lib/cool_id.rb', line 150

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.



156
157
158
# File 'lib/cool_id.rb', line 156

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.



159
160
161
# File 'lib/cool_id.rb', line 159

def model_class
  @model_class
end

#prefixString (readonly)

Returns The prefix for generated IDs.

Returns:

  • (String)

    The prefix for generated IDs.



147
148
149
# File 'lib/cool_id.rb', line 147

def prefix
  @prefix
end