Class: String
Instance Method Summary collapse
- #at(position) ⇒ Object
- #camelize(first_letter = :upper) ⇒ Object (also: #camelcase)
- #camelize!(first_letter = :upper) ⇒ Object (also: #camelcase!)
- #classify ⇒ Object
- #classify! ⇒ Object
- #dasherize ⇒ Object
- #dasherize! ⇒ Object
- #demodulize ⇒ Object
- #demodulize! ⇒ Object
- #domain ⇒ Object
- #downcase? ⇒ Boolean
- #ellipsize(ellipsize_at, options = {}) ⇒ Object
- #exclude?(string) ⇒ Boolean
- #first(limit = 1) ⇒ Object
- #from(position) ⇒ Object
- #humanize(options = {}) ⇒ Object
- #humanize!(options = {}) ⇒ Object
- #indent(amount, indent_string = nil, indent_empty_lines = false) ⇒ Object
- #indent!(amount, indent_string = nil, indent_empty_lines = false) ⇒ Object
- #last(limit = 1) ⇒ Object
- #mixedcase? ⇒ Boolean
- #ordinal ⇒ Object
- #ordinalize ⇒ Object
- #parameterize(sep = "-") ⇒ Object
- #parameterize!(sep = "-") ⇒ Object
- #pollute(delimiter = "^--^--^") ⇒ Object
- #remove(*patterns) ⇒ Object
- #remove!(*patterns) ⇒ Object
- #remove_tags ⇒ Object
- #remove_tags! ⇒ Object
- #shift(*patterns) ⇒ Object
- #shift!(*patterns) ⇒ Object
- #slugify ⇒ Object
- #slugify! ⇒ Object
- #squish ⇒ Object
- #squish! ⇒ Object
- #titleize ⇒ Object (also: #titlecase)
- #titleize! ⇒ Object (also: #titlecase!)
- #to(position) ⇒ Object
- #truncate(truncate_at, options = {}) ⇒ Object
- #truncate_words(words_count, options = {}) ⇒ Object
- #underscore ⇒ Object
- #underscore! ⇒ Object
- #unpollute(delimiter = "^--^--^") ⇒ Object
- #upcase? ⇒ Boolean
Instance Method Details
#at(position) ⇒ Object
4 5 6 |
# File 'lib/active_object/string.rb', line 4 def at(position) self[position] end |
#camelize(first_letter = :upper) ⇒ Object Also known as: camelcase
10 11 12 13 14 15 16 |
# File 'lib/active_object/string.rb', line 10 def camelize(first_letter=:upper) if first_letter != :lower to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } else to_s.first.chr.downcase + camelize(self)[1..-1] end end |
#camelize!(first_letter = :upper) ⇒ Object Also known as: camelcase!
22 23 24 |
# File 'lib/active_object/string.rb', line 22 def camelize!(first_letter=:upper) replace(camelize(first_letter)) end |
#classify ⇒ Object
30 31 32 |
# File 'lib/active_object/string.rb', line 30 def classify to_s.sub(/.*\./, "").camelize end |
#classify! ⇒ Object
36 37 38 |
# File 'lib/active_object/string.rb', line 36 def classify! replace(classify) end |
#dasherize ⇒ Object
42 43 44 |
# File 'lib/active_object/string.rb', line 42 def dasherize gsub(/_/, "-") end |
#dasherize! ⇒ Object
48 49 50 |
# File 'lib/active_object/string.rb', line 48 def dasherize! replace(dasherize) end |
#demodulize ⇒ Object
54 55 56 |
# File 'lib/active_object/string.rb', line 54 def demodulize to_s.gsub(/^.*::/, "") end |
#demodulize! ⇒ Object
60 61 62 |
# File 'lib/active_object/string.rb', line 60 def demodulize! replace(demodulize) end |
#domain ⇒ Object
65 66 67 68 |
# File 'lib/active_object/string.rb', line 65 def domain string = dup string =~ (/^(?:\w+:\/\/)?([^\/?]+)(?:\/|\?|$)/) ? $1 : string end |
#downcase? ⇒ Boolean
70 71 72 |
# File 'lib/active_object/string.rb', line 70 def downcase? downcase == self end |
#ellipsize(ellipsize_at, options = {}) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/active_object/string.rb', line 74 def ellipsize(ellipsize_at, ={}) return(self)if size <= ellipsize_at separator = .fetch(:separator, "...") offset = .fetch(:offset, 4) "#{self[0,offset]}#{separator}#{self[-offset,offset]}" end |
#exclude?(string) ⇒ Boolean
83 84 85 |
# File 'lib/active_object/string.rb', line 83 def exclude?(string) !include?(string) end |
#first(limit = 1) ⇒ Object
89 90 91 92 |
# File 'lib/active_object/string.rb', line 89 def first(limit=1) return("") if limit.zero? limit >= size ? self.dup : to(limit - 1) end |
#from(position) ⇒ Object
96 97 98 |
# File 'lib/active_object/string.rb', line 96 def from(position) self[position..-1] end |
#humanize(options = {}) ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/active_object/string.rb', line 102 def humanize( = {}) underscore. gsub(/_id\z/, ""). tr("_", " "). gsub(/([a-z\d]*)/i) { |s| s.downcase }. gsub(/\A\w/) { |s| .fetch(:capitalize, true) ? s.upcase : s } end |
#humanize!(options = {}) ⇒ Object
112 113 114 |
# File 'lib/active_object/string.rb', line 112 def humanize!(={}) replace(humanize()) end |
#indent(amount, indent_string = nil, indent_empty_lines = false) ⇒ Object
118 119 120 121 122 |
# File 'lib/active_object/string.rb', line 118 def indent(amount, indent_string=nil, indent_empty_lines=false) indent_string = indent_string || self[/^[ \t]/] || " " substitutes = indent_empty_lines ? /^/ : /^(?!$)/ gsub(substitutes, indent_string * amount) end |
#indent!(amount, indent_string = nil, indent_empty_lines = false) ⇒ Object
126 127 128 |
# File 'lib/active_object/string.rb', line 126 def indent!(amount, indent_string=nil, indent_empty_lines=false) replace(indent(amount, indent_string, indent_empty_lines)) end |
#last(limit = 1) ⇒ Object
132 133 134 135 |
# File 'lib/active_object/string.rb', line 132 def last(limit=1) return("") if limit.zero? limit >= size ? self.dup : from(-limit) end |
#mixedcase? ⇒ Boolean
138 139 140 |
# File 'lib/active_object/string.rb', line 138 def mixedcase? !upcase? && !downcase? end |
#ordinal ⇒ Object
143 144 145 |
# File 'lib/active_object/string.rb', line 143 def ordinal to_i.ordinal end |
#ordinalize ⇒ Object
149 150 151 |
# File 'lib/active_object/string.rb', line 149 def ordinalize to_i.ordinalize end |
#parameterize(sep = "-") ⇒ Object
155 156 157 158 159 160 |
# File 'lib/active_object/string.rb', line 155 def parameterize(sep="-") underscore. gsub(/\s+/, "_"). gsub("_", sep). downcase end |
#parameterize!(sep = "-") ⇒ Object
164 165 166 |
# File 'lib/active_object/string.rb', line 164 def parameterize!(sep="-") replace(parameterize(sep)) end |
#pollute(delimiter = "^--^--^") ⇒ Object
169 170 171 |
# File 'lib/active_object/string.rb', line 169 def pollute(delimiter="^--^--^") split('').map { |c| "#{c}#{delimiter}" }.join end |
#remove(*patterns) ⇒ Object
174 175 176 177 178 179 180 |
# File 'lib/active_object/string.rb', line 174 def remove(*patterns) string = dup patterns.each do |pattern| string.gsub!(pattern, "") end string end |
#remove!(*patterns) ⇒ Object
184 185 186 |
# File 'lib/active_object/string.rb', line 184 def remove!(*patterns) replace(remove(*patterns)) end |
#remove_tags ⇒ Object
189 190 191 |
# File 'lib/active_object/string.rb', line 189 def gsub(/<\/?[^>]*>/, "") end |
#remove_tags! ⇒ Object
193 194 195 |
# File 'lib/active_object/string.rb', line 193 def replace() end |
#shift(*patterns) ⇒ Object
197 198 199 200 201 202 203 |
# File 'lib/active_object/string.rb', line 197 def shift(*patterns) string = dup patterns.each do |pattern| string.sub!(pattern, "") end string end |
#shift!(*patterns) ⇒ Object
205 206 207 |
# File 'lib/active_object/string.rb', line 205 def shift!(*patterns) replace(shift(*patterns)) end |
#slugify ⇒ Object
209 210 211 212 213 214 215 |
# File 'lib/active_object/string.rb', line 209 def slugify gsub(/[^\x00-\x7F]+/, ''). gsub(/[^\w_ \-]+/i, ''). gsub(/[ \-]+/i, '-'). gsub(/^\-|\-$/i, ''). downcase end |
#slugify! ⇒ Object
217 218 219 |
# File 'lib/active_object/string.rb', line 217 def slugify! replace(slugify) end |
#squish ⇒ Object
222 223 224 |
# File 'lib/active_object/string.rb', line 222 def squish strip.gsub(/\s+/, ' ') end |
#squish! ⇒ Object
228 229 230 |
# File 'lib/active_object/string.rb', line 228 def squish! replace(squish) end |
#titleize ⇒ Object Also known as: titlecase
234 235 236 237 238 |
# File 'lib/active_object/string.rb', line 234 def titleize underscore. humanize. gsub(/\b(?<!['’`])[a-z]/) { $&.capitalize } end |
#titleize! ⇒ Object Also known as: titlecase!
244 245 246 |
# File 'lib/active_object/string.rb', line 244 def titleize! replace(titleize) end |
#to(position) ⇒ Object
252 253 254 |
# File 'lib/active_object/string.rb', line 252 def to(position) self[0..position] end |
#truncate(truncate_at, options = {}) ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/active_object/string.rb', line 258 def truncate(truncate_at, ={}) return(dup) unless length > truncate_at omission = .fetch(:omission, "...") length_with_room_for_omission = truncate_at - omission.length stop = if .fetch(:separator, false) rindex(.fetch(:separator, ''), length_with_room_for_omission) || length_with_room_for_omission else length_with_room_for_omission end "#{self[0, stop]}#{omission}" end |
#truncate_words(words_count, options = {}) ⇒ Object
275 276 277 278 279 280 281 282 283 284 |
# File 'lib/active_object/string.rb', line 275 def truncate_words(words_count, ={}) sep = [:separator] || /\s+/ sep = Regexp.escape(sep.to_s) unless Regexp === sep if self =~ /\A((?:.+?#{sep}){#{words_count - 1}}.+?)#{sep}.*/m $1 + ([:omission] || '...') else dup end end |
#underscore ⇒ Object
288 289 290 291 292 293 294 |
# File 'lib/active_object/string.rb', line 288 def underscore gsub(/::/, '/'). gsub(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end |
#underscore! ⇒ Object
298 299 300 |
# File 'lib/active_object/string.rb', line 298 def underscore! replace(underscore) end |
#unpollute(delimiter = "^--^--^") ⇒ Object
303 304 305 |
# File 'lib/active_object/string.rb', line 303 def unpollute(delimiter="^--^--^") gsub(delimiter, "") end |
#upcase? ⇒ Boolean
307 308 309 |
# File 'lib/active_object/string.rb', line 307 def upcase? upcase == self end |