Class: Senv::Script::Slug

Inherits:
String
  • Object
show all
Defined in:
lib/senv/script.rb

Constant Summary collapse

Join =
'-'

Class Method Summary collapse

Class Method Details

.for(*args) ⇒ Object



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/senv/script.rb', line 426

def Slug.for(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}

  join = (options[:join] || options['join'] || Join).to_s

  string = args.flatten.compact.join(' ')

  tokens = string.scan(%r`[^\s#{ join }]+`)

  tokens.map! do |token|
    token.gsub(%r`[^\p{L}/.]`, '').downcase
  end

  tokens.map! do |token|
    token.gsub(%r`[/.]`, join * 2)
  end

  tokens.join(join)
end