Module: ActiveObject::String

Defined in:
lib/active_object/string.rb

Instance Method Summary collapse

Instance Method Details

#any?(*keys) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
# File 'lib/active_object/string.rb', line 36

def any?(*keys)
  included = false

  keys.flatten.each do |key|
    included = include?(key)
    break if included
  end

  included
end

#at(position) ⇒ Object



47
48
49
# File 'lib/active_object/string.rb', line 47

def at(position)
  self[position]
end

#camelize(first_letter = :upper) ⇒ Object Also known as: camelcase



51
52
53
54
55
56
57
58
# File 'lib/active_object/string.rb', line 51

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!



62
63
64
# File 'lib/active_object/string.rb', line 62

def camelize!(first_letter = :upper)
  replace(camelize(first_letter))
end

#classifyObject



68
69
70
# File 'lib/active_object/string.rb', line 68

def classify
  to_s.sub(/.*\./, '').camelize
end

#classify!Object



72
73
74
# File 'lib/active_object/string.rb', line 72

def classify!
  replace(classify)
end

#constantizeObject



76
77
78
# File 'lib/active_object/string.rb', line 76

def constantize
  ::Object.const_get(self)
end

#dasherizeObject



80
81
82
# File 'lib/active_object/string.rb', line 80

def dasherize
  tr(/_/, '-')
end

#dasherize!Object



84
85
86
# File 'lib/active_object/string.rb', line 84

def dasherize!
  replace(dasherize)
end

#deconstantizeObject



88
89
90
# File 'lib/active_object/string.rb', line 88

def deconstantize
  to_s[0, rindex('::') || 0]
end

#deconstantize!Object



92
93
94
# File 'lib/active_object/string.rb', line 92

def deconstantize!
  replace(deconstantize)
end

#demodulizeObject



96
97
98
# File 'lib/active_object/string.rb', line 96

def demodulize
  to_s.gsub(/^.*::/, '')
end

#demodulize!Object



100
101
102
# File 'lib/active_object/string.rb', line 100

def demodulize!
  replace(demodulize)
end

#domainObject



104
105
106
# File 'lib/active_object/string.rb', line 104

def domain
  self =~ %r{^(?:\w+:\/\/)?([^\/?]+)(?:\/|\?|$)} ? ::Regexp.last_match(1) : self
end

#downcase?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/active_object/string.rb', line 108

def downcase?
  downcase == self
end

#ellipsize(ellipsize_at, options = {}) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/active_object/string.rb', line 112

def ellipsize(ellipsize_at, options = {})
  return self if length <= ellipsize_at

  separator = options[:separator] || '...'
  offset = options[:offset] || 4

  "#{self[0, offset]}#{separator}#{self[-offset, offset]}"
end

#exclude?(string) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/active_object/string.rb', line 121

def exclude?(string)
  !include?(string)
end

#first(limit = 1) ⇒ Object



125
126
127
128
129
# File 'lib/active_object/string.rb', line 125

def first(limit = 1)
  return '' if limit.zero?

  limit >= length ? self : to(limit - 1)
end

#format(*args) ⇒ Object



131
132
133
# File 'lib/active_object/string.rb', line 131

def format(*args)
  super(self, *args.flatten)
end

#from(position) ⇒ Object



135
136
137
# File 'lib/active_object/string.rb', line 135

def from(position)
  self[position..-1]
end

#headerizeObject



139
140
141
# File 'lib/active_object/string.rb', line 139

def headerize
  squish.split(' ').map(&:capitalize).join(' ')
end

#headerize!Object



143
144
145
# File 'lib/active_object/string.rb', line 143

def headerize!
  replace(headerize)
end

#humanize(options = {}) ⇒ Object



147
148
149
150
151
152
153
154
155
# File 'lib/active_object/string.rb', line 147

def humanize(options = {})
  capitalize = options[: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



157
158
159
# File 'lib/active_object/string.rb', line 157

def humanize!(options = {})
  replace(humanize(options))
end

#indent(amount, indent_string = nil, indent_empty_lines = false) ⇒ Object



161
162
163
164
165
166
# File 'lib/active_object/string.rb', line 161

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



168
169
170
# File 'lib/active_object/string.rb', line 168

def indent!(amount, indent_string = nil, indent_empty_lines = false)
  replace(indent(amount, indent_string, indent_empty_lines))
end

#index_all(pattern) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/active_object/string.rb', line 172

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



186
187
188
189
190
191
192
193
194
# File 'lib/active_object/string.rb', line 186

def labelize(options = {})
  capitalize = options[: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!



198
199
200
# File 'lib/active_object/string.rb', line 198

def labelize!(options = {})
  replace(labelize(options))
end

#last(limit = 1) ⇒ Object



204
205
206
207
208
# File 'lib/active_object/string.rb', line 204

def last(limit = 1)
  return '' if limit.zero?

  limit >= length ? self : from(-limit)
end

#mixedcase?Boolean

Returns:

  • (Boolean)


210
211
212
# File 'lib/active_object/string.rb', line 210

def mixedcase?
  !upcase? && !downcase?
end

#ordinalObject



214
215
216
# File 'lib/active_object/string.rb', line 214

def ordinal
  to_i.ordinal
end

#ordinalizeObject



218
219
220
# File 'lib/active_object/string.rb', line 218

def ordinalize
  to_i.ordinalize
end

#parameterize(separator: '-') ⇒ Object



222
223
224
# File 'lib/active_object/string.rb', line 222

def parameterize(separator: '-')
  underscore.gsub(/\s+/, separator).downcase
end

#parameterize!(separator: '-') ⇒ Object



226
227
228
# File 'lib/active_object/string.rb', line 226

def parameterize!(separator: '-')
  replace(parameterize(separator: separator))
end

#pollute(delimiter = '^--^--^') ⇒ Object



230
231
232
# File 'lib/active_object/string.rb', line 230

def pollute(delimiter = '^--^--^')
  split('').map { |chr| "#{chr}#{delimiter}" }.join
end

#pollute!(delimiter = '^--^--^') ⇒ Object



234
235
236
# File 'lib/active_object/string.rb', line 234

def pollute!(delimiter = '^--^--^')
  replace(pollute(delimiter))
end

#popObject



238
239
240
# File 'lib/active_object/string.rb', line 238

def pop
  self[-1]
end

#push(string) ⇒ Object



242
243
244
# File 'lib/active_object/string.rb', line 242

def push(string)
  replace(concat(string))
end

#remove(*patterns) ⇒ Object



246
247
248
249
250
# File 'lib/active_object/string.rb', line 246

def remove(*patterns)
  string = dup
  patterns.flatten.each { |pat| pat.is_a?(Range) ? string.slice!(pat) : string.gsub!(pat, '') }
  string
end

#remove!(*patterns) ⇒ Object



252
253
254
# File 'lib/active_object/string.rb', line 252

def remove!(*patterns)
  replace(remove(*patterns))
end

#remove_tagsObject



256
257
258
# File 'lib/active_object/string.rb', line 256

def remove_tags
  gsub(%r{<\/?[^>]*>}, '')
end

#remove_tags!Object



260
261
262
# File 'lib/active_object/string.rb', line 260

def remove_tags!
  replace(remove_tags)
end

#sample(separator = ' ') ⇒ Object



264
265
266
# File 'lib/active_object/string.rb', line 264

def sample(separator = ' ')
  split(separator).sample
end

#sample!(separator = ' ') ⇒ Object



268
269
270
# File 'lib/active_object/string.rb', line 268

def sample!(separator = ' ')
  replace(sample(separator))
end

#shift(*patterns) ⇒ Object



272
273
274
275
276
277
278
# File 'lib/active_object/string.rb', line 272

def shift(*patterns)
  return self[0] if patterns.empty?

  string = dup
  patterns.flatten.each { |pat| string.sub!(pat, '') }
  string
end

#shift!(*patterns) ⇒ Object



280
281
282
# File 'lib/active_object/string.rb', line 280

def shift!(*patterns)
  replace(shift(*patterns))
end

#shuffle(separator = '') ⇒ Object



284
285
286
# File 'lib/active_object/string.rb', line 284

def shuffle(separator = '')
  split(separator).shuffle.join
end

#shuffle!(separator = '') ⇒ Object



288
289
290
# File 'lib/active_object/string.rb', line 288

def shuffle!(separator = '')
  replace(shuffle(separator))
end

#sift(chars_to_keep) ⇒ Object



292
293
294
295
296
297
298
299
300
301
# File 'lib/active_object/string.rb', line 292

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



303
304
305
# File 'lib/active_object/string.rb', line 303

def sift!(chars_to_keep)
  replace(sift(chars_to_keep))
end

#slugifyObject



307
308
309
310
311
312
313
# File 'lib/active_object/string.rb', line 307

def slugify
  to_s.gsub(/[^\x00-\x7F]+/, '')
      .gsub(/[^\w_ \-]+/i, '')
      .gsub(/[ \-]+/i, '-')
      .gsub(/^\-|\-$/i, '')
      .downcase
end

#slugify!Object



315
316
317
# File 'lib/active_object/string.rb', line 315

def slugify!
  replace(slugify)
end

#sortObject



327
328
329
# File 'lib/active_object/string.rb', line 327

def sort
  chars.sort.join
end

#sort!Object



331
332
333
# File 'lib/active_object/string.rb', line 331

def sort!
  replace(sort)
end

#squishObject



319
320
321
# File 'lib/active_object/string.rb', line 319

def squish
  strip.gsub(/\s+/, ' ')
end

#squish!Object



323
324
325
# File 'lib/active_object/string.rb', line 323

def squish!
  replace(squish)
end

#titleizeObject Also known as: titlecase



335
336
337
# File 'lib/active_object/string.rb', line 335

def titleize
  underscore.humanize.gsub(/\b(?<!['’`])[a-z]/) { $&.capitalize }
end

#titleize!Object Also known as: titlecase!



341
342
343
# File 'lib/active_object/string.rb', line 341

def titleize!
  replace(titleize)
end

#to(position) ⇒ Object



347
348
349
# File 'lib/active_object/string.rb', line 347

def to(position)
  self[0..position]
end

#transliterizeObject



351
352
353
# File 'lib/active_object/string.rb', line 351

def transliterize
  TRANSLITERATIONS.each_with_object(dup) { |(k, v), str| str.gsub!(k, v) }
end

#transliterize!Object



355
356
357
# File 'lib/active_object/string.rb', line 355

def transliterize!
  TRANSLITERATIONS.each_with_object(self) { |(k, v), str| str.gsub!(k, v) }
end

#truncate(truncate_at, options = {}) ⇒ Object



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/active_object/string.rb', line 359

def truncate(truncate_at, options = {})
  return dup unless length > truncate_at

  seperator = options[:separator]
  omission = options[:omission] || '...'
  size_with_room_for_omission = truncate_at - omission.length

  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



375
376
377
378
379
380
381
# File 'lib/active_object/string.rb', line 375

def truncate_words(words_count, options = {})
  sep = options[: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)}#{options[:omissio] || '...'}"
end

#underscoreObject



383
384
385
386
387
388
389
# File 'lib/active_object/string.rb', line 383

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



391
392
393
# File 'lib/active_object/string.rb', line 391

def underscore!
  replace(underscore)
end

#unpollute(delimiter = '^--^--^') ⇒ Object



395
396
397
# File 'lib/active_object/string.rb', line 395

def unpollute(delimiter = '^--^--^')
  gsub(delimiter, '')
end

#unpollute!(delimiter = '^--^--^') ⇒ Object



399
400
401
# File 'lib/active_object/string.rb', line 399

def unpollute!(delimiter = '^--^--^')
  replace(unpollute(delimiter))
end

#unshift(*patterns) ⇒ Object



407
408
409
410
411
412
# File 'lib/active_object/string.rb', line 407

def unshift(*patterns)
  string = ''
  patterns.flatten.each { |pat| string.concat(pat) }
  string.concat(self)
  string
end

#unshift!(*patterns) ⇒ Object



414
415
416
# File 'lib/active_object/string.rb', line 414

def unshift!(*patterns)
  replace(unshift(*patterns))
end

#upcase?Boolean

Returns:

  • (Boolean)


403
404
405
# File 'lib/active_object/string.rb', line 403

def upcase?
  upcase == self
end