Module: DaHuang::StringExt

Defined in:
lib/string_ext.rb

Instance Method Summary collapse

Instance Method Details

#inspectObject



20
21
22
# File 'lib/string_ext.rb', line 20

def inspect
  "'" + to_s + "'"
end

#is_numeric?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/string_ext.rb', line 7

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

#normalizeObject



3
4
5
# File 'lib/string_ext.rb', line 3

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

#to_search_stringObject



11
12
13
14
# File 'lib/string_ext.rb', line 11

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

#to_url_stringObject



16
17
18
# File 'lib/string_ext.rb', line 16

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

#urlize(options = {}) ⇒ Object

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]/)



33
34
35
36
37
38
39
40
41
42
# File 'lib/string_ext.rb', line 33

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