Class: Ace::Review::Atoms::SlugGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/review/atoms/slug_generator.rb

Overview

Generates URL-safe slugs from text strings

Handles edge cases:

  • Special characters replaced with hyphens

  • Consecutive hyphens collapsed

  • Leading/trailing hyphens removed

  • Length truncation for filesystem safety

Constant Summary collapse

DEFAULT_MAX_LENGTH =
64

Class Method Summary collapse

Class Method Details

.generate(text, max_length: DEFAULT_MAX_LENGTH) ⇒ String

Generate a URL-safe slug from text

Examples:

Basic usage

SlugGenerator.generate("google:gemini-2.5-flash")
#=> "google-gemini-2-5-flash"

With special characters

SlugGenerator.generate("model::name@provider")
#=> "model-name-provider"

With leading/trailing special chars

SlugGenerator.generate("@model-name@")
#=> "model-name"

Long text truncation

SlugGenerator.generate("very-long-model-name...", max_length: 10)
#=> "very-long" (truncated, trailing hyphen removed)

Parameters:

  • text (String)

    The text to convert to a slug

  • max_length (Integer) (defaults to: DEFAULT_MAX_LENGTH)

    Maximum slug length (default: 64)

Returns:

  • (String)

    URL-safe slug



37
38
39
40
41
42
43
44
45
46
# File 'lib/ace/review/atoms/slug_generator.rb', line 37

def self.generate(text, max_length: DEFAULT_MAX_LENGTH)
  return "" if text.nil? || text.empty?

  text
    .gsub(/[^a-zA-Z0-9\-_]/, "-").squeeze("-")              # Collapse consecutive hyphens
    .gsub(/\A-|-\z/, "")          # Remove leading/trailing hyphens
    .downcase
    .slice(0, max_length)         # Truncate to max length
    .gsub(/-\z/, "")              # Remove trailing hyphen after truncation
end