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
61 62 63 |
# File 'lib/dm_ruby_extensions/extend_string.rb', line 61 def absolute_url? include?('://') || start_with?('/') ? true : false end |
#as_boolean ⇒ Object
13 14 15 |
# File 'lib/dm_ruby_extensions/extend_string.rb', line 13 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
19 20 21 22 23 |
# File 'lib/dm_ruby_extensions/extend_string.rb', line 19 def as_css_size size = self size += 'px' unless size.blank? || size.end_with?('px', '%', 'em') || size == 'auto' || size == 'inherit' size end |
#expand_url(path = '') ⇒ Object
if a relative url path is given, then expand it by prepending the supplied path.
53 54 55 56 57 |
# File 'lib/dm_ruby_extensions/extend_string.rb', line 53 def (path = '') return self if blank? || absolute_url? path.end_with?('/') ? "#{path}#{self}" : "#{path}/#{self}" 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.
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 185 186 187 188 189 |
# File 'lib/dm_ruby_extensions/extend_string.rb', line 130 def name_case( = {}) = { lazy: true, irish: true }.merge # Skip if string is mixed case if [:lazy] first_letter_lower = self[0] == downcase[0] all_lower_or_upper = downcase == self || upcase == self return self unless first_letter_lower || all_lower_or_upper end localstring = downcase localstring.gsub!(/\b\w/, &:upcase) localstring.gsub!(/'\w\b/, &:downcase) # Lowercase 's if [:irish] if localstring =~ (/\bMac[A-Za-z]{2,}[^aciozj]\b/) || 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, &:upcase ) localstring end |
#name_case! ⇒ Object
Modifies str in place and properly namecases the string.
193 194 195 |
# File 'lib/dm_ruby_extensions/extend_string.rb', line 193 def name_case! gsub!(self, name_case) end |
#replace_non_alphanumeric(replacement = '') ⇒ Object
Replace non-alphanumbic character
33 34 35 |
# File 'lib/dm_ruby_extensions/extend_string.rb', line 33 def replace_non_alphanumeric(replacement = '') gsub(/[^\w.-]/, replacement) end |
#sanitize_filename ⇒ Object
Santize the string
Note: File.basename doesn't work right with Windows paths on Unix
40 41 42 43 44 45 46 47 48 |
# File 'lib/dm_ruby_extensions/extend_string.rb', line 40 def sanitize_filename name = strip #--- get only the filename, not the whole path name.gsub!(%r{^.*(\\|/)}, '') #--- Finally, replace all non alphanumeric, underscore or periods with underscore name.gsub!(/[^\w.-]/, '_') name end |
#smart_capitalize ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/dm_ruby_extensions/extend_string.rb', line 87 def smart_capitalize result = dup # ignore any leading crazy characters and capitalize the first real character return result unless self =~ /^['"(\[']*([a-z])/ i = index(::Regexp.last_match(1)) x = result[i, result.length] # word with capitals and periods mid-word are left alone result[i, 1] = result[i, 1].upcase unless x =~ (/[A-Z]/) || x =~ (/\.\w+/) result end |
#smart_capitalize! ⇒ Object
103 104 105 |
# File 'lib/dm_ruby_extensions/extend_string.rb', line 103 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
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/dm_ruby_extensions/extend_string.rb', line 70 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/) { ": #{::Regexp.last_match(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
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/dm_ruby_extensions/extend_string.rb', line 112 def smart_truncate(opts = {}) opts = { words: 12 }.merge(opts) if opts[:sentences] result = split(/\.(\s|$)+/).reject { |s| s.strip.empty? }[0, opts[:sentences]] return "#{result.map(&:strip).join('. ')}..." end a = 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
27 28 29 |
# File 'lib/dm_ruby_extensions/extend_string.rb', line 27 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
8 9 10 |
# File 'lib/dm_ruby_extensions/extend_string.rb', line 8 def to_s_default(default_str = 'n/a') empty? || strip.empty? ? default_str : to_s end |