Sha256: 014ef72638d9e49959cfa8a6919a66e0c8eea203652513789ec0c179ad55913a

Contents?: true

Size: 1.2 KB

Versions: 4

Compression:

Stored size: 1.2 KB

Contents

class String
  def normalize
    ActiveSupport::Multibyte::Chars.new(self).mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/n,'').to_s
  end

  def is_numeric?
    /^\d+$/.match(self) ? true : false
  end

  def to_search_string
    a = gsub(/[-_+]/, " ").gsub(/%20/, " ").downcase
    a.index("%") ? a : "%#{a}"
  end

  def to_url_string
    titleize.gsub(" ", "_")
  end

  # Convert a string to a format suitable for a URL without ever using escaped characters.
  # It calls strip, removeaccents, downcase (optional) then removes the spaces (optional)
  # and finally removes any characters matching the default regexp (/[^-_A-Za-z0-9]/).
  #
  # Options
  #
  # * :downcase => call downcase on the string (defaults to true)
  # * :convert_spaces => Convert space to underscore (defaults to false)
  # * :regexp => The regexp matching characters that will be converting to an empty string (defaults to /[^-_A-Za-z0-9]/)
  def urlize(options = {})
    options[:downcase] ||= true
    options[:convert_spaces] ||= false
    options[:regexp] ||= /[^-_A-Za-z0-9]/

    str = self.strip.normalize
    str.downcase! if options[:downcase]
    str.gsub!(/\ /,'_') if options[:convert_spaces]
    str.gsub(options[:regexp], '')
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
da_huangs_ruby_extensions-0.0.6 lib/string_ext.rb
da_huangs_ruby_extensions-0.0.5 lib/string_ext.rb
da_huangs_ruby_extensions-0.0.4 lib/string.rb
da_huangs_ruby_extensions-0.0.1 lib/string.rb