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
- #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(seperator = '-') ⇒ Object
- #parameterize!(seperator = '-') ⇒ 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
3 4 5 6 7 8 9 10 |
# File 'lib/active_object/string.rb', line 3 def any?(*keys) included = false keys.flatten.each do |key| included = include?(key) break if included end included end |
#at(position) ⇒ Object
12 13 14 |
# File 'lib/active_object/string.rb', line 12 def at(position) self[position] end |
#camelize(first_letter = :upper) ⇒ Object Also known as: camelcase
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/active_object/string.rb', line 16 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!
30 31 32 |
# File 'lib/active_object/string.rb', line 30 def camelize!(first_letter = :upper) replace(camelize(first_letter)) end |
#classify ⇒ Object
36 37 38 |
# File 'lib/active_object/string.rb', line 36 def classify to_s.sub(/.*\./, '').camelize end |
#classify! ⇒ Object
40 41 42 |
# File 'lib/active_object/string.rb', line 40 def classify! replace(classify) end |
#constantize ⇒ Object
44 45 46 |
# File 'lib/active_object/string.rb', line 44 def constantize Object.const_get(self) end |
#dasherize ⇒ Object
48 49 50 |
# File 'lib/active_object/string.rb', line 48 def dasherize tr(/_/, '-') end |
#dasherize! ⇒ Object
52 53 54 |
# File 'lib/active_object/string.rb', line 52 def dasherize! replace(dasherize) end |
#deconstantize ⇒ Object
56 57 58 |
# File 'lib/active_object/string.rb', line 56 def deconstantize to_s[0, rindex('::') || 0] end |
#deconstantize! ⇒ Object
60 61 62 |
# File 'lib/active_object/string.rb', line 60 def deconstantize! replace(deconstantize) end |
#demodulize ⇒ Object
64 65 66 |
# File 'lib/active_object/string.rb', line 64 def demodulize to_s.gsub(/^.*::/, '') end |
#demodulize! ⇒ Object
68 69 70 |
# File 'lib/active_object/string.rb', line 68 def demodulize! replace(demodulize) end |
#domain ⇒ Object
72 73 74 |
# File 'lib/active_object/string.rb', line 72 def domain self =~ %r{^(?:\w+:\/\/)?([^\/?]+)(?:\/|\?|$)} ? Regexp.last_match(1) : self end |
#downcase? ⇒ Boolean
76 77 78 |
# File 'lib/active_object/string.rb', line 76 def downcase? downcase == self end |
#ellipsize(ellipsize_at, options = {}) ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/active_object/string.rb', line 80 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
89 90 91 |
# File 'lib/active_object/string.rb', line 89 def exclude?(string) !include?(string) end |
#first(limit = 1) ⇒ Object
93 94 95 96 97 |
# File 'lib/active_object/string.rb', line 93 def first(limit = 1) return('') if limit.zero? limit >= length ? self : to(limit - 1) end |
#format(*args) ⇒ Object
99 100 101 |
# File 'lib/active_object/string.rb', line 99 def format(*args) super(self, *args.flatten) end |
#from(position) ⇒ Object
103 104 105 |
# File 'lib/active_object/string.rb', line 103 def from(position) self[position..-1] end |
#humanize(options = {}) ⇒ Object
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/active_object/string.rb', line 107 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
118 119 120 |
# File 'lib/active_object/string.rb', line 118 def humanize!( = {}) replace(humanize()) 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 156 |
# File 'lib/active_object/string.rb', line 147 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!
160 161 162 |
# File 'lib/active_object/string.rb', line 160 def labelize!( = {}) replace(labelize()) end |
#last(limit = 1) ⇒ Object
166 167 168 169 170 |
# File 'lib/active_object/string.rb', line 166 def last(limit = 1) return('') if limit.zero? limit >= length ? self : from(-limit) end |
#mixedcase? ⇒ Boolean
172 173 174 |
# File 'lib/active_object/string.rb', line 172 def mixedcase? !upcase? && !downcase? end |
#ordinal ⇒ Object
176 177 178 |
# File 'lib/active_object/string.rb', line 176 def ordinal to_i.ordinal end |
#ordinalize ⇒ Object
180 181 182 |
# File 'lib/active_object/string.rb', line 180 def ordinalize to_i.ordinalize end |
#parameterize(seperator = '-') ⇒ Object
184 185 186 |
# File 'lib/active_object/string.rb', line 184 def parameterize(seperator = '-') underscore.gsub(/\s+/, seperator).downcase end |
#parameterize!(seperator = '-') ⇒ Object
188 189 190 |
# File 'lib/active_object/string.rb', line 188 def parameterize!(seperator = '-') replace(parameterize(seperator)) end |
#pollute(delimiter = '^--^--^') ⇒ Object
192 193 194 |
# File 'lib/active_object/string.rb', line 192 def pollute(delimiter = '^--^--^') split('').map { |chr| "#{chr}#{delimiter}" }.join end |
#pollute!(delimiter = '^--^--^') ⇒ Object
196 197 198 |
# File 'lib/active_object/string.rb', line 196 def pollute!(delimiter = '^--^--^') replace(pollute(delimiter)) end |
#pop ⇒ Object
200 201 202 |
# File 'lib/active_object/string.rb', line 200 def pop self[-1] end |
#push(string) ⇒ Object
204 205 206 |
# File 'lib/active_object/string.rb', line 204 def push(string) replace(concat(string)) end |
#remove(*patterns) ⇒ Object
208 209 210 211 212 213 214 215 216 |
# File 'lib/active_object/string.rb', line 208 def remove(*patterns) string = dup patterns.flatten.each do |pat| pat.is_a?(Range) ? string.slice!(pat) : string.gsub!(pat, '') end 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
222 223 224 |
# File 'lib/active_object/string.rb', line 222 def gsub(%r{<\/?[^>]*>}, '') end |
#remove_tags! ⇒ Object
226 227 228 |
# File 'lib/active_object/string.rb', line 226 def replace() end |
#sample(separator = ' ') ⇒ Object
230 231 232 |
# File 'lib/active_object/string.rb', line 230 def sample(separator = ' ') split(separator).sample end |
#sample!(separator = ' ') ⇒ Object
234 235 236 |
# File 'lib/active_object/string.rb', line 234 def sample!(separator = ' ') replace(sample(separator)) end |
#shift(*patterns) ⇒ Object
238 239 240 241 242 243 244 245 246 |
# File 'lib/active_object/string.rb', line 238 def shift(*patterns) if patterns.empty? self[0] else string = dup patterns.flatten.each { |pat| string.sub!(pat, '') } string end end |
#shift!(*patterns) ⇒ Object
248 249 250 |
# File 'lib/active_object/string.rb', line 248 def shift!(*patterns) replace(shift(*patterns)) end |
#shuffle(separator = '') ⇒ Object
252 253 254 |
# File 'lib/active_object/string.rb', line 252 def shuffle(separator = '') split(separator).shuffle.join end |
#shuffle!(separator = '') ⇒ Object
256 257 258 |
# File 'lib/active_object/string.rb', line 256 def shuffle!(separator = '') replace(shuffle(separator)) end |
#sift(chars_to_keep) ⇒ Object
260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/active_object/string.rb', line 260 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
272 273 274 |
# File 'lib/active_object/string.rb', line 272 def sift!(chars_to_keep) replace(sift(chars_to_keep)) end |
#slugify ⇒ Object
276 277 278 279 280 281 282 |
# File 'lib/active_object/string.rb', line 276 def slugify gsub(/[^\x00-\x7F]+/, '') .gsub(/[^\w_ \-]+/i, '') .gsub(/[ \-]+/i, '-') .gsub(/^\-|\-$/i, '') .downcase end |
#slugify! ⇒ Object
284 285 286 |
# File 'lib/active_object/string.rb', line 284 def slugify! replace(slugify) end |
#sort ⇒ Object
296 297 298 |
# File 'lib/active_object/string.rb', line 296 def sort chars.sort.join end |
#sort! ⇒ Object
300 301 302 |
# File 'lib/active_object/string.rb', line 300 def sort! replace(sort) end |
#squish ⇒ Object
288 289 290 |
# File 'lib/active_object/string.rb', line 288 def squish strip.gsub(/\s+/, ' ') end |
#squish! ⇒ Object
292 293 294 |
# File 'lib/active_object/string.rb', line 292 def squish! replace(squish) end |
#titleize ⇒ Object Also known as: titlecase
304 305 306 |
# File 'lib/active_object/string.rb', line 304 def titleize underscore.humanize.gsub(/\b(?<!['’`])[a-z]/) { $&.capitalize } end |
#titleize! ⇒ Object Also known as: titlecase!
310 311 312 |
# File 'lib/active_object/string.rb', line 310 def titleize! replace(titleize) end |
#to(position) ⇒ Object
316 317 318 |
# File 'lib/active_object/string.rb', line 316 def to(position) self[0..position] end |
#truncate(truncate_at, options = {}) ⇒ Object
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/active_object/string.rb', line 320 def truncate(truncate_at, = {}) return(dup) unless length > truncate_at omission = [:omission] || '...' size_with_room_for_omission = truncate_at - omission.length seperator = [: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
336 337 338 339 340 341 342 343 344 345 |
# File 'lib/active_object/string.rb', line 336 def truncate_words(words_count, = {}) sep = [:separator] || /\s+/ sep = Regexp.escape(sep.to_s) unless sep.is_a(Regexp) if self =~ /\A((?:.+?#{sep}){#{words_count - 1}}.+?)#{sep}.*/m "#{Regexp.last_match(1)}#{options[:omissio] || '...'}" else self end end |
#underscore ⇒ Object
347 348 349 350 351 352 353 |
# File 'lib/active_object/string.rb', line 347 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
355 356 357 |
# File 'lib/active_object/string.rb', line 355 def underscore! replace(underscore) end |
#unpollute(delimiter = '^--^--^') ⇒ Object
359 360 361 |
# File 'lib/active_object/string.rb', line 359 def unpollute(delimiter = '^--^--^') gsub(delimiter, '') end |
#unpollute!(delimiter = '^--^--^') ⇒ Object
363 364 365 |
# File 'lib/active_object/string.rb', line 363 def unpollute!(delimiter = '^--^--^') replace(unpollute(delimiter)) end |
#unshift(*patterns) ⇒ Object
371 372 373 374 375 376 |
# File 'lib/active_object/string.rb', line 371 def unshift(*patterns) string = '' patterns.flatten.each { |pat| string.concat(pat) } string.concat(self) string end |
#unshift!(*patterns) ⇒ Object
378 379 380 |
# File 'lib/active_object/string.rb', line 378 def unshift!(*patterns) replace(unshift(*patterns)) end |
#upcase? ⇒ Boolean
367 368 369 |
# File 'lib/active_object/string.rb', line 367 def upcase? upcase == self end |