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

#classify ⇒ Object



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

#constantize ⇒ Object



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

def constantize
  Object.const_get(self)
end

#dasherize ⇒ Object



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

#deconstantize ⇒ Object



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

#demodulize ⇒ Object



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

#domain ⇒ Object



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
# File 'lib/active_object/string.rb', line 132

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

#humanize!(options = {}) ⇒ Object



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

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

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



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

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



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

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

#last(limit = 1) ⇒ Object



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

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

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

#mixedcase? ⇒ Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/active_object/string.rb', line 170

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

#ordinal ⇒ Object



175
176
177
# File 'lib/active_object/string.rb', line 175

def ordinal
  to_i.ordinal
end

#ordinalize ⇒ Object



181
182
183
# File 'lib/active_object/string.rb', line 181

def ordinalize
  to_i.ordinalize
end

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



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

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

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



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

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

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



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

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

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



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

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

#remove(*patterns) ⇒ Object



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

def remove(*patterns)
  string = dup
  patterns.flatten.each { |p| string.gsub!(p, ''.freeze) }
  string
end

#remove!(*patterns) ⇒ Object



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

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

#remove_tags ⇒ Object



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

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

#remove_tags! ⇒ Object



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

def remove_tags!
  replace(remove_tags)
end

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



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

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

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



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

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

#shift(*patterns) ⇒ Object



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

def shift(*patterns)
  string = dup
  patterns.flatten.each { |p| string.sub!(p, ''.freeze) }
  string
end

#shift!(*patterns) ⇒ Object



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

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

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



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

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

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



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

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

#slugify ⇒ Object



257
258
259
260
261
262
263
# File 'lib/active_object/string.rb', line 257

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

#slugify! ⇒ Object



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

def slugify!
  replace(slugify)
end

#squish ⇒ Object



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

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

#squish! ⇒ Object



277
278
279
# File 'lib/active_object/string.rb', line 277

def squish!
  replace(squish)
end

#titleize ⇒ Object Also known as: titlecase



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

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

#titleize! ⇒ Object Also known as: titlecase!



293
294
295
# File 'lib/active_object/string.rb', line 293

def titleize!
  replace(titleize)
end

#to(position) ⇒ Object



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

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

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



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

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



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

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

#underscore ⇒ Object



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

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



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

def underscore!
  replace(underscore)
end

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



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

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

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



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

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

#upcase? ⇒ Boolean

Returns:

  • (Boolean)


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

def upcase?
  upcase == self
end