Sha256: 41c89fdaffe249c0b12f28621c320bb1967681cd9e71043c61ce953516481071

Contents?: true

Size: 1.33 KB

Versions: 8

Compression:

Stored size: 1.33 KB

Contents

module DaHuang
  module StringExt
    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

    def inspect
      "'" + to_s + "'"
    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

end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
da_huangs_ruby_extensions-0.0.25 lib/string_ext.rb
da_huangs_ruby_extensions-0.0.24 lib/string_ext.rb
da_huangs_ruby_extensions-0.0.23 lib/string_ext.rb
da_huangs_ruby_extensions-0.0.22 lib/string_ext.rb
da_huangs_ruby_extensions-0.0.21 lib/string_ext.rb
da_huangs_ruby_extensions-0.0.20 lib/string_ext.rb
da_huangs_ruby_extensions-0.0.19 lib/string_ext.rb
da_huangs_ruby_extensions-0.0.18 lib/string_ext.rb