Sha256: e19f7759b9fa62767bf2280ce1c6ac4ff7e45b42d7ca2008746959308bb36c0e

Contents?: true

Size: 764 Bytes

Versions: 1

Compression:

Stored size: 764 Bytes

Contents

begin
  require 'rubygems'
rescue LoadError
end

begin
  require 'unidecode'
rescue LoadError
end

class Slug < ::String
  Slug::Version = '0.0.1'

  def Slug.version
    '0.0.1'
  end

  Join = '-'

  def Slug.for(*args)
    options = args.last.is_a?(Hash) ? args.pop : {}
    join = options[:join]||options['join']||Join
    string = args.flatten.compact.join(join)
    string = unidecode(string)
    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

  unless defined?(Unidecoder)
    def Slug.unidecode(string)
      string
    end
  else
    def Slug.unidecode(string)
      Unidecoder.decode(string)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dao-4.2.1 lib/dao/slug.rb