Sha256: 78cc665bf3b69b4e2a577676d8d0f059d3caef5e0a4b77cf76a3d077e640cc3e

Contents?: true

Size: 761 Bytes

Versions: 15

Compression:

Stored size: 761 Bytes

Contents

module Ro
  class Slug < ::String
    Join = '-'

    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(join)
      string = unidecode(string)
      words = string.to_s.scan(%r|[/\w]+|)
      words.map!{|word| word.gsub %r|[_-]|, join}
      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.gsub('/', (join * 2)))
    end

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

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
ro-1.4.6 lib/ro/slug.rb
ro-1.4.4 lib/ro/slug.rb
ro-1.4.3 lib/ro/slug.rb
ro-1.4.2 lib/ro/slug.rb
ro-1.4.1 lib/ro/slug.rb
ro-1.4.0 lib/ro/slug.rb
ro-1.3.8 lib/ro/slug.rb
ro-1.3.7 lib/ro/slug.rb
ro-1.3.6 lib/ro/slug.rb
ro-1.3.5 lib/ro/slug.rb
ro-1.3.4 lib/ro/slug.rb
ro-1.3.3 lib/ro/slug.rb
ro-1.3.2 lib/ro/slug.rb
ro-1.3.1 lib/ro/slug.rb
ro-1.2.0 lib/ro/slug.rb