Sha256: 5f488a2b13dce85d9d5b70a758feef6e0138d1109d2a22fdd52563559a9574c0

Contents?: true

Size: 1.59 KB

Versions: 14

Compression:

Stored size: 1.59 KB

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

  def parameterize!(sep = '_')
    replace(self.parameterize(sep))
  end

end

## Hash

class Hash

  def underscore_keys
    new_hash = {}

    self.each_pair do |key, value|
      if value.respond_to?(:collect!) # Array
        value.collect do |item|
          if item.respond_to?(:each_pair) # Hash item within
            item.underscore_keys
          else
            item
          end
        end
      elsif value.respond_to?(:each_pair) # Hash
        value = value.underscore_keys
      end

      new_key = key.is_a?(String) ? key.underscore : key # only String keys

      new_hash[new_key] = value
    end

    self.replace(new_hash)
  end

end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
locomotive_cms-1.0.0.beta.2 lib/locomotive/core_ext.rb
locomotive_cms-1.0.0.beta lib/locomotive/core_ext.rb
locomotive_cms-0.0.4.beta12 lib/locomotive/core_ext.rb
locomotive_cms-0.0.4.beta11 lib/locomotive/core_ext.rb
locomotive_cms-0.0.4.beta10 lib/locomotive/core_ext.rb
locomotive_cms-0.0.4.beta9 lib/locomotive/core_ext.rb
locomotive_cms-0.0.4.beta8 lib/locomotive/core_ext.rb
locomotive_cms-0.0.4.beta7 lib/locomotive/core_ext.rb
locomotive_cms-0.0.4.beta5 lib/locomotive/core_ext.rb
locomotive_cms-0.0.4.beta4 lib/locomotive/core_ext.rb
locomotive_cms-0.0.4.beta3 lib/locomotive/core_ext.rb
locomotive_cms-0.0.4.beta2 lib/locomotive/core_ext.rb
locomotive_cms-0.0.4.beta1 lib/locomotive/core_ext.rb
locomotive_cms-0.0.4 lib/locomotive/core_ext.rb