Module: ActiveObject::String

Defined in:
lib/active_object/string.rb

Instance Method Summary collapse

Instance Method Details

#any?(*keys) ⇒ Boolean

Returns:

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

#classifyObject



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

#constantizeObject



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

def constantize
  ::Object.const_get(self)
end

#dasherizeObject



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

#deconstantizeObject



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

#demodulizeObject



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

#domainObject



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

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

#downcase?Boolean

Returns:

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


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

#humanize(options = {}) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/active_object/string.rb', line 108

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



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

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

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



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

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



129
130
131
# File 'lib/active_object/string.rb', line 129

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

#index_all(pattern) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/active_object/string.rb', line 133

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



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

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!



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

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

#last(limit = 1) ⇒ Object



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

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

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

#mixedcase?Boolean

Returns:

  • (Boolean)


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

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

#ordinalObject



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

def ordinal
  to_i.ordinal
end

#ordinalizeObject



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

def ordinalize
  to_i.ordinalize
end

#parameterize(separator: '-') ⇒ Object



183
184
185
# File 'lib/active_object/string.rb', line 183

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

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



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

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

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



191
192
193
# File 'lib/active_object/string.rb', line 191

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

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



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

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

#popObject



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

def pop
  self[-1]
end

#push(string) ⇒ Object



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

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

#remove(*patterns) ⇒ Object



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

def remove(*patterns)
  string = dup
  patterns.flatten.each { |pat| pat.is_a?(Range) ? string.slice!(pat) : string.gsub!(pat, '') }
  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



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

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

#remove_tags!Object



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

def remove_tags!
  replace(remove_tags)
end

#sample(separator = ' ') ⇒ Object



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

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

#sample!(separator = ' ') ⇒ Object



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

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

#shift(*patterns) ⇒ Object



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

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

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

#shift!(*patterns) ⇒ Object



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

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

#shuffle(separator = '') ⇒ Object



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

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

#shuffle!(separator = '') ⇒ Object



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

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

#sift(chars_to_keep) ⇒ Object



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

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



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

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

#slugifyObject



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

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

#slugify!Object



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

def slugify!
  replace(slugify)
end

#sortObject



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

def sort
  chars.sort.join
end

#sort!Object



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

def sort!
  replace(sort)
end

#squishObject



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

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

#squish!Object



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

def squish!
  replace(squish)
end

#titleizeObject Also known as: titlecase



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

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

#titleize!Object Also known as: titlecase!



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

def titleize!
  replace(titleize)
end

#to(position) ⇒ Object



309
310
311
# File 'lib/active_object/string.rb', line 309

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

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



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/active_object/string.rb', line 313

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

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

  seperator = options[: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



329
330
331
332
333
334
335
336
# File 'lib/active_object/string.rb', line 329

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



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

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



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

def underscore!
  replace(underscore)
end

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



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

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

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



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

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

#unshift(*patterns) ⇒ Object



362
363
364
365
366
367
# File 'lib/active_object/string.rb', line 362

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

#unshift!(*patterns) ⇒ Object



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

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

#upcase?Boolean

Returns:

  • (Boolean)


358
359
360
# File 'lib/active_object/string.rb', line 358

def upcase?
  upcase == self
end