Sha256: 63d59042a0ded5008d0e1d78e4658d820f6879e241d3fa5815f4498cd134bbbe

Contents?: true

Size: 978 Bytes

Versions: 14

Compression:

Stored size: 978 Bytes

Contents

## String
class String
  # def perma_string(sep = '_')
  #   ActiveSupport::Inflector.parameterize(self, sep).to_s
  # end
  
  def slugify(options = {})
    options = { :sep => '_', :without_extension => false, :downcase => false, :underscore => false }.merge(options)
    # replace accented chars with ther ascii equivalents
    s = ActiveSupport::Inflector.transliterate(self).to_s
    # No more than one slash in a row
    s.gsub!(/(\/[\/]+)/, '/')
    # Remove leading or trailing space
    s.strip!
    # Remove leading or trailing slash
    s.gsub! /(^[\/]+)|([\/]+$)/, ''
    # Remove extensions
    s.gsub! /(\.[a-zA-Z]{2,})/, '' if options[:without_extension]
    # Downcase
    s.downcase! if options[:downcase]
    # Turn unwanted chars into the seperator
    s.gsub!(/[^a-zA-Z0-9\-_\+\/]+/i, options[:sep])
    # Underscore
    s.gsub!(/[\-]/i, '_') if options[:underscore]
    s
  end
  
  def slugify!(options = {})
    replace(self.slugify(options))
  end
  
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
locomotive_cms-0.0.2.8 lib/core_ext.rb
locomotive_cms-0.0.2.7 lib/core_ext.rb
locomotive_cms-0.0.2.6 lib/core_ext.rb
locomotive_cms-0.0.2.5 lib/core_ext.rb
locomotive_cms-0.0.2.4 lib/core_ext.rb
locomotive_cms-0.0.2.3 lib/core_ext.rb
locomotive_cms-0.0.2.2 lib/core_ext.rb
locomotive_cms-0.0.2.1 lib/core_ext.rb
locomotive_cms-0.0.2 lib/core_ext.rb
locomotive_cms-0.0.1.4 lib/core_ext.rb
locomotive_cms-0.0.1.3 lib/core_ext.rb
locomotive_cms-0.0.1.2 lib/core_ext.rb
locomotive_cms-0.0.1.1 lib/core_ext.rb
locomotive_cms-0.0.1 lib/core_ext.rb