Class: String
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
- #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
- #labelize(options = {}) ⇒ Object (also: #labelcase)
- #labelize!(options = {}) ⇒ Object (also: #labelcase!)
- #last(limit = 1) ⇒ Object
- #mixedcase? ⇒ Boolean
- #ordinal ⇒ Object
- #ordinalize ⇒ Object
- #parameterize(seperator = '-'.freeze) ⇒ Object
- #parameterize!(seperator = '-'.freeze) ⇒ Object
- #pollute(delimiter = '^--^--^'.freeze) ⇒ Object
- #pollute!(delimiter = '^--^--^'.freeze) ⇒ Object
- #remove(*patterns) ⇒ Object
- #remove!(*patterns) ⇒ Object
- #remove_tags ⇒ Object
- #remove_tags! ⇒ Object
- #sample(separator = ' '.freeze) ⇒ Object
- #sample!(separator = ' '.freeze) ⇒ Object
- #shift(*patterns) ⇒ Object
- #shift!(*patterns) ⇒ Object
- #shuffle(separator = ''.freeze) ⇒ Object
- #shuffle!(separator = ''.freeze) ⇒ Object
- #slugify ⇒ Object
- #slugify! ⇒ 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 = '^--^--^'.freeze) ⇒ Object
- #unpollute!(delimiter = '^--^--^'.freeze) ⇒ Object
- #upcase? ⇒ Boolean
Instance Method Details
#any?(*keys) ⇒ Boolean
3 4 5 6 7 |
# File 'lib/active_object/string.rb', line 3 def any?(*keys) included = false keys.flatten.each { |k| break if included = include?(k) } included end |
#at(position) ⇒ Object
10 11 12 |
# File 'lib/active_object/string.rb', line 10 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 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!
30 31 32 |
# File 'lib/active_object/string.rb', line 30 def camelize!(first_letter=:upper) replace(camelize(first_letter)) end |
#classify ⇒ Object
38 39 40 41 42 |
# File 'lib/active_object/string.rb', line 38 def classify to_s. sub(/.*\./, ''.freeze). camelize end |
#classify! ⇒ Object
46 47 48 |
# File 'lib/active_object/string.rb', line 46 def classify! replace(classify) end |
#constantize ⇒ Object
52 53 54 |
# File 'lib/active_object/string.rb', line 52 def constantize Object.const_get(self) end |
#dasherize ⇒ Object
58 59 60 |
# File 'lib/active_object/string.rb', line 58 def dasherize gsub(/_/, '-'.freeze) end |
#dasherize! ⇒ Object
64 65 66 |
# File 'lib/active_object/string.rb', line 64 def dasherize! replace(dasherize) end |
#deconstantize ⇒ Object
70 71 72 |
# File 'lib/active_object/string.rb', line 70 def deconstantize to_s[0, rindex('::'.freeze) || 0] end |
#deconstantize! ⇒ Object
76 77 78 |
# File 'lib/active_object/string.rb', line 76 def deconstantize! replace(deconstantize) end |
#demodulize ⇒ Object
82 83 84 |
# File 'lib/active_object/string.rb', line 82 def demodulize to_s.gsub(/^.*::/, ''.freeze) end |
#demodulize! ⇒ Object
88 89 90 |
# File 'lib/active_object/string.rb', line 88 def demodulize! replace(demodulize) end |
#domain ⇒ Object
93 94 95 96 |
# File 'lib/active_object/string.rb', line 93 def domain string = dup string =~ (/^(?:\w+:\/\/)?([^\/?]+)(?:\/|\?|$)/) ? $1 : string end |
#downcase? ⇒ Boolean
98 99 100 |
# File 'lib/active_object/string.rb', line 98 def downcase? downcase == self end |
#ellipsize(ellipsize_at, options = {}) ⇒ Object
102 103 104 105 106 107 108 109 |
# File 'lib/active_object/string.rb', line 102 def ellipsize(ellipsize_at, ={}) return(self)if length <= ellipsize_at separator = .fetch(:separator, '...'.freeze) offset = .fetch(:offset, 4) "#{self[0, offset]}#{separator}#{self[-offset, offset]}" end |
#exclude?(string) ⇒ Boolean
112 113 114 |
# File 'lib/active_object/string.rb', line 112 def exclude?(string) !include?(string) end |
#first(limit = 1) ⇒ Object
118 119 120 121 122 |
# File 'lib/active_object/string.rb', line 118 def first(limit=1) return(''.freeze) if limit.zero? limit >= length ? self.dup : to(limit - 1) end |
#from(position) ⇒ Object
126 127 128 |
# File 'lib/active_object/string.rb', line 126 def from(position) self[position..-1] end |
#humanize(options = {}) ⇒ Object
132 133 134 135 136 137 138 139 |
# File 'lib/active_object/string.rb', line 132 def humanize(={}) underscore. gsub(/_id\z/, ''.freeze). tr('_'.freeze, ' '.freeze). squish. gsub(/([a-z\d]*)/i) { |s| s.downcase }. gsub(/\A\w/) { |s| .fetch(:capitalize, true) ? s.upcase : s } end |
#humanize!(options = {}) ⇒ Object
143 144 145 |
# File 'lib/active_object/string.rb', line 143 def humanize!(={}) replace(humanize()) end |
#indent(amount, indent_string = nil, indent_empty_lines = false) ⇒ Object
149 150 151 152 153 154 |
# File 'lib/active_object/string.rb', line 149 def indent(amount, indent_string=nil, indent_empty_lines=false) indent_string = indent_string || self[/^[ \t]/] || ' '.freeze substitutes = indent_empty_lines ? /^/ : /^(?!$)/ gsub(substitutes, indent_string * amount) end |
#indent!(amount, indent_string = nil, indent_empty_lines = false) ⇒ Object
158 159 160 |
# File 'lib/active_object/string.rb', line 158 def indent!(amount, indent_string=nil, indent_empty_lines=false) replace(indent(amount, indent_string, indent_empty_lines)) end |
#labelize(options = {}) ⇒ Object Also known as: labelcase
163 164 165 166 167 168 169 170 |
# File 'lib/active_object/string.rb', line 163 def labelize(={}) underscore. tr('_'.freeze, ' '.freeze). squish. gsub(/([a-z\d]*)/i) { |s| s.downcase }. gsub(/\A\w/) { |s| .fetch(:capitalize, true) ? s.upcase : s }. gsub(/ id\z/, ' ID'.freeze) end |
#labelize!(options = {}) ⇒ Object Also known as: labelcase!
174 175 176 |
# File 'lib/active_object/string.rb', line 174 def labelize!(={}) replace(labelize()) end |
#last(limit = 1) ⇒ Object
181 182 183 184 185 |
# File 'lib/active_object/string.rb', line 181 def last(limit=1) return(''.freeze) if limit.zero? limit >= length ? self.dup : from(-limit) end |
#mixedcase? ⇒ Boolean
188 189 190 |
# File 'lib/active_object/string.rb', line 188 def mixedcase? !upcase? && !downcase? end |
#ordinal ⇒ Object
193 194 195 |
# File 'lib/active_object/string.rb', line 193 def ordinal to_i.ordinal end |
#ordinalize ⇒ Object
199 200 201 |
# File 'lib/active_object/string.rb', line 199 def ordinalize to_i.ordinalize end |
#parameterize(seperator = '-'.freeze) ⇒ Object
205 206 207 208 209 210 |
# File 'lib/active_object/string.rb', line 205 def parameterize(seperator='-'.freeze) underscore. gsub(/\s+/, '_'.freeze). gsub('_'.freeze, seperator). downcase end |
#parameterize!(seperator = '-'.freeze) ⇒ Object
214 215 216 |
# File 'lib/active_object/string.rb', line 214 def parameterize!(seperator='-'.freeze) replace(parameterize(seperator)) end |
#pollute(delimiter = '^--^--^'.freeze) ⇒ Object
219 220 221 |
# File 'lib/active_object/string.rb', line 219 def pollute(delimiter='^--^--^'.freeze) split(''.freeze).map { |c| "#{c}#{delimiter}" }.join end |
#pollute!(delimiter = '^--^--^'.freeze) ⇒ Object
223 224 225 |
# File 'lib/active_object/string.rb', line 223 def pollute!(delimiter='^--^--^'.freeze) replace(pollute(delimiter)) end |
#remove(*patterns) ⇒ Object
228 229 230 231 232 |
# File 'lib/active_object/string.rb', line 228 def remove(*patterns) string = dup patterns.flatten.each { |p| string.gsub!(p, ''.freeze) } string end |
#remove!(*patterns) ⇒ Object
236 237 238 |
# File 'lib/active_object/string.rb', line 236 def remove!(*patterns) replace(remove(*patterns)) end |
#remove_tags ⇒ Object
241 242 243 |
# File 'lib/active_object/string.rb', line 241 def gsub(/<\/?[^>]*>/, ''.freeze) end |
#remove_tags! ⇒ Object
245 246 247 |
# File 'lib/active_object/string.rb', line 245 def replace() end |
#sample(separator = ' '.freeze) ⇒ Object
249 250 251 |
# File 'lib/active_object/string.rb', line 249 def sample(separator=' '.freeze) split(separator).sample end |
#sample!(separator = ' '.freeze) ⇒ Object
253 254 255 |
# File 'lib/active_object/string.rb', line 253 def sample!(separator=' '.freeze) replace(sample(separator)) end |
#shift(*patterns) ⇒ Object
257 258 259 260 261 |
# File 'lib/active_object/string.rb', line 257 def shift(*patterns) string = dup patterns.flatten.each { |p| string.sub!(p, ''.freeze) } string end |
#shift!(*patterns) ⇒ Object
263 264 265 |
# File 'lib/active_object/string.rb', line 263 def shift!(*patterns) replace(shift(*patterns)) end |
#shuffle(separator = ''.freeze) ⇒ Object
267 268 269 |
# File 'lib/active_object/string.rb', line 267 def shuffle(separator=''.freeze) split(separator).shuffle.join end |
#shuffle!(separator = ''.freeze) ⇒ Object
271 272 273 |
# File 'lib/active_object/string.rb', line 271 def shuffle!(separator=''.freeze) replace(shuffle(separator)) end |
#slugify ⇒ Object
275 276 277 278 279 280 281 |
# File 'lib/active_object/string.rb', line 275 def slugify gsub(/[^\x00-\x7F]+/, ''.freeze). gsub(/[^\w_ \-]+/i, ''.freeze). gsub(/[ \-]+/i, '-'.freeze). gsub(/^\-|\-$/i, ''.freeze). downcase end |
#slugify! ⇒ Object
283 284 285 |
# File 'lib/active_object/string.rb', line 283 def slugify! replace(slugify) end |
#squish ⇒ Object
288 289 290 291 |
# File 'lib/active_object/string.rb', line 288 def squish strip. gsub(/\s+/, ' '.freeze) end |
#squish! ⇒ Object
295 296 297 |
# File 'lib/active_object/string.rb', line 295 def squish! replace(squish) end |
#titleize ⇒ Object Also known as: titlecase
301 302 303 304 305 |
# File 'lib/active_object/string.rb', line 301 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
319 320 321 |
# File 'lib/active_object/string.rb', line 319 def to(position) self[0..position] end |
#truncate(truncate_at, options = {}) ⇒ Object
325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/active_object/string.rb', line 325 def truncate(truncate_at, ={}) return(dup) unless length > truncate_at omission = .fetch(:omission, '...'.freeze) size_with_room_for_omission = truncate_at - omission.length stop = if .fetch(:separator, false) rindex(.fetch(:separator, ''.freeze), 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
342 343 344 345 346 347 348 349 350 351 |
# File 'lib/active_object/string.rb', line 342 def truncate_words(words_count, ={}) sep = .fetch(:separator, /\s+/) sep = Regexp.escape(sep.to_s) unless Regexp === sep if self =~ /\A((?:.+?#{sep}){#{words_count - 1}}.+?)#{sep}.*/m "#{$1}#{options.fetch(:omission, '...'.freeze)}" else dup end end |
#underscore ⇒ Object
355 356 357 358 359 360 361 |
# File 'lib/active_object/string.rb', line 355 def underscore gsub(/::/, '/'.freeze). gsub(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2'.freeze). gsub(/([a-z\d])([A-Z])/, '\1_\2'.freeze). tr('-'.freeze, '_'.freeze). downcase end |
#underscore! ⇒ Object
365 366 367 |
# File 'lib/active_object/string.rb', line 365 def underscore! replace(underscore) end |
#unpollute(delimiter = '^--^--^'.freeze) ⇒ Object
370 371 372 |
# File 'lib/active_object/string.rb', line 370 def unpollute(delimiter='^--^--^'.freeze) gsub(delimiter, ''.freeze) end |
#unpollute!(delimiter = '^--^--^'.freeze) ⇒ Object
374 375 376 |
# File 'lib/active_object/string.rb', line 374 def unpollute!(delimiter='^--^--^'.freeze) replace(unpollute(delimiter)) end |
#upcase? ⇒ Boolean
378 379 380 |
# File 'lib/active_object/string.rb', line 378 def upcase? upcase == self end |