Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/dm_ruby_extensions/extend_string.rb
Overview
If the string is empty, then return a default string
Instance Method Summary collapse
-
#absolute_url? ⇒ Boolean
Test if a url is absolute ——————————————————————————.
-
#as_boolean ⇒ Object
——————————————————————————.
-
#as_css_size ⇒ Object
given a css type of size (like a width), make it into a valid css value ——————————————————————————.
-
#expand_url(path = '') ⇒ Object
if a relative url path is given, then expand it by prepending the supplied path.
-
#name_case(options = {}) ⇒ Object
github.com/tenderlove/namecase NameCase is a Ruby implementation of Lingua::EN::NameCase, a library for converting strings/names to be properly cased.
-
#name_case! ⇒ Object
Modifies str in place and properly namecases the string.
-
#replace_non_alphanumeric(replacement = '') ⇒ Object
Replace non-alphanumbic character ——————————————————————————.
-
#sanitize_filename ⇒ Object
Santize the string Note: File.basename doesn’t work right with Windows paths on Unix ——————————————————————————.
-
#smart_capitalize ⇒ Object
——————————————————————————.
-
#smart_capitalize! ⇒ Object
——————————————————————————.
-
#smart_titlecase ⇒ Object
Ruby version of John Gruber’s TitleCase.pl, from github.com/samsouder/titlecase Used for better capitalizing titles and sentences ——————————————————————————.
-
#smart_truncate(opts = {}) ⇒ Object
Truncate the string to a set or words, or sentences.
-
#sql_wildcard ⇒ Object
Adds SQL wildcard cahracters to begin/end of string for use in LIKE statements ——————————————————————————.
-
#to_s_default(default_str = 'n/a') ⇒ Object
If the string is empty, return a default value, otherwise the string ——————————————————————————.
Instance Method Details
#absolute_url? ⇒ Boolean
Test if a url is absolute
62 63 64 |
# File 'lib/dm_ruby_extensions/extend_string.rb', line 62 def absolute_url? (self.include?('://') || self.start_with?('/')) ? true : false end |
#as_boolean ⇒ Object
12 13 14 |
# File 'lib/dm_ruby_extensions/extend_string.rb', line 12 def as_boolean (self == 'true' || self == 'yes' || self == '1') ? true : false end |
#as_css_size ⇒ Object
given a css type of size (like a width), make it into a valid css value
18 19 20 21 22 |
# File 'lib/dm_ruby_extensions/extend_string.rb', line 18 def as_css_size size = self size += 'px' unless size.blank? || size.end_with?('px', '%', 'em') || size == 'auto' || size == 'inherit' return size end |
#expand_url(path = '') ⇒ Object
if a relative url path is given, then expand it by prepending the supplied path.
52 53 54 55 56 57 58 |
# File 'lib/dm_ruby_extensions/extend_string.rb', line 52 def (path = '') if self.blank? || self.absolute_url? return self else return path.end_with?('/') ? "#{path}#{self}" : "#{path}/#{self}" end end |
#name_case(options = {}) ⇒ Object
github.com/tenderlove/namecase NameCase is a Ruby implementation of Lingua::EN::NameCase, a library for converting strings/names to be properly cased. This is good for converting denormalized data to human friendly data.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/dm_ruby_extensions/extend_string.rb', line 125 def name_case( = {}) = { :lazy => true, :irish => true }.merge # Skip if string is mixed case if [:lazy] first_letter_lower = self[0] == self.downcase[0] all_lower_or_upper = (self.downcase == self || self.upcase == self) return self unless first_letter_lower || all_lower_or_upper end localstring = downcase localstring.gsub!(/\b\w/) { |first| first.upcase } localstring.gsub!(/\'\w\b/) { |c| c.downcase } # Lowercase 's if [:irish] if localstring =~ /\bMac[A-Za-z]{2,}[^aciozj]\b/ or localstring =~ /\bMc/ match = localstring.match(/\b(Ma?c)([A-Za-z]+)/) localstring.gsub!(/\bMa?c[A-Za-z]+/) { match[1] + match[2].capitalize } # Now fix "Mac" exceptions localstring.gsub!(/\bMacEdo/, 'Macedo') localstring.gsub!(/\bMacEvicius/, 'Macevicius') localstring.gsub!(/\bMacHado/, 'Machado') localstring.gsub!(/\bMacHar/, 'Machar') localstring.gsub!(/\bMacHin/, 'Machin') localstring.gsub!(/\bMacHlin/, 'Machlin') localstring.gsub!(/\bMacIas/, 'Macias') localstring.gsub!(/\bMacIulis/, 'Maciulis') localstring.gsub!(/\bMacKie/, 'Mackie') localstring.gsub!(/\bMacKle/, 'Mackle') localstring.gsub!(/\bMacKlin/, 'Macklin') localstring.gsub!(/\bMacKmin/, 'Mackmin') localstring.gsub!(/\bMacQuarie/, 'Macquarie') end localstring.gsub!('Macmurdo','MacMurdo') end # Fixes for "son (daughter) of" etc localstring.gsub!(/\bAl(?=\s+\w)/, 'al') # al Arabic or forename Al. localstring.gsub!(/\bAp\b/, 'ap') # ap Welsh. localstring.gsub!(/\bBen(?=\s+\w)/,'ben') # ben Hebrew or forename Ben. localstring.gsub!(/\bDell([ae])\b/,'dell\1') # della and delle Italian. localstring.gsub!(/\bD([aeiou])\b/,'d\1') # da, de, di Italian; du French; do Brasil localstring.gsub!(/\bD([ao]s)\b/,'d\1') # das, dos Brasileiros localstring.gsub!(/\bDe([lr])\b/,'de\1') # del Italian; der Dutch/Flemish. localstring.gsub!(/\bEl\b/,'el') # el Greek or El Spanish. localstring.gsub!(/\bLa\b/,'la') # la French or La Spanish. localstring.gsub!(/\bL([eo])\b/,'l\1') # lo Italian; le French. localstring.gsub!(/\bVan(?=\s+\w)/,'van') # van German or forename Van. localstring.gsub!(/\bVon\b/,'von') # von Dutch/Flemish # Fix roman numeral names localstring.gsub!( / \b ( (?: [Xx]{1,3} | [Xx][Ll] | [Ll][Xx]{0,3} )? (?: [Ii]{1,3} | [Ii][VvXx] | [Vv][Ii]{0,3} )? ) \b /x ) { |match| match.upcase } localstring end |
#name_case! ⇒ Object
Modifies str in place and properly namecases the string.
188 189 190 |
# File 'lib/dm_ruby_extensions/extend_string.rb', line 188 def name_case! self.gsub!(self, self.name_case) end |
#replace_non_alphanumeric(replacement = '') ⇒ Object
Replace non-alphanumbic character
32 33 34 |
# File 'lib/dm_ruby_extensions/extend_string.rb', line 32 def replace_non_alphanumeric(replacement = '') self.gsub /[^\w\.\-]/, replacement end |
#sanitize_filename ⇒ Object
Santize the string
Note: File.basename doesn't work right with Windows paths on Unix
39 40 41 42 43 44 45 46 47 |
# File 'lib/dm_ruby_extensions/extend_string.rb', line 39 def sanitize_filename name = self.strip #--- get only the filename, not the whole path name.gsub! /^.*(\\|\/)/, '' #--- Finally, replace all non alphanumeric, underscore or periods with underscore name.gsub! /[^\w\.\-]/, '_' return name end |
#smart_capitalize ⇒ Object
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/dm_ruby_extensions/extend_string.rb', line 88 def smart_capitalize # ignore any leading crazy characters and capitalize the first real character if self =~ /^['"\(\[']*([a-z])/ i = index($1) x = self[i,self.length] # word with capitals and periods mid-word are left alone self[i,1] = self[i,1].upcase unless x =~ /[A-Z]/ or x =~ /\.\w+/ end self end |
#smart_capitalize! ⇒ Object
100 101 102 |
# File 'lib/dm_ruby_extensions/extend_string.rb', line 100 def smart_capitalize! replace(smart_capitalize) end |
#smart_titlecase ⇒ Object
Ruby version of John Gruber’s TitleCase.pl, from github.com/samsouder/titlecase Used for better capitalizing titles and sentences
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/dm_ruby_extensions/extend_string.rb', line 71 def smart_titlecase small_words = %w(a an and as at but by en for if in of on or the to v v. via vs vs. von) x = split(" ").map do |word| # note: word could contain non-word characters! # downcase all small_words, capitalize the rest small_words.include?(word.gsub(/\W/, "").downcase) ? word.downcase! : word.smart_capitalize! word end # capitalize first and last words x.first.smart_capitalize! x.last.smart_capitalize! # small words after colons are capitalized x.join(" ").gsub(/:\s?(\W*#{small_words.join("|")}\W*)\s/) { ": #{$1.smart_capitalize} " } end |
#smart_truncate(opts = {}) ⇒ Object
Truncate the string to a set or words, or sentences. string.smart_truncate(:sentences => 3) string.smart_truncate(:words => 12) From stackoverflow.com/questions/1293573/rails-smart-text-truncation
109 110 111 112 113 114 115 116 117 |
# File 'lib/dm_ruby_extensions/extend_string.rb', line 109 def smart_truncate(opts = {}) opts = {:words => 12}.merge(opts) if opts[:sentences] return self.split(/\.(\s|$)+/).reject{ |s| s.strip.empty? }[0, opts[:sentences]].map{|s| s.strip}.join('. ') + '...' end a = self.split(/\s/) # or /[ ]+/ to only split on spaces n = opts[:words] a[0...n].join(' ') + (a.size > n ? '...' : '') end |
#sql_wildcard ⇒ Object
Adds SQL wildcard cahracters to begin/end of string for use in LIKE statements
26 27 28 |
# File 'lib/dm_ruby_extensions/extend_string.rb', line 26 def sql_wildcard "%#{self}%" end |
#to_s_default(default_str = 'n/a') ⇒ Object
If the string is empty, return a default value, otherwise the string
7 8 9 |
# File 'lib/dm_ruby_extensions/extend_string.rb', line 7 def to_s_default(default_str = 'n/a') (empty? || strip.empty?) ? default_str : self.to_s end |