Class: Ace::Review::Atoms::SlugGenerator
- Inherits:
-
Object
- Object
- Ace::Review::Atoms::SlugGenerator
- 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
-
.generate(text, max_length: DEFAULT_MAX_LENGTH) ⇒ String
Generate a URL-safe slug from text.
Class Method Details
.generate(text, max_length: DEFAULT_MAX_LENGTH) ⇒ String
Generate a URL-safe slug from text
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 |