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
8
9
10
11
12
# File 'lib/active_object/string.rb', line 3

def any?(*keys)
  included = false

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

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



21
22
23
24
25
26
27
# File 'lib/active_object/string.rb', line 21

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!



33
34
35
# File 'lib/active_object/string.rb', line 33

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

#classifyObject



41
42
43
# File 'lib/active_object/string.rb', line 41

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

#classify!Object



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

def classify!
  replace(classify)
end

#constantizeObject



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

def constantize
  Object.const_get(self)
end

#dasherizeObject



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

def dasherize
  gsub(/_/, "-")
end

#dasherize!Object



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

def dasherize!
  replace(dasherize)
end

#deconstantizeObject



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

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

#deconstantize!Object



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

def deconstantize!
  replace(deconstantize)
end

#demodulizeObject



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

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

#demodulize!Object



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

def demodulize!
  replace(demodulize)
end

#domainObject



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

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

#downcase?Boolean

Returns:

  • (Boolean)


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

def downcase?
  downcase == self
end

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



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

def ellipsize(ellipsize_at, options={})
   return(self)if size <= ellipsize_at
   separator = options.fetch(:separator, "...")
   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
# File 'lib/active_object/string.rb', line 118

def first(limit=1)
  return("") if limit.zero?
  limit >= size ? self.dup : to(limit - 1)
end

#from(position) ⇒ Object



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

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

#humanize(options = {}) ⇒ Object



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

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

#humanize!(options = {}) ⇒ Object



141
142
143
# File 'lib/active_object/string.rb', line 141

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

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



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

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



155
156
157
# File 'lib/active_object/string.rb', line 155

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

#last(limit = 1) ⇒ Object



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

def last(limit=1)
  return("") if limit.zero?
  limit >= size ? self.dup : from(-limit)
end

#mixedcase?Boolean

Returns:

  • (Boolean)


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

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

#ordinalObject



172
173
174
# File 'lib/active_object/string.rb', line 172

def ordinal
  to_i.ordinal
end

#ordinalizeObject



178
179
180
# File 'lib/active_object/string.rb', line 178

def ordinalize
  to_i.ordinalize
end

#parameterize(sep = "-") ⇒ Object



184
185
186
187
188
189
# File 'lib/active_object/string.rb', line 184

def parameterize(sep="-")
  underscore.
  gsub(/\s+/, "_").
  gsub("_", sep).
  downcase
end

#parameterize!(sep = "-") ⇒ Object



193
194
195
# File 'lib/active_object/string.rb', line 193

def parameterize!(sep="-")
  replace(parameterize(sep))
end

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



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

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

#remove(*patterns) ⇒ Object



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

def remove(*patterns)
  string = dup
  patterns.flatten.each do |pattern|
    string.gsub!(pattern, "")
  end
  string
end

#remove!(*patterns) ⇒ Object



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

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

#remove_tagsObject



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

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

#remove_tags!Object



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

def remove_tags!
  replace(remove_tags)
end

#sample(separator = ' ') ⇒ Object



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

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

#sample!(separator = ' ') ⇒ Object



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

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

#shift(*patterns) ⇒ Object



234
235
236
237
238
239
240
# File 'lib/active_object/string.rb', line 234

def shift(*patterns)
  string = dup
  patterns.flatten.each do |pattern|
    string.sub!(pattern, "")
  end
  string
end

#shift!(*patterns) ⇒ Object



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

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

#shuffle(separator = '') ⇒ Object



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

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

#shuffle!(separator = '') ⇒ Object



250
251
252
# File 'lib/active_object/string.rb', line 250

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

#slugifyObject



254
255
256
257
258
259
260
# File 'lib/active_object/string.rb', line 254

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

#slugify!Object



262
263
264
# File 'lib/active_object/string.rb', line 262

def slugify!
  replace(slugify)
end

#squishObject



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

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

#squish!Object



273
274
275
# File 'lib/active_object/string.rb', line 273

def squish!
  replace(squish)
end

#titleizeObject Also known as: titlecase



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

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

#titleize!Object Also known as: titlecase!



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

def titleize!
  replace(titleize)
end

#to(position) ⇒ Object



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

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

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



303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/active_object/string.rb', line 303

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

  omission = options.fetch(:omission, "...")
  size_with_room_for_omission = truncate_at - omission.size

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



320
321
322
323
324
325
326
327
328
329
# File 'lib/active_object/string.rb', line 320

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

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

#underscoreObject



333
334
335
336
337
338
339
# File 'lib/active_object/string.rb', line 333

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



343
344
345
# File 'lib/active_object/string.rb', line 343

def underscore!
  replace(underscore)
end

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



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

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

#upcase?Boolean

Returns:

  • (Boolean)


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

def upcase?
  upcase == self
end