Class: Ro::Slug

Inherits:
String show all
Defined in:
lib/ro/slug.rb

Constant Summary collapse

@@JOIN =
'-'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from String

#html_safe

Constructor Details

#initialize(*args) ⇒ Slug

Returns a new instance of Slug.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ro/slug.rb', line 12

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

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

  string = args.flatten.compact.join(join)
  words = string.to_s.scan(%r{[/\w]+})
  words.map! { |word| word.gsub(/[_-]/, join) }
  words.map! { |word| word.gsub %r{[^/0-9a-zA-Z_-]}, '' }
  words.delete_if { |word| word.nil? or word.strip.empty? }

  slug = words.join(join).downcase.gsub('/', (join * 2))

  super(slug)
end

Class Method Details

.for(arg, *args, &block) ⇒ Object



6
7
8
9
# File 'lib/ro/slug.rb', line 6

def for(arg, *args, &block)
  return arg if arg.is_a?(Slug) && args.empty? && block.nil?
  new(arg, *args, &block)
end