Module: Philiprehberger::Slug::Generator

Defined in:
lib/philiprehberger/slug/generator.rb

Overview

Core slug generation logic

Class Method Summary collapse

Class Method Details

.call(string, separator: '-', max: nil, unique: nil, custom_mapping: nil) ⇒ String

Generate a URL-safe slug from a string

Parameters:

  • string (String)

    the input string

  • separator (String) (defaults to: '-')

    the word separator

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

    maximum length

  • unique (Proc, nil) (defaults to: nil)

    uniqueness checker

  • custom_mapping (Hash, nil) (defaults to: nil)

    custom character replacements

Returns:

  • (String)

    the slug

Raises:

  • (Error)

    if input is not a string



16
17
18
19
20
21
22
23
# File 'lib/philiprehberger/slug/generator.rb', line 16

def self.call(string, separator: '-', max: nil, unique: nil, custom_mapping: nil)
  raise Error, "Input must be a String, got #{string.class}" unless string.is_a?(String)

  slug = build_slug(string, separator, custom_mapping)
  slug = truncate_at_boundary(slug, max, separator) if max
  slug = ensure_unique(slug, separator, unique) if unique
  slug
end