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
- #headerize ⇒ Object
- #headerize! ⇒ 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(separator: '-') ⇒ Object
- #parameterize!(separator: '-') ⇒ 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
6 7 8 9 10 11 12 13 |
# File 'lib/active_object/string.rb', line 6 def any?(*keys) included = false keys.flatten.each do |key| included = include?(key) break if included end included end |
#at(position) ⇒ Object
15 16 17 |
# File 'lib/active_object/string.rb', line 15 def at(position) self[position] end |
#camelize(first_letter = :upper) ⇒ Object Also known as: camelcase
19 20 21 22 23 24 25 26 27 |
# File 'lib/active_object/string.rb', line 19 def camelize(first_letter = :upper) if first_letter.to_sym != :lower regex_last = ::Regexp.last_match(1).upcase to_s.gsub(%r{\/(.?)}) { "::#{regex_last}" }.gsub(%r{^/(?:^|_)(.)}) { regex_last } else "#{to_s.first.chr.downcase}#{camelize(self)[1..-1]}" end end |
#camelize!(first_letter = :upper) ⇒ Object Also known as: camelcase!
31 32 33 |
# File 'lib/active_object/string.rb', line 31 def camelize!(first_letter = :upper) replace(camelize(first_letter)) end |
#classify ⇒ Object
37 38 39 |
# File 'lib/active_object/string.rb', line 37 def classify to_s.sub(/.*\./, '').camelize end |
#classify! ⇒ Object
41 42 43 |
# File 'lib/active_object/string.rb', line 41 def classify! replace(classify) end |
#constantize ⇒ Object
45 46 47 |
# File 'lib/active_object/string.rb', line 45 def constantize ::Object.const_get(self) end |
#dasherize ⇒ Object
49 50 51 |
# File 'lib/active_object/string.rb', line 49 def dasherize tr(/_/, '-') end |
#dasherize! ⇒ Object
53 54 55 |
# File 'lib/active_object/string.rb', line 53 def dasherize! replace(dasherize) end |
#deconstantize ⇒ Object
57 58 59 |
# File 'lib/active_object/string.rb', line 57 def deconstantize to_s[0, rindex('::') || 0] end |
#deconstantize! ⇒ Object
61 62 63 |
# File 'lib/active_object/string.rb', line 61 def deconstantize! replace(deconstantize) end |
#demodulize ⇒ Object
65 66 67 |
# File 'lib/active_object/string.rb', line 65 def demodulize to_s.gsub(/^.*::/, '') end |
#demodulize! ⇒ Object
69 70 71 |
# File 'lib/active_object/string.rb', line 69 def demodulize! replace(demodulize) end |
#domain ⇒ Object
73 74 75 |
# File 'lib/active_object/string.rb', line 73 def domain self =~ %r{^(?:\w+:\/\/)?([^\/?]+)(?:\/|\?|$)} ? ::Regexp.last_match(1) : self end |
#downcase? ⇒ Boolean
77 78 79 |
# File 'lib/active_object/string.rb', line 77 def downcase? downcase == self end |
#ellipsize(ellipsize_at, options = {}) ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/active_object/string.rb', line 81 def ellipsize(ellipsize_at, = {}) return self if length <= ellipsize_at separator = [:separator] || '...' offset = [:offset] || 4 "#{self[0, offset]}#{separator}#{self[-offset, offset]}" end |
#exclude?(string) ⇒ Boolean
90 91 92 |
# File 'lib/active_object/string.rb', line 90 def exclude?(string) !include?(string) end |
#first(limit = 1) ⇒ Object
94 95 96 97 98 |
# File 'lib/active_object/string.rb', line 94 def first(limit = 1) return '' if limit.zero? limit >= length ? self : to(limit - 1) end |
#format(*args) ⇒ Object
100 101 102 |
# File 'lib/active_object/string.rb', line 100 def format(*args) super(self, *args.flatten) end |
#from(position) ⇒ Object
104 105 106 |
# File 'lib/active_object/string.rb', line 104 def from(position) self[position..-1] end |
#headerize ⇒ Object
108 109 110 111 112 |
# File 'lib/active_object/string.rb', line 108 def headerize squish.split(' ') .map(&:capitalize) .join(' ') end |
#headerize! ⇒ Object
114 115 116 |
# File 'lib/active_object/string.rb', line 114 def headerize! replace(headerize) end |
#humanize(options = {}) ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/active_object/string.rb', line 118 def humanize( = {}) capitalize = [:capitalize] || true underscore.gsub(/_id\z/, '') .tr('_', ' ') .squish .gsub(/([a-z\d]*)/i, &:downcase) .gsub(/\A\w/) { |str| capitalize ? str.upcase : str } end |
#humanize!(options = {}) ⇒ Object
128 129 130 |
# File 'lib/active_object/string.rb', line 128 def humanize!( = {}) replace(humanize()) end |
#indent(amount, indent_string = nil, indent_empty_lines = false) ⇒ Object
132 133 134 135 136 137 |
# File 'lib/active_object/string.rb', line 132 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
139 140 141 |
# File 'lib/active_object/string.rb', line 139 def indent!(amount, indent_string = nil, indent_empty_lines = false) replace(indent(amount, indent_string, indent_empty_lines)) end |
#index_all(pattern) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/active_object/string.rb', line 143 def index_all(pattern) pattern = pattern.to_s if pattern.is_a?(Numeric) arr_indexes = [] srch_index = rindex(pattern) while srch_index 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
157 158 159 160 161 162 163 164 165 |
# File 'lib/active_object/string.rb', line 157 def labelize( = {}) capitalize = [:capitalize] || true underscore.tr('_', ' ') .squish .gsub(/([a-z\d]*)/i, &:downcase) .gsub(/\A\w/) { |str| capitalize ? str.upcase : str } .gsub(/ id\z/, ' ID') end |
#labelize!(options = {}) ⇒ Object Also known as: labelcase!
169 170 171 |
# File 'lib/active_object/string.rb', line 169 def labelize!( = {}) replace(labelize()) end |
#last(limit = 1) ⇒ Object
175 176 177 178 179 |
# File 'lib/active_object/string.rb', line 175 def last(limit = 1) return '' if limit.zero? limit >= length ? self : from(-limit) end |
#mixedcase? ⇒ Boolean
181 182 183 |
# File 'lib/active_object/string.rb', line 181 def mixedcase? !upcase? && !downcase? end |
#ordinal ⇒ Object
185 186 187 |
# File 'lib/active_object/string.rb', line 185 def ordinal to_i.ordinal end |
#ordinalize ⇒ Object
189 190 191 |
# File 'lib/active_object/string.rb', line 189 def ordinalize to_i.ordinalize end |
#parameterize(separator: '-') ⇒ Object
193 194 195 |
# File 'lib/active_object/string.rb', line 193 def parameterize(separator: '-') underscore.gsub(/\s+/, separator).downcase end |
#parameterize!(separator: '-') ⇒ Object
197 198 199 |
# File 'lib/active_object/string.rb', line 197 def parameterize!(separator: '-') replace(parameterize(separator: separator)) end |
#pollute(delimiter = '^--^--^') ⇒ Object
201 202 203 |
# File 'lib/active_object/string.rb', line 201 def pollute(delimiter = '^--^--^') split('').map { |chr| "#{chr}#{delimiter}" }.join end |
#pollute!(delimiter = '^--^--^') ⇒ Object
205 206 207 |
# File 'lib/active_object/string.rb', line 205 def pollute!(delimiter = '^--^--^') replace(pollute(delimiter)) end |
#pop ⇒ Object
209 210 211 |
# File 'lib/active_object/string.rb', line 209 def pop self[-1] end |
#push(string) ⇒ Object
213 214 215 |
# File 'lib/active_object/string.rb', line 213 def push(string) replace(concat(string)) end |
#remove(*patterns) ⇒ Object
217 218 219 220 221 |
# File 'lib/active_object/string.rb', line 217 def remove(*patterns) string = dup patterns.flatten.each { |pat| pat.is_a?(Range) ? string.slice!(pat) : string.gsub!(pat, '') } string end |
#remove!(*patterns) ⇒ Object
223 224 225 |
# File 'lib/active_object/string.rb', line 223 def remove!(*patterns) replace(remove(*patterns)) end |
#remove_tags ⇒ Object
227 228 229 |
# File 'lib/active_object/string.rb', line 227 def gsub(%r{<\/?[^>]*>}, '') end |
#remove_tags! ⇒ Object
231 232 233 |
# File 'lib/active_object/string.rb', line 231 def replace() end |
#sample(separator = ' ') ⇒ Object
235 236 237 |
# File 'lib/active_object/string.rb', line 235 def sample(separator = ' ') split(separator).sample end |
#sample!(separator = ' ') ⇒ Object
239 240 241 |
# File 'lib/active_object/string.rb', line 239 def sample!(separator = ' ') replace(sample(separator)) end |
#shift(*patterns) ⇒ Object
243 244 245 246 247 248 249 |
# File 'lib/active_object/string.rb', line 243 def shift(*patterns) return self[0] if patterns.empty? string = dup patterns.flatten.each { |pat| string.sub!(pat, '') } string end |
#shift!(*patterns) ⇒ Object
251 252 253 |
# File 'lib/active_object/string.rb', line 251 def shift!(*patterns) replace(shift(*patterns)) end |
#shuffle(separator = '') ⇒ Object
255 256 257 |
# File 'lib/active_object/string.rb', line 255 def shuffle(separator = '') split(separator).shuffle.join end |
#shuffle!(separator = '') ⇒ Object
259 260 261 |
# File 'lib/active_object/string.rb', line 259 def shuffle!(separator = '') replace(shuffle(separator)) end |
#sift(chars_to_keep) ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/active_object/string.rb', line 263 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(&:to_s) when Range then chars_to_keep.to_a.map(&:to_s) else raise TypeError, 'Invalid parameter' end chars.keep_if { |chr| chars_to_keep.include?(chr) }.join end |
#sift!(chars_to_keep) ⇒ Object
275 276 277 |
# File 'lib/active_object/string.rb', line 275 def sift!(chars_to_keep) replace(sift(chars_to_keep)) end |
#slugify ⇒ Object
279 280 281 282 283 284 285 |
# File 'lib/active_object/string.rb', line 279 def slugify to_s.gsub(/[^\x00-\x7F]+/, '') .gsub(/[^\w_ \-]+/i, '') .gsub(/[ \-]+/i, '-') .gsub(/^\-|\-$/i, '') .downcase end |
#slugify! ⇒ Object
287 288 289 |
# File 'lib/active_object/string.rb', line 287 def slugify! replace(slugify) end |
#sort ⇒ Object
299 300 301 |
# File 'lib/active_object/string.rb', line 299 def sort chars.sort.join end |
#sort! ⇒ Object
303 304 305 |
# File 'lib/active_object/string.rb', line 303 def sort! replace(sort) end |
#squish ⇒ Object
291 292 293 |
# File 'lib/active_object/string.rb', line 291 def squish strip.gsub(/\s+/, ' ') end |
#squish! ⇒ Object
295 296 297 |
# File 'lib/active_object/string.rb', line 295 def squish! replace(squish) end |
#titleize ⇒ Object Also known as: titlecase
307 308 309 |
# File 'lib/active_object/string.rb', line 307 def titleize underscore.humanize.gsub(/\b(?<!['’`])[a-z]/) { $&.capitalize } end |
#titleize! ⇒ Object Also known as: titlecase!
313 314 315 |
# File 'lib/active_object/string.rb', line 313 def titleize! replace(titleize) end |
#to(position) ⇒ Object
319 320 321 |
# File 'lib/active_object/string.rb', line 319 def to(position) self[0..position] end |
#truncate(truncate_at, options = {}) ⇒ Object
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/active_object/string.rb', line 323 def truncate(truncate_at, = {}) return dup unless length > truncate_at omission = [:omission] || '...' size_with_room_for_omission = truncate_at - omission.length seperator = [:separator] stop = if seperator rindex(seperator || '', 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
339 340 341 342 343 344 345 346 |
# File 'lib/active_object/string.rb', line 339 def truncate_words(words_count, = {}) sep = [:separator] || /\s+/ sep = ::Regexp.escape(sep.to_s) unless sep.is_a(Regexp) return self unless self =~ /\A((?:.+?#{sep}){#{words_count - 1}}.+?)#{sep}.*/m "#{::Regexp.last_match(1)}#{[:omissio] || '...'}" end |
#underscore ⇒ Object
348 349 350 351 352 353 354 |
# File 'lib/active_object/string.rb', line 348 def underscore to_s.gsub(/::/, '/') .gsub(/([A-Z\d]+)([A-Z][a-z])/, "\1_\2") .gsub(/([a-z\d])([A-Z])/, "\1_\2") .tr('-', '_') .downcase end |
#underscore! ⇒ Object
356 357 358 |
# File 'lib/active_object/string.rb', line 356 def underscore! replace(underscore) end |
#unpollute(delimiter = '^--^--^') ⇒ Object
360 361 362 |
# File 'lib/active_object/string.rb', line 360 def unpollute(delimiter = '^--^--^') gsub(delimiter, '') end |
#unpollute!(delimiter = '^--^--^') ⇒ Object
364 365 366 |
# File 'lib/active_object/string.rb', line 364 def unpollute!(delimiter = '^--^--^') replace(unpollute(delimiter)) end |
#unshift(*patterns) ⇒ Object
372 373 374 375 376 377 |
# File 'lib/active_object/string.rb', line 372 def unshift(*patterns) string = '' patterns.flatten.each { |pat| string.concat(pat) } string.concat(self) string end |
#unshift!(*patterns) ⇒ Object
379 380 381 |
# File 'lib/active_object/string.rb', line 379 def unshift!(*patterns) replace(unshift(*patterns)) end |
#upcase? ⇒ Boolean
368 369 370 |
# File 'lib/active_object/string.rb', line 368 def upcase? upcase == self end |