Module: ActiveObject::String
- Defined in:
- lib/active_object/string.rb
Instance Method Summary collapse
- #any?(*keys) ⇒ Boolean
- #at(position) ⇒ Object
- #camelize(first_letter = :upper) ⇒ Object (also: #camelcase)
- #camelize!(first_letter = :upper) ⇒ Object (also: #camelcase!)
- #classify ⇒ Object
- #classify! ⇒ Object
- #constantize ⇒ Object
- #dasherize ⇒ Object
- #dasherize! ⇒ Object
- #deconstantize ⇒ Object
- #deconstantize! ⇒ Object
- #demodulize ⇒ Object
- #demodulize! ⇒ Object
- #domain ⇒ Object
- #downcase? ⇒ Boolean
- #ellipsize(ellipsize_at, options = {}) ⇒ Object
- #exclude?(string) ⇒ Boolean
- #first(limit = 1) ⇒ Object
- #format(*args) ⇒ 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
- #index_all(pattern) ⇒ Object
- #labelize(options = {}) ⇒ Object (also: #labelcase)
- #labelize!(options = {}) ⇒ Object (also: #labelcase!)
- #last(limit = 1) ⇒ Object
- #mixedcase? ⇒ Boolean
- #ordinal ⇒ Object
- #ordinalize ⇒ Object
- #parameterize(seperator = "-") ⇒ Object
- #parameterize!(seperator = "-") ⇒ Object
- #pollute(delimiter = "^--^--^") ⇒ Object
- #pollute!(delimiter = "^--^--^") ⇒ Object
- #pop ⇒ Object
- #push(string) ⇒ Object
- #remove(*patterns) ⇒ Object
- #remove!(*patterns) ⇒ Object
- #remove_tags ⇒ Object
- #remove_tags! ⇒ Object
- #sample(separator = " ") ⇒ Object
- #sample!(separator = " ") ⇒ Object
- #shift(*patterns) ⇒ Object
- #shift!(*patterns) ⇒ Object
- #shuffle(separator = "") ⇒ Object
- #shuffle!(separator = "") ⇒ Object
- #sift(chars_to_keep) ⇒ Object
- #sift!(chars_to_keep) ⇒ Object
- #slugify ⇒ Object
- #slugify! ⇒ Object
- #sort ⇒ Object
- #sort! ⇒ 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
- #unpollute!(delimiter = "^--^--^") ⇒ Object
- #unshift(*patterns) ⇒ Object
- #unshift!(*patterns) ⇒ Object
- #upcase? ⇒ Boolean
Instance Method Details
#any?(*keys) ⇒ Boolean
3 4 5 6 7 |
# File 'lib/active_object/string.rb', line 3 def any?(*keys) included = false keys.flatten.each { |k| break if included = include?(k) } included end |
#at(position) ⇒ Object
9 10 11 |
# File 'lib/active_object/string.rb', line 9 def at(position) self[position] end |
#camelize(first_letter = :upper) ⇒ Object Also known as: camelcase
13 14 15 16 17 18 19 |
# File 'lib/active_object/string.rb', line 13 def camelize(first_letter=:upper) if first_letter.to_sym != :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!
23 24 25 |
# File 'lib/active_object/string.rb', line 23 def camelize!(first_letter=:upper) replace(camelize(first_letter)) end |
#classify ⇒ Object
29 30 31 |
# File 'lib/active_object/string.rb', line 29 def classify to_s.sub(/.*\./, "").camelize end |
#classify! ⇒ Object
33 34 35 |
# File 'lib/active_object/string.rb', line 33 def classify! replace(classify) end |
#constantize ⇒ Object
37 38 39 |
# File 'lib/active_object/string.rb', line 37 def constantize Object.const_get(self) end |
#dasherize ⇒ Object
41 42 43 |
# File 'lib/active_object/string.rb', line 41 def dasherize gsub(/_/, "-") end |
#dasherize! ⇒ Object
45 46 47 |
# File 'lib/active_object/string.rb', line 45 def dasherize! replace(dasherize) end |
#deconstantize ⇒ Object
49 50 51 |
# File 'lib/active_object/string.rb', line 49 def deconstantize to_s[0, rindex("::") || 0] end |
#deconstantize! ⇒ Object
53 54 55 |
# File 'lib/active_object/string.rb', line 53 def deconstantize! replace(deconstantize) end |
#demodulize ⇒ Object
57 58 59 |
# File 'lib/active_object/string.rb', line 57 def demodulize to_s.gsub(/^.*::/, "") end |
#demodulize! ⇒ Object
61 62 63 |
# File 'lib/active_object/string.rb', line 61 def demodulize! replace(demodulize) end |
#domain ⇒ Object
65 66 67 |
# File 'lib/active_object/string.rb', line 65 def domain self =~ (/^(?:\w+:\/\/)?([^\/?]+)(?:\/|\?|$)/) ? $1 : self end |
#downcase? ⇒ Boolean
69 70 71 |
# File 'lib/active_object/string.rb', line 69 def downcase? downcase == self end |
#ellipsize(ellipsize_at, options = {}) ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/active_object/string.rb', line 73 def ellipsize(ellipsize_at, ={}) return(self) if length <= ellipsize_at separator = .fetch(:separator, "...") offset = .fetch(:offset, 4) "#{self[0, offset]}#{separator}#{self[-offset, offset]}" end |
#exclude?(string) ⇒ Boolean
82 83 84 |
# File 'lib/active_object/string.rb', line 82 def exclude?(string) !include?(string) end |
#first(limit = 1) ⇒ Object
86 87 88 89 90 |
# File 'lib/active_object/string.rb', line 86 def first(limit=1) return("") if limit.zero? limit >= length ? self : to(limit - 1) end |
#format(*args) ⇒ Object
92 93 94 |
# File 'lib/active_object/string.rb', line 92 def format(*args) super(self, *(args.flatten)) 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
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/active_object/string.rb', line 100 def humanize(={}) capitalize = .fetch(:capitalize, true) underscore. gsub(/_id\z/, ""). tr("_", " "). squish. gsub(/([a-z\d]*)/i) { |s| s.downcase }. gsub(/\A\w/) { |s| capitalize ? s.upcase : s } end |
#humanize!(options = {}) ⇒ Object
111 112 113 |
# File 'lib/active_object/string.rb', line 111 def humanize!(={}) replace(humanize()) end |
#indent(amount, indent_string = nil, indent_empty_lines = false) ⇒ Object
115 116 117 118 119 120 |
# File 'lib/active_object/string.rb', line 115 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
122 123 124 |
# File 'lib/active_object/string.rb', line 122 def indent!(amount, indent_string=nil, indent_empty_lines=false) replace(indent(amount, indent_string, indent_empty_lines)) end |
#index_all(pattern) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/active_object/string.rb', line 126 def index_all(pattern) pattern = pattern.to_s if pattern.is_a?(Numeric) arr_indexes = [] srch_index = rindex(pattern) while srch_index do temp_string = self[0..(srch_index - 1)] arr_indexes << srch_index srch_index = srch_index.zero? ? nil : temp_string.rindex(pattern) end arr_indexes.reverse end |
#labelize(options = {}) ⇒ Object Also known as: labelcase
140 141 142 143 144 145 146 147 148 149 |
# File 'lib/active_object/string.rb', line 140 def labelize(={}) capitalize = .fetch(:capitalize, true) underscore. tr("_", " "). squish. gsub(/([a-z\d]*)/i) { |s| s.downcase }. gsub(/\A\w/) { |s| capitalize ? s.upcase : s }. gsub(/ id\z/, " ID") end |
#labelize!(options = {}) ⇒ Object Also known as: labelcase!
153 154 155 |
# File 'lib/active_object/string.rb', line 153 def labelize!(={}) replace(labelize()) end |
#last(limit = 1) ⇒ Object
160 161 162 163 164 |
# File 'lib/active_object/string.rb', line 160 def last(limit=1) return("") if limit.zero? limit >= length ? self : from(-limit) end |
#mixedcase? ⇒ Boolean
166 167 168 |
# File 'lib/active_object/string.rb', line 166 def mixedcase? !upcase? && !downcase? end |
#ordinal ⇒ Object
170 171 172 |
# File 'lib/active_object/string.rb', line 170 def ordinal to_i.ordinal end |
#ordinalize ⇒ Object
174 175 176 |
# File 'lib/active_object/string.rb', line 174 def ordinalize to_i.ordinalize end |
#parameterize(seperator = "-") ⇒ Object
178 179 180 |
# File 'lib/active_object/string.rb', line 178 def parameterize(seperator="-") underscore.gsub(/\s+/, seperator).downcase end |
#parameterize!(seperator = "-") ⇒ Object
182 183 184 |
# File 'lib/active_object/string.rb', line 182 def parameterize!(seperator="-") replace(parameterize(seperator)) end |
#pollute(delimiter = "^--^--^") ⇒ Object
186 187 188 |
# File 'lib/active_object/string.rb', line 186 def pollute(delimiter="^--^--^") split("").map { |c| "#{c}#{delimiter}" }.join end |
#pollute!(delimiter = "^--^--^") ⇒ Object
190 191 192 |
# File 'lib/active_object/string.rb', line 190 def pollute!(delimiter="^--^--^") replace(pollute(delimiter)) end |
#pop ⇒ Object
194 195 196 |
# File 'lib/active_object/string.rb', line 194 def pop self[-1] end |
#push(string) ⇒ Object
198 199 200 |
# File 'lib/active_object/string.rb', line 198 def push(string) replace(concat(string)) end |
#remove(*patterns) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/active_object/string.rb', line 202 def remove(*patterns) string = dup patterns.flatten.each do |p| if p.is_a?(Range) string.slice!(p) else string.gsub!(p, "") end end string end |
#remove!(*patterns) ⇒ Object
216 217 218 |
# File 'lib/active_object/string.rb', line 216 def remove!(*patterns) replace(remove(*patterns)) end |
#remove_tags ⇒ Object
220 221 222 |
# File 'lib/active_object/string.rb', line 220 def gsub(/<\/?[^>]*>/, "") end |
#remove_tags! ⇒ Object
224 225 226 |
# File 'lib/active_object/string.rb', line 224 def replace() end |
#sample(separator = " ") ⇒ Object
228 229 230 |
# File 'lib/active_object/string.rb', line 228 def sample(separator=" ") split(separator).sample end |
#sample!(separator = " ") ⇒ Object
232 233 234 |
# File 'lib/active_object/string.rb', line 232 def sample!(separator=" ") replace(sample(separator)) end |
#shift(*patterns) ⇒ Object
236 237 238 239 240 241 242 243 244 |
# File 'lib/active_object/string.rb', line 236 def shift(*patterns) if patterns.empty? self[0] else string = dup patterns.flatten.each { |p| string.sub!(p, "") } string end end |
#shift!(*patterns) ⇒ Object
246 247 248 |
# File 'lib/active_object/string.rb', line 246 def shift!(*patterns) replace(shift(*patterns)) end |
#shuffle(separator = "") ⇒ Object
250 251 252 |
# File 'lib/active_object/string.rb', line 250 def shuffle(separator="") split(separator).shuffle.join end |
#shuffle!(separator = "") ⇒ Object
254 255 256 |
# File 'lib/active_object/string.rb', line 254 def shuffle!(separator="") replace(shuffle(separator)) end |
#sift(chars_to_keep) ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/active_object/string.rb', line 258 def sift(chars_to_keep) chars_to_keep = case chars_to_keep when String then chars_to_keep.chars when Array then chars_to_keep.map { |c| c.to_s } when Range then chars_to_keep.to_a.map { |c| c.to_s } else raise TypeError, "Invalid parameter" end chars.keep_if { |chr| chars_to_keep.include?(chr) }.join end |
#sift!(chars_to_keep) ⇒ Object
270 271 272 |
# File 'lib/active_object/string.rb', line 270 def sift!(chars_to_keep) replace(sift(chars_to_keep)) end |
#slugify ⇒ Object
274 275 276 277 278 279 280 |
# File 'lib/active_object/string.rb', line 274 def slugify gsub(/[^\x00-\x7F]+/, ""). gsub(/[^\w_ \-]+/i, ""). gsub(/[ \-]+/i, "-"). gsub(/^\-|\-$/i, ""). downcase end |
#slugify! ⇒ Object
282 283 284 |
# File 'lib/active_object/string.rb', line 282 def slugify! replace(slugify) end |
#sort ⇒ Object
294 295 296 |
# File 'lib/active_object/string.rb', line 294 def sort chars.sort.join end |
#sort! ⇒ Object
298 299 300 |
# File 'lib/active_object/string.rb', line 298 def sort! replace(sort) end |
#squish ⇒ Object
286 287 288 |
# File 'lib/active_object/string.rb', line 286 def squish strip.gsub(/\s+/, " ") end |
#squish! ⇒ Object
290 291 292 |
# File 'lib/active_object/string.rb', line 290 def squish! replace(squish) end |
#titleize ⇒ Object Also known as: titlecase
302 303 304 |
# File 'lib/active_object/string.rb', line 302 def titleize underscore.humanize.gsub(/\b(?<!['’`])[a-z]/) { $&.capitalize } end |
#titleize! ⇒ Object Also known as: titlecase!
308 309 310 |
# File 'lib/active_object/string.rb', line 308 def titleize! replace(titleize) end |
#to(position) ⇒ Object
314 315 316 |
# File 'lib/active_object/string.rb', line 314 def to(position) self[0..position] end |
#truncate(truncate_at, options = {}) ⇒ Object
318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/active_object/string.rb', line 318 def truncate(truncate_at, ={}) return(dup) unless length > truncate_at omission = .fetch(:omission, "...") size_with_room_for_omission = truncate_at - omission.length stop = if .fetch(:separator, false) rindex(.fetch(:separator, ""), size_with_room_for_omission) || size_with_room_for_omission else size_with_room_for_omission end "#{self[0, stop]}#{omission}" end |
#truncate_words(words_count, options = {}) ⇒ Object
333 334 335 336 337 338 339 340 341 342 |
# File 'lib/active_object/string.rb', line 333 def truncate_words(words_count, ={}) sep = .fetch(:separator, /\s+/) sep = Regexp.escape(sep.to_s) unless Regexp === sep if self =~ /\A((?:.+?#{sep}){#{words_count - 1}}.+?)#{sep}.*/m "#{$1}#{.fetch(:omission, '...')}" else self end end |
#underscore ⇒ Object
344 345 346 347 348 349 350 |
# File 'lib/active_object/string.rb', line 344 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
352 353 354 |
# File 'lib/active_object/string.rb', line 352 def underscore! replace(underscore) end |
#unpollute(delimiter = "^--^--^") ⇒ Object
356 357 358 |
# File 'lib/active_object/string.rb', line 356 def unpollute(delimiter="^--^--^") gsub(delimiter, "") end |
#unpollute!(delimiter = "^--^--^") ⇒ Object
360 361 362 |
# File 'lib/active_object/string.rb', line 360 def unpollute!(delimiter="^--^--^") replace(unpollute(delimiter)) end |
#unshift(*patterns) ⇒ Object
368 369 370 371 372 373 |
# File 'lib/active_object/string.rb', line 368 def unshift(*patterns) string = "" patterns.flatten.each { |p| string.concat(p) } string.concat(self) string end |
#unshift!(*patterns) ⇒ Object
375 376 377 |
# File 'lib/active_object/string.rb', line 375 def unshift!(*patterns) replace(unshift(*patterns)) end |
#upcase? ⇒ Boolean
364 365 366 |
# File 'lib/active_object/string.rb', line 364 def upcase? upcase == self end |