Module: DaHuang::StringExt
- Defined in:
- lib/string_ext.rb
Instance Method Summary collapse
- #inspect ⇒ Object
- #is_numeric? ⇒ Boolean
- #normalize ⇒ Object
- #to_search_string ⇒ Object
- #to_url_string ⇒ Object
-
#urlize(options = {}) ⇒ Object
Convert a string to a format suitable for a URL without ever using escaped characters.
Instance Method Details
#inspect ⇒ Object
20 21 22 |
# File 'lib/string_ext.rb', line 20 def inspect "'" + to_s + "'" end |
#is_numeric? ⇒ Boolean
7 8 9 |
# File 'lib/string_ext.rb', line 7 def is_numeric? /^\d+$/.match(self) ? true : false end |
#normalize ⇒ Object
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_string ⇒ Object
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_string ⇒ Object
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( = {}) [:downcase] ||= true [:convert_spaces] ||= false [:regexp] ||= /[^-_A-Za-z0-9]/ str = self.strip.normalize str.downcase! if [:downcase] str.gsub!(/\ /,'_') if [:convert_spaces] str.gsub([:regexp], '') end |