Module: Botiasloop::HumanId
- Defined in:
- lib/botiasloop/human_id.rb
Overview
Generates human-readable identifiers for conversations Format: color-animal-XXX (e.g., blue-dog-123) All IDs are stored and compared in lowercase for case-insensitivity
Constant Summary collapse
- MAX_RETRIES =
10
Class Method Summary collapse
-
.build_id ⇒ String
Build a single ID attempt.
-
.exists?(id) ⇒ Boolean
Check if an ID already exists in the database Case-insensitive comparison.
-
.generate ⇒ String
Generate a unique human-readable ID Checks database for collisions and retries if needed.
-
.normalize(id) ⇒ String
Normalize an ID to lowercase for storage and comparison.
Class Method Details
.build_id ⇒ String
Build a single ID attempt
50 51 52 53 54 55 56 |
# File 'lib/botiasloop/human_id.rb', line 50 def self.build_id color = FFaker::Color.name.downcase.tr(" ", "-") animal = FFaker::AnimalUS.common_name.downcase.tr(" ", "-") number = rand(100..999) "#{color}-#{animal}-#{number}" end |
.exists?(id) ⇒ Boolean
Check if an ID already exists in the database Case-insensitive comparison
42 43 44 45 |
# File 'lib/botiasloop/human_id.rb', line 42 def self.exists?(id) normalized = normalize(id) Conversation.where(Sequel.function(:lower, :id) => normalized).count > 0 end |
.generate ⇒ String
Generate a unique human-readable ID Checks database for collisions and retries if needed
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/botiasloop/human_id.rb', line 17 def self.generate retries = 0 loop do id = build_id return id unless exists?(id) retries += 1 raise Error, "Failed to generate unique ID after #{MAX_RETRIES} attempts" if retries >= MAX_RETRIES end end |
.normalize(id) ⇒ String
Normalize an ID to lowercase for storage and comparison
33 34 35 |
# File 'lib/botiasloop/human_id.rb', line 33 def self.normalize(id) id.to_s.downcase.strip end |