Class: String

Inherits:
Object show all
Defined in:
lib/active_object/string.rb

Instance Method Summary collapse

Instance Method Details

#any?(*keys) ⇒ Boolean

Returns:

  • (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



10
11
12
# File 'lib/active_object/string.rb', line 10

def at(position)
  self[position]
end

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



16
17
18
19
20
21
22
23
24
# File 'lib/active_object/string.rb', line 16

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!



30
31
32
# File 'lib/active_object/string.rb', line 30

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

#classifyObject



38
39
40
41
42
# File 'lib/active_object/string.rb', line 38

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

#classify!Object



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

def classify!
  replace(classify)
end

#constantizeObject



52
53
54
# File 'lib/active_object/string.rb', line 52

def constantize
  Object.const_get(self)
end

#dasherizeObject



58
59
60
# File 'lib/active_object/string.rb', line 58

def dasherize
  gsub(/_/, '-'.freeze)
end

#dasherize!Object



64
65
66
# File 'lib/active_object/string.rb', line 64

def dasherize!
  replace(dasherize)
end

#deconstantizeObject



70
71
72
# File 'lib/active_object/string.rb', line 70

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

#deconstantize!Object



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

def deconstantize!
  replace(deconstantize)
end

#demodulizeObject



82
83
84
# File 'lib/active_object/string.rb', line 82

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

#demodulize!Object



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

def demodulize!
  replace(demodulize)
end

#domainObject



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

def domain
  string = dup
  string =~ (/^(?:\w+:\/\/)?([^\/?]+)(?:\/|\?|$)/) ? $1 : string
end

#downcase?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/active_object/string.rb', line 98

def downcase?
  downcase == self
end

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



102
103
104
105
106
107
108
109
# File 'lib/active_object/string.rb', line 102

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

   separator = options.fetch(:separator, '...'.freeze)
   offset    = options.fetch(:offset, 4)

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

#exclude?(string) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/active_object/string.rb', line 112

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

#first(limit = 1) ⇒ Object



118
119
120
121
122
# File 'lib/active_object/string.rb', line 118

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

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

#from(position) ⇒ Object



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

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

#humanize(options = {}) ⇒ Object



132
133
134
135
136
137
138
139
# File 'lib/active_object/string.rb', line 132

def humanize(options={})
  underscore.
  gsub(/_id\z/, ''.freeze).
  tr('_'.freeze, ' '.freeze).
  squish.
  gsub(/([a-z\d]*)/i) { |s| s.downcase }.
  gsub(/\A\w/) { |s| options.fetch(:capitalize, true) ? s.upcase : s }
end

#humanize!(options = {}) ⇒ Object



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

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

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



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

def indent(amount, indent_string=nil, indent_empty_lines=false)
  indent_string = indent_string || self[/^[ \t]/] || ' '.freeze
  substitutes   = indent_empty_lines ? /^/ : /^(?!$)/

  gsub(substitutes, indent_string * amount)
end

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



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

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

#index_all(pattern) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/active_object/string.rb', line 163

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



177
178
179
180
181
182
183
184
# File 'lib/active_object/string.rb', line 177

def labelize(options={})
  underscore.
  tr('_'.freeze, ' '.freeze).
  squish.
  gsub(/([a-z\d]*)/i) { |s| s.downcase }.
  gsub(/\A\w/) { |s| options.fetch(:capitalize, true) ? s.upcase : s }.
  gsub(/ id\z/, ' ID'.freeze)
end

#labelize!(options = {}) ⇒ Object Also known as: labelcase!



188
189
190
# File 'lib/active_object/string.rb', line 188

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

#last(limit = 1) ⇒ Object



195
196
197
198
199
# File 'lib/active_object/string.rb', line 195

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

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

#mixedcase?Boolean

Returns:

  • (Boolean)


202
203
204
# File 'lib/active_object/string.rb', line 202

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

#ordinalObject



207
208
209
# File 'lib/active_object/string.rb', line 207

def ordinal
  to_i.ordinal
end

#ordinalizeObject



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

def ordinalize
  to_i.ordinalize
end

#parameterize(seperator = '-'.freeze) ⇒ Object



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

def parameterize(seperator='-'.freeze)
  underscore.
  gsub(/\s+/, '_'.freeze).
  gsub('_'.freeze, seperator).
  downcase
end

#parameterize!(seperator = '-'.freeze) ⇒ Object



228
229
230
# File 'lib/active_object/string.rb', line 228

def parameterize!(seperator='-'.freeze)
  replace(parameterize(seperator))
end

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



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

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

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



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

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

#popObject



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

def pop
  self[-1]
end

#push(string) ⇒ Object



245
246
247
# File 'lib/active_object/string.rb', line 245

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

#remove(*patterns) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/active_object/string.rb', line 250

def remove(*patterns)
  string = dup

  patterns.flatten.each do |p|
    if p.is_a?(Range)
      string.slice!(p)
    else
      string.gsub!(p, ''.freeze)
    end
  end

  string
end

#remove!(*patterns) ⇒ Object



266
267
268
# File 'lib/active_object/string.rb', line 266

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

#remove_tagsObject



271
272
273
# File 'lib/active_object/string.rb', line 271

def remove_tags
  gsub(/<\/?[^>]*>/, ''.freeze)
end

#remove_tags!Object



275
276
277
# File 'lib/active_object/string.rb', line 275

def remove_tags!
  replace(remove_tags)
end

#sample(separator = ' '.freeze) ⇒ Object



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

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

#sample!(separator = ' '.freeze) ⇒ Object



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

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

#shift(*patterns) ⇒ Object



287
288
289
290
291
292
293
294
295
# File 'lib/active_object/string.rb', line 287

def shift(*patterns)
  if patterns.empty?
    self[0]
  else
    string = dup
    patterns.flatten.each { |p| string.sub!(p, ''.freeze) }
    string
  end
end

#shift!(*patterns) ⇒ Object



297
298
299
# File 'lib/active_object/string.rb', line 297

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

#shuffle(separator = ''.freeze) ⇒ Object



301
302
303
# File 'lib/active_object/string.rb', line 301

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

#shuffle!(separator = ''.freeze) ⇒ Object



305
306
307
# File 'lib/active_object/string.rb', line 305

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

#sift(chars_to_keep) ⇒ Object



309
310
311
312
313
314
315
316
317
318
319
# File 'lib/active_object/string.rb', line 309

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



321
322
323
# File 'lib/active_object/string.rb', line 321

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

#slugifyObject



325
326
327
328
329
330
331
# File 'lib/active_object/string.rb', line 325

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

#slugify!Object



333
334
335
# File 'lib/active_object/string.rb', line 333

def slugify!
  replace(slugify)
end

#sortObject



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

def sort
  chars.sort.join
end

#sort!Object



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

def sort!
  replace(sort)
end

#squishObject



338
339
340
341
# File 'lib/active_object/string.rb', line 338

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

#squish!Object



345
346
347
# File 'lib/active_object/string.rb', line 345

def squish!
  replace(squish)
end

#titleizeObject Also known as: titlecase



359
360
361
362
363
# File 'lib/active_object/string.rb', line 359

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

#titleize!Object Also known as: titlecase!



369
370
371
# File 'lib/active_object/string.rb', line 369

def titleize!
  replace(titleize)
end

#to(position) ⇒ Object



377
378
379
# File 'lib/active_object/string.rb', line 377

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

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



383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/active_object/string.rb', line 383

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

  omission = options.fetch(:omission, '...'.freeze)
  size_with_room_for_omission = truncate_at - omission.length

  stop = if options.fetch(:separator, false)
    rindex(options.fetch(:separator, ''.freeze), 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



400
401
402
403
404
405
406
407
408
409
# File 'lib/active_object/string.rb', line 400

def truncate_words(words_count, options={})
  sep = options.fetch(:separator, /\s+/)
  sep = Regexp.escape(sep.to_s) unless Regexp === sep

  if self =~ /\A((?:.+?#{sep}){#{words_count - 1}}.+?)#{sep}.*/m
    "#{$1}#{options.fetch(:omission, '...'.freeze)}"
  else
    dup
  end
end

#underscoreObject



413
414
415
416
417
418
419
# File 'lib/active_object/string.rb', line 413

def underscore
  gsub(/::/, '/'.freeze).
  gsub(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2'.freeze).
  gsub(/([a-z\d])([A-Z])/, '\1_\2'.freeze).
  tr('-'.freeze, '_'.freeze).
  downcase
end

#underscore!Object



423
424
425
# File 'lib/active_object/string.rb', line 423

def underscore!
  replace(underscore)
end

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



428
429
430
# File 'lib/active_object/string.rb', line 428

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

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



432
433
434
# File 'lib/active_object/string.rb', line 432

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

#unshift(*patterns) ⇒ Object



440
441
442
443
444
445
# File 'lib/active_object/string.rb', line 440

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

#unshift!(*patterns) ⇒ Object



447
448
449
# File 'lib/active_object/string.rb', line 447

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

#upcase?Boolean

Returns:

  • (Boolean)


436
437
438
# File 'lib/active_object/string.rb', line 436

def upcase?
  upcase == self
end