Class: Dao::Slug

Inherits:
String
  • Object
show all
Defined in:
lib/dao/slug.rb

Constant Summary collapse

Join =
'-'

Class Method Summary collapse

Class Method Details

.for(*args) ⇒ Object


6
7
8
9
10
11
12
13
14
# File 'lib/dao/slug.rb', line 6

def Slug.for(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  join = options[:join]||options['join']||Join
  string = args.flatten.compact.join(join)
  words = string.to_s.scan(%r/\w+/)
  words.map!{|word| word.gsub %r/[^0-9a-zA-Z_-]/, ''}
  words.delete_if{|word| word.nil? or word.strip.empty?}
  new(words.join(join).downcase)
end