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

#classify ⇒ Object



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

#dasherize ⇒ Object



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

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

#dasherize! ⇒ Object



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

def dasherize!
  replace(dasherize)
end

#demodulize ⇒ Object



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

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

#demodulize! ⇒ Object



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

def demodulize!
  replace(demodulize)
end

#domain ⇒ Object



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

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

#downcase? ⇒ Boolean

Returns:

  • (Boolean)


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

def downcase?
  downcase == self
end

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



85
86
87
88
89
90
91
# File 'lib/active_object/string.rb', line 85

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)


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

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

#first(limit = 1) ⇒ Object



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

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

#from(position) ⇒ Object



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

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

#humanize(options = {}) ⇒ Object



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

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



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

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

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



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

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



137
138
139
# File 'lib/active_object/string.rb', line 137

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

#last(limit = 1) ⇒ Object



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

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

#mixedcase? ⇒ Boolean

Returns:

  • (Boolean)


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

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

#ordinal ⇒ Object



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

def ordinal
  to_i.ordinal
end

#ordinalize ⇒ Object



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

def ordinalize
  to_i.ordinalize
end

#parameterize(sep = "-") ⇒ Object



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

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

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



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

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

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



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

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

#remove(*patterns) ⇒ Object



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

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

#remove!(*patterns) ⇒ Object



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

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

#remove_tags ⇒ Object



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

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

#remove_tags! ⇒ Object



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

def remove_tags!
  replace(remove_tags)
end

#shift(*patterns) ⇒ Object



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

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

#shift!(*patterns) ⇒ Object



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

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

#slugify ⇒ Object



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

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

#slugify! ⇒ Object



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

def slugify!
  replace(slugify)
end

#squish ⇒ Object



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

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

#squish! ⇒ Object



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

def squish!
  replace(squish)
end

#titleize ⇒ Object Also known as: titlecase



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

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

#titleize! ⇒ Object Also known as: titlecase!



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

def titleize!
  replace(titleize)
end

#to(position) ⇒ Object



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

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

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



269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/active_object/string.rb', line 269

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

  omission = options.fetch(:omission, "...")
  length_with_room_for_omission = truncate_at - omission.length

  stop = if options.fetch(:separator, false)
      rindex(options.fetch(:separator, ''), length_with_room_for_omission) || length_with_room_for_omission
    else
      length_with_room_for_omission
    end

  "#{self[0, stop]}#{omission}"
end

#truncate_words(words_count, options = {}) ⇒ Object



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

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

#underscore ⇒ Object



299
300
301
302
303
304
305
# File 'lib/active_object/string.rb', line 299

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



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

def underscore!
  replace(underscore)
end

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



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

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

#upcase? ⇒ Boolean

Returns:

  • (Boolean)


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

def upcase?
  upcase == self
end