Module: ActiveObject::String
- Defined in:
- lib/active_object/string.rb
Instance Method Summary collapse
- #any?(*keys) ⇒ Boolean
- #at(position) ⇒ Object
- #camelize(first_letter = :upper) ⇒ Object (also: #camelcase)
- #camelize!(first_letter = :upper) ⇒ Object (also: #camelcase!)
- #classify ⇒ Object
- #classify! ⇒ Object
- #constantize ⇒ Object
- #dasherize ⇒ Object
- #dasherize! ⇒ Object
- #deconstantize ⇒ Object
- #deconstantize! ⇒ Object
- #demodulize ⇒ Object
- #demodulize! ⇒ Object
- #domain ⇒ Object
- #downcase? ⇒ Boolean
- #ellipsize(ellipsize_at, options = {}) ⇒ Object
- #exclude?(string) ⇒ Boolean
- #first(limit = 1) ⇒ Object
- #format(*args) ⇒ Object
- #from(position) ⇒ Object
- #headerize ⇒ Object
- #headerize! ⇒ Object
- #humanize(options = {}) ⇒ Object
- #humanize!(options = {}) ⇒ Object
- #indent(amount, indent_string = nil, indent_empty_lines = false) ⇒ Object
- #indent!(amount, indent_string = nil, indent_empty_lines = false) ⇒ Object
- #index_all(pattern) ⇒ Object
- #labelize(options = {}) ⇒ Object (also: #labelcase)
- #labelize!(options = {}) ⇒ Object (also: #labelcase!)
- #last(limit = 1) ⇒ Object
- #mixedcase? ⇒ Boolean
- #ordinal ⇒ Object
- #ordinalize ⇒ Object
- #parameterize(separator: '-') ⇒ Object
- #parameterize!(separator: '-') ⇒ Object
- #pollute(delimiter = '^--^--^') ⇒ Object
- #pollute!(delimiter = '^--^--^') ⇒ Object
- #pop ⇒ Object
- #push(string) ⇒ Object
- #remove(*patterns) ⇒ Object
- #remove!(*patterns) ⇒ Object
- #remove_tags ⇒ Object
- #remove_tags! ⇒ Object
- #sample(separator = ' ') ⇒ Object
- #sample!(separator = ' ') ⇒ Object
- #shift(*patterns) ⇒ Object
- #shift!(*patterns) ⇒ Object
- #shuffle(separator = '') ⇒ Object
- #shuffle!(separator = '') ⇒ Object
- #sift(chars_to_keep) ⇒ Object
- #sift!(chars_to_keep) ⇒ Object
- #slugify ⇒ Object
- #slugify! ⇒ Object
- #sort ⇒ Object
- #sort! ⇒ Object
- #squish ⇒ Object
- #squish! ⇒ Object
- #titleize ⇒ Object (also: #titlecase)
- #titleize! ⇒ Object (also: #titlecase!)
- #to(position) ⇒ Object
- #truncate(truncate_at, options = {}) ⇒ Object
- #truncate_words(words_count, options = {}) ⇒ Object
- #underscore ⇒ Object
- #underscore! ⇒ Object
- #unpollute(delimiter = '^--^--^') ⇒ Object
- #unpollute!(delimiter = '^--^--^') ⇒ Object
- #unshift(*patterns) ⇒ Object
- #unshift!(*patterns) ⇒ Object
- #upcase? ⇒ Boolean
Instance Method Details
#any?(*keys) ⇒ Boolean
6 7 8 9 10 11 12 13 14 15 |
# 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
17 18 19 |
# File 'lib/active_object/string.rb', line 17 def at(position) self[position] end |
#camelize(first_letter = :upper) ⇒ Object Also known as: camelcase
21 22 23 24 25 26 27 28 |
# File 'lib/active_object/string.rb', line 21 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!
32 33 34 |
# File 'lib/active_object/string.rb', line 32 def camelize!(first_letter = :upper) replace(camelize(first_letter)) end |
#classify ⇒ Object
38 39 40 |
# File 'lib/active_object/string.rb', line 38 def classify to_s.sub(/.*\./, '').camelize end |
#classify! ⇒ Object
42 43 44 |
# File 'lib/active_object/string.rb', line 42 def classify! replace(classify) end |
#constantize ⇒ Object
46 47 48 |
# File 'lib/active_object/string.rb', line 46 def constantize ::Object.const_get(self) end |
#dasherize ⇒ Object
50 51 52 |
# File 'lib/active_object/string.rb', line 50 def dasherize tr(/_/, '-') end |
#dasherize! ⇒ Object
54 55 56 |
# File 'lib/active_object/string.rb', line 54 def dasherize! replace(dasherize) end |
#deconstantize ⇒ Object
58 59 60 |
# File 'lib/active_object/string.rb', line 58 def deconstantize to_s[0, rindex('::') || 0] end |
#deconstantize! ⇒ Object
62 63 64 |
# File 'lib/active_object/string.rb', line 62 def deconstantize! replace(deconstantize) end |
#demodulize ⇒ Object
66 67 68 |
# File 'lib/active_object/string.rb', line 66 def demodulize to_s.gsub(/^.*::/, '') end |
#demodulize! ⇒ Object
70 71 72 |
# File 'lib/active_object/string.rb', line 70 def demodulize! replace(demodulize) end |
#domain ⇒ Object
74 75 76 |
# File 'lib/active_object/string.rb', line 74 def domain self =~ %r{^(?:\w+:\/\/)?([^\/?]+)(?:\/|\?|$)} ? ::Regexp.last_match(1) : self end |
#downcase? ⇒ Boolean
78 79 80 |
# File 'lib/active_object/string.rb', line 78 def downcase? downcase == self end |
#ellipsize(ellipsize_at, options = {}) ⇒ Object
82 83 84 85 86 87 88 89 |
# File 'lib/active_object/string.rb', line 82 def ellipsize(ellipsize_at, = {}) return self if length <= ellipsize_at separator = [:separator] || '...' offset = [:offset] || 4 "#{self[0, offset]}#{separator}#{self[-offset, offset]}" end |
#exclude?(string) ⇒ Boolean
91 92 93 |
# File 'lib/active_object/string.rb', line 91 def exclude?(string) !include?(string) end |
#first(limit = 1) ⇒ Object
95 96 97 98 99 |
# File 'lib/active_object/string.rb', line 95 def first(limit = 1) return '' if limit.zero? limit >= length ? self : to(limit - 1) end |
#format(*args) ⇒ Object
101 102 103 |
# File 'lib/active_object/string.rb', line 101 def format(*args) super(self, *args.flatten) end |
#from(position) ⇒ Object
105 106 107 |
# File 'lib/active_object/string.rb', line 105 def from(position) self[position..-1] end |
#headerize ⇒ Object
109 110 111 |
# File 'lib/active_object/string.rb', line 109 def headerize squish.split(' ').map(&:capitalize).join(' ') end |
#headerize! ⇒ Object
113 114 115 |
# File 'lib/active_object/string.rb', line 113 def headerize! replace(headerize) end |
#humanize(options = {}) ⇒ Object
117 118 119 120 121 122 123 124 125 |
# File 'lib/active_object/string.rb', line 117 def humanize( = {}) capitalize = [: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
127 128 129 |
# File 'lib/active_object/string.rb', line 127 def humanize!( = {}) replace(humanize()) end |
#indent(amount, indent_string = nil, indent_empty_lines = false) ⇒ Object
131 132 133 134 135 136 |
# File 'lib/active_object/string.rb', line 131 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
138 139 140 |
# File 'lib/active_object/string.rb', line 138 def indent!(amount, indent_string = nil, indent_empty_lines = false) replace(indent(amount, indent_string, indent_empty_lines)) end |
#index_all(pattern) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/active_object/string.rb', line 142 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
156 157 158 159 160 161 162 163 164 |
# File 'lib/active_object/string.rb', line 156 def labelize( = {}) capitalize = [: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!
168 169 170 |
# File 'lib/active_object/string.rb', line 168 def labelize!( = {}) replace(labelize()) end |
#last(limit = 1) ⇒ Object
174 175 176 177 178 |
# File 'lib/active_object/string.rb', line 174 def last(limit = 1) return '' if limit.zero? limit >= length ? self : from(-limit) end |
#mixedcase? ⇒ Boolean
180 181 182 |
# File 'lib/active_object/string.rb', line 180 def mixedcase? !upcase? && !downcase? end |
#ordinal ⇒ Object
184 185 186 |
# File 'lib/active_object/string.rb', line 184 def ordinal to_i.ordinal end |
#ordinalize ⇒ Object
188 189 190 |
# File 'lib/active_object/string.rb', line 188 def ordinalize to_i.ordinalize end |
#parameterize(separator: '-') ⇒ Object
192 193 194 |
# File 'lib/active_object/string.rb', line 192 def parameterize(separator: '-') underscore.gsub(/\s+/, separator).downcase end |
#parameterize!(separator: '-') ⇒ Object
196 197 198 |
# File 'lib/active_object/string.rb', line 196 def parameterize!(separator: '-') replace(parameterize(separator: separator)) end |
#pollute(delimiter = '^--^--^') ⇒ Object
200 201 202 |
# File 'lib/active_object/string.rb', line 200 def pollute(delimiter = '^--^--^') split('').map { |chr| "#{chr}#{delimiter}" }.join end |
#pollute!(delimiter = '^--^--^') ⇒ Object
204 205 206 |
# File 'lib/active_object/string.rb', line 204 def pollute!(delimiter = '^--^--^') replace(pollute(delimiter)) end |
#pop ⇒ Object
208 209 210 |
# File 'lib/active_object/string.rb', line 208 def pop self[-1] end |
#push(string) ⇒ Object
212 213 214 |
# File 'lib/active_object/string.rb', line 212 def push(string) replace(concat(string)) end |
#remove(*patterns) ⇒ Object
216 217 218 219 220 |
# File 'lib/active_object/string.rb', line 216 def remove(*patterns) string = dup patterns.flatten.each { |pat| pat.is_a?(Range) ? string.slice!(pat) : string.gsub!(pat, '') } string end |
#remove!(*patterns) ⇒ Object
222 223 224 |
# File 'lib/active_object/string.rb', line 222 def remove!(*patterns) replace(remove(*patterns)) end |
#remove_tags ⇒ Object
226 227 228 |
# File 'lib/active_object/string.rb', line 226 def gsub(%r{<\/?[^>]*>}, '') end |
#remove_tags! ⇒ Object
230 231 232 |
# File 'lib/active_object/string.rb', line 230 def replace() end |
#sample(separator = ' ') ⇒ Object
234 235 236 |
# File 'lib/active_object/string.rb', line 234 def sample(separator = ' ') split(separator).sample end |
#sample!(separator = ' ') ⇒ Object
238 239 240 |
# File 'lib/active_object/string.rb', line 238 def sample!(separator = ' ') replace(sample(separator)) end |
#shift(*patterns) ⇒ Object
242 243 244 245 246 247 248 |
# File 'lib/active_object/string.rb', line 242 def shift(*patterns) return self[0] if patterns.empty? string = dup patterns.flatten.each { |pat| string.sub!(pat, '') } string end |
#shift!(*patterns) ⇒ Object
250 251 252 |
# File 'lib/active_object/string.rb', line 250 def shift!(*patterns) replace(shift(*patterns)) end |
#shuffle(separator = '') ⇒ Object
254 255 256 |
# File 'lib/active_object/string.rb', line 254 def shuffle(separator = '') split(separator).shuffle.join end |
#shuffle!(separator = '') ⇒ Object
258 259 260 |
# File 'lib/active_object/string.rb', line 258 def shuffle!(separator = '') replace(shuffle(separator)) end |
#sift(chars_to_keep) ⇒ Object
262 263 264 265 266 267 268 269 270 271 |
# File 'lib/active_object/string.rb', line 262 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
273 274 275 |
# File 'lib/active_object/string.rb', line 273 def sift!(chars_to_keep) replace(sift(chars_to_keep)) end |
#slugify ⇒ Object
277 278 279 280 281 282 283 |
# File 'lib/active_object/string.rb', line 277 def slugify to_s.gsub(/[^\x00-\x7F]+/, '') .gsub(/[^\w_ \-]+/i, '') .gsub(/[ \-]+/i, '-') .gsub(/^\-|\-$/i, '') .downcase end |
#slugify! ⇒ Object
285 286 287 |
# File 'lib/active_object/string.rb', line 285 def slugify! replace(slugify) end |
#sort ⇒ Object
297 298 299 |
# File 'lib/active_object/string.rb', line 297 def sort chars.sort.join end |
#sort! ⇒ Object
301 302 303 |
# File 'lib/active_object/string.rb', line 301 def sort! replace(sort) end |
#squish ⇒ Object
289 290 291 |
# File 'lib/active_object/string.rb', line 289 def squish strip.gsub(/\s+/, ' ') end |
#squish! ⇒ Object
293 294 295 |
# File 'lib/active_object/string.rb', line 293 def squish! replace(squish) end |
#titleize ⇒ Object Also known as: titlecase
305 306 307 |
# File 'lib/active_object/string.rb', line 305 def titleize underscore.humanize.gsub(/\b(?<!['’`])[a-z]/) { $&.capitalize } end |
#titleize! ⇒ Object Also known as: titlecase!
311 312 313 |
# File 'lib/active_object/string.rb', line 311 def titleize! replace(titleize) end |
#to(position) ⇒ Object
317 318 319 |
# File 'lib/active_object/string.rb', line 317 def to(position) self[0..position] end |
#truncate(truncate_at, options = {}) ⇒ Object
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/active_object/string.rb', line 321 def truncate(truncate_at, = {}) return dup unless length > truncate_at seperator = [:separator] omission = [:omission] || '...' size_with_room_for_omission = truncate_at - omission.length 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
337 338 339 340 341 342 343 |
# File 'lib/active_object/string.rb', line 337 def truncate_words(words_count, = {}) sep = [: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)}#{[:omissio] || '...'}" end |
#underscore ⇒ Object
345 346 347 348 349 350 351 |
# File 'lib/active_object/string.rb', line 345 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
353 354 355 |
# File 'lib/active_object/string.rb', line 353 def underscore! replace(underscore) end |
#unpollute(delimiter = '^--^--^') ⇒ Object
357 358 359 |
# File 'lib/active_object/string.rb', line 357 def unpollute(delimiter = '^--^--^') gsub(delimiter, '') end |
#unpollute!(delimiter = '^--^--^') ⇒ Object
361 362 363 |
# File 'lib/active_object/string.rb', line 361 def unpollute!(delimiter = '^--^--^') replace(unpollute(delimiter)) end |
#unshift(*patterns) ⇒ Object
369 370 371 372 373 374 |
# File 'lib/active_object/string.rb', line 369 def unshift(*patterns) string = '' patterns.flatten.each { |pat| string.concat(pat) } string.concat(self) string end |
#unshift!(*patterns) ⇒ Object
376 377 378 |
# File 'lib/active_object/string.rb', line 376 def unshift!(*patterns) replace(unshift(*patterns)) end |
#upcase? ⇒ Boolean
365 366 367 |
# File 'lib/active_object/string.rb', line 365 def upcase? upcase == self end |