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(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
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 |
# 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!
28 29 30 |
# File 'lib/active_object/string.rb', line 28 def camelize!(first_letter = :upper) replace(camelize(first_letter)) end |
#classify ⇒ Object
34 35 36 |
# File 'lib/active_object/string.rb', line 34 def classify to_s.sub(/.*\./, '').camelize end |
#classify! ⇒ Object
38 39 40 |
# File 'lib/active_object/string.rb', line 38 def classify! replace(classify) end |
#constantize ⇒ Object
42 43 44 |
# File 'lib/active_object/string.rb', line 42 def constantize Object.const_get(self) end |
#dasherize ⇒ Object
46 47 48 |
# File 'lib/active_object/string.rb', line 46 def dasherize tr(/_/, '-') end |
#dasherize! ⇒ Object
50 51 52 |
# File 'lib/active_object/string.rb', line 50 def dasherize! replace(dasherize) end |
#deconstantize ⇒ Object
54 55 56 |
# File 'lib/active_object/string.rb', line 54 def deconstantize to_s[0, rindex('::') || 0] end |
#deconstantize! ⇒ Object
58 59 60 |
# File 'lib/active_object/string.rb', line 58 def deconstantize! replace(deconstantize) end |
#demodulize ⇒ Object
62 63 64 |
# File 'lib/active_object/string.rb', line 62 def demodulize to_s.gsub(/^.*::/, '') end |
#demodulize! ⇒ Object
66 67 68 |
# File 'lib/active_object/string.rb', line 66 def demodulize! replace(demodulize) end |
#domain ⇒ Object
70 71 72 |
# File 'lib/active_object/string.rb', line 70 def domain self =~ %r{^(?:\w+:\/\/)?([^\/?]+)(?:\/|\?|$)} ? Regexp.last_match(1) : self end |
#downcase? ⇒ Boolean
74 75 76 |
# File 'lib/active_object/string.rb', line 74 def downcase? downcase == self end |
#ellipsize(ellipsize_at, options = {}) ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/active_object/string.rb', line 78 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
87 88 89 |
# File 'lib/active_object/string.rb', line 87 def exclude?(string) !include?(string) end |
#first(limit = 1) ⇒ Object
91 92 93 94 95 |
# File 'lib/active_object/string.rb', line 91 def first(limit = 1) return '' if limit.zero? limit >= length ? self : to(limit - 1) end |
#format(*args) ⇒ Object
97 98 99 |
# File 'lib/active_object/string.rb', line 97 def format(*args) super(self, *args.flatten) end |
#from(position) ⇒ Object
101 102 103 |
# File 'lib/active_object/string.rb', line 101 def from(position) self[position..-1] end |
#humanize(options = {}) ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'lib/active_object/string.rb', line 105 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
115 116 117 |
# File 'lib/active_object/string.rb', line 115 def humanize!( = {}) replace(humanize()) end |
#indent(amount, indent_string = nil, indent_empty_lines = false) ⇒ Object
119 120 121 122 123 124 |
# File 'lib/active_object/string.rb', line 119 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
126 127 128 |
# File 'lib/active_object/string.rb', line 126 def indent!(amount, indent_string = nil, indent_empty_lines = false) replace(indent(amount, indent_string, indent_empty_lines)) end |
#index_all(pattern) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/active_object/string.rb', line 130 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
144 145 146 147 148 149 150 151 152 |
# File 'lib/active_object/string.rb', line 144 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!
156 157 158 |
# File 'lib/active_object/string.rb', line 156 def labelize!( = {}) replace(labelize()) end |
#last(limit = 1) ⇒ Object
162 163 164 165 166 |
# File 'lib/active_object/string.rb', line 162 def last(limit = 1) return '' if limit.zero? limit >= length ? self : from(-limit) end |
#mixedcase? ⇒ Boolean
168 169 170 |
# File 'lib/active_object/string.rb', line 168 def mixedcase? !upcase? && !downcase? end |
#ordinal ⇒ Object
172 173 174 |
# File 'lib/active_object/string.rb', line 172 def ordinal to_i.ordinal end |
#ordinalize ⇒ Object
176 177 178 |
# File 'lib/active_object/string.rb', line 176 def ordinalize to_i.ordinalize end |
#parameterize(separator: '-') ⇒ Object
180 181 182 |
# File 'lib/active_object/string.rb', line 180 def parameterize(separator: '-') underscore.gsub(/\s+/, separator).downcase end |
#parameterize!(separator: '-') ⇒ Object
184 185 186 |
# File 'lib/active_object/string.rb', line 184 def parameterize!(separator: '-') replace(parameterize(separator: separator)) end |
#pollute(delimiter = '^--^--^') ⇒ Object
188 189 190 |
# File 'lib/active_object/string.rb', line 188 def pollute(delimiter = '^--^--^') split('').map { |chr| "#{chr}#{delimiter}" }.join end |
#pollute!(delimiter = '^--^--^') ⇒ Object
192 193 194 |
# File 'lib/active_object/string.rb', line 192 def pollute!(delimiter = '^--^--^') replace(pollute(delimiter)) end |
#pop ⇒ Object
196 197 198 |
# File 'lib/active_object/string.rb', line 196 def pop self[-1] end |
#push(string) ⇒ Object
200 201 202 |
# File 'lib/active_object/string.rb', line 200 def push(string) replace(concat(string)) end |
#remove(*patterns) ⇒ Object
204 205 206 207 208 |
# File 'lib/active_object/string.rb', line 204 def remove(*patterns) string = dup patterns.flatten.each { |pat| pat.is_a?(Range) ? string.slice!(pat) : string.gsub!(pat, '') } string end |
#remove!(*patterns) ⇒ Object
210 211 212 |
# File 'lib/active_object/string.rb', line 210 def remove!(*patterns) replace(remove(*patterns)) end |
#remove_tags ⇒ Object
214 215 216 |
# File 'lib/active_object/string.rb', line 214 def gsub(%r{<\/?[^>]*>}, '') end |
#remove_tags! ⇒ Object
218 219 220 |
# File 'lib/active_object/string.rb', line 218 def replace() end |
#sample(separator = ' ') ⇒ Object
222 223 224 |
# File 'lib/active_object/string.rb', line 222 def sample(separator = ' ') split(separator).sample end |
#sample!(separator = ' ') ⇒ Object
226 227 228 |
# File 'lib/active_object/string.rb', line 226 def sample!(separator = ' ') replace(sample(separator)) end |
#shift(*patterns) ⇒ Object
230 231 232 233 234 235 236 |
# File 'lib/active_object/string.rb', line 230 def shift(*patterns) return self[0] if patterns.empty? string = dup patterns.flatten.each { |pat| string.sub!(pat, '') } string end |
#shift!(*patterns) ⇒ Object
238 239 240 |
# File 'lib/active_object/string.rb', line 238 def shift!(*patterns) replace(shift(*patterns)) end |
#shuffle(separator = '') ⇒ Object
242 243 244 |
# File 'lib/active_object/string.rb', line 242 def shuffle(separator = '') split(separator).shuffle.join end |
#shuffle!(separator = '') ⇒ Object
246 247 248 |
# File 'lib/active_object/string.rb', line 246 def shuffle!(separator = '') replace(shuffle(separator)) end |
#sift(chars_to_keep) ⇒ Object
250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/active_object/string.rb', line 250 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
262 263 264 |
# File 'lib/active_object/string.rb', line 262 def sift!(chars_to_keep) replace(sift(chars_to_keep)) end |
#slugify ⇒ Object
266 267 268 269 270 271 272 |
# File 'lib/active_object/string.rb', line 266 def slugify to_s.gsub(/[^\x00-\x7F]+/, '') .gsub(/[^\w_ \-]+/i, '') .gsub(/[ \-]+/i, '-') .gsub(/^\-|\-$/i, '') .downcase end |
#slugify! ⇒ Object
274 275 276 |
# File 'lib/active_object/string.rb', line 274 def slugify! replace(slugify) end |
#sort ⇒ Object
286 287 288 |
# File 'lib/active_object/string.rb', line 286 def sort chars.sort.join end |
#sort! ⇒ Object
290 291 292 |
# File 'lib/active_object/string.rb', line 290 def sort! replace(sort) end |
#squish ⇒ Object
278 279 280 |
# File 'lib/active_object/string.rb', line 278 def squish strip.gsub(/\s+/, ' ') end |
#squish! ⇒ Object
282 283 284 |
# File 'lib/active_object/string.rb', line 282 def squish! replace(squish) end |
#titleize ⇒ Object Also known as: titlecase
294 295 296 |
# File 'lib/active_object/string.rb', line 294 def titleize underscore.humanize.gsub(/\b(?<!['’`])[a-z]/) { $&.capitalize } end |
#titleize! ⇒ Object Also known as: titlecase!
300 301 302 |
# File 'lib/active_object/string.rb', line 300 def titleize! replace(titleize) end |
#to(position) ⇒ Object
306 307 308 |
# File 'lib/active_object/string.rb', line 306 def to(position) self[0..position] end |
#truncate(truncate_at, options = {}) ⇒ Object
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/active_object/string.rb', line 310 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
326 327 328 329 330 331 332 333 |
# File 'lib/active_object/string.rb', line 326 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
335 336 337 338 339 340 341 |
# File 'lib/active_object/string.rb', line 335 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
343 344 345 |
# File 'lib/active_object/string.rb', line 343 def underscore! replace(underscore) end |
#unpollute(delimiter = '^--^--^') ⇒ Object
347 348 349 |
# File 'lib/active_object/string.rb', line 347 def unpollute(delimiter = '^--^--^') gsub(delimiter, '') end |
#unpollute!(delimiter = '^--^--^') ⇒ Object
351 352 353 |
# File 'lib/active_object/string.rb', line 351 def unpollute!(delimiter = '^--^--^') replace(unpollute(delimiter)) end |
#unshift(*patterns) ⇒ Object
359 360 361 362 363 364 |
# File 'lib/active_object/string.rb', line 359 def unshift(*patterns) string = '' patterns.flatten.each { |pat| string.concat(pat) } string.concat(self) string end |
#unshift!(*patterns) ⇒ Object
366 367 368 |
# File 'lib/active_object/string.rb', line 366 def unshift!(*patterns) replace(unshift(*patterns)) end |
#upcase? ⇒ Boolean
355 356 357 |
# File 'lib/active_object/string.rb', line 355 def upcase? upcase == self end |