module ActiveObject::String
Public Instance Methods
any?(*keys)
click to toggle source
# File lib/active_object/string.rb, line 36 def any?(*keys) included = false keys.flatten.each do |key| included = include?(key) break if included end included end
at(position)
click to toggle source
# File lib/active_object/string.rb, line 47 def at(position) self[position] end
camelize(first_letter = :upper)
click to toggle source
# File lib/active_object/string.rb, line 51 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
Also aliased as: camelcase
camelize!(first_letter = :upper)
click to toggle source
# File lib/active_object/string.rb, line 62 def camelize!(first_letter = :upper) replace(camelize(first_letter)) end
Also aliased as: camelcase!
classify()
click to toggle source
# File lib/active_object/string.rb, line 68 def classify to_s.sub(/.*\./, '').camelize end
classify!()
click to toggle source
# File lib/active_object/string.rb, line 72 def classify! replace(classify) end
constantize()
click to toggle source
# File lib/active_object/string.rb, line 76 def constantize ::Object.const_get(self) end
dasherize()
click to toggle source
# File lib/active_object/string.rb, line 80 def dasherize tr(/_/, '-') end
dasherize!()
click to toggle source
# File lib/active_object/string.rb, line 84 def dasherize! replace(dasherize) end
deconstantize()
click to toggle source
# File lib/active_object/string.rb, line 88 def deconstantize to_s[0, rindex('::') || 0] end
deconstantize!()
click to toggle source
# File lib/active_object/string.rb, line 92 def deconstantize! replace(deconstantize) end
demodulize()
click to toggle source
# File lib/active_object/string.rb, line 96 def demodulize to_s.gsub(/^.*::/, '') end
demodulize!()
click to toggle source
# File lib/active_object/string.rb, line 100 def demodulize! replace(demodulize) end
domain()
click to toggle source
# File lib/active_object/string.rb, line 104 def domain self =~ %r{^(?:\w+:\/\/)?([^\/?]+)(?:\/|\?|$)} ? ::Regexp.last_match(1) : self end
downcase?()
click to toggle source
# File lib/active_object/string.rb, line 108 def downcase? downcase == self end
ellipsize(ellipsize_at, options = {})
click to toggle source
# File lib/active_object/string.rb, line 112 def ellipsize(ellipsize_at, options = {}) return self if length <= ellipsize_at separator = options[:separator] || '...' offset = options[:offset] || 4 "#{self[0, offset]}#{separator}#{self[-offset, offset]}" end
exclude?(string)
click to toggle source
# File lib/active_object/string.rb, line 121 def exclude?(string) !include?(string) end
first(limit = 1)
click to toggle source
# File lib/active_object/string.rb, line 125 def first(limit = 1) return '' if limit.zero? limit >= length ? self : to(limit - 1) end
format(*args)
click to toggle source
Calls superclass method
# File lib/active_object/string.rb, line 131 def format(*args) super(self, *args.flatten) end
from(position)
click to toggle source
# File lib/active_object/string.rb, line 135 def from(position) self[position..-1] end
headerize()
click to toggle source
# File lib/active_object/string.rb, line 139 def headerize squish.split(' ').map(&:capitalize).join(' ') end
headerize!()
click to toggle source
# File lib/active_object/string.rb, line 143 def headerize! replace(headerize) end
humanize(options = {})
click to toggle source
# File lib/active_object/string.rb, line 147 def humanize(options = {}) capitalize = options[: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 = {})
click to toggle source
# File lib/active_object/string.rb, line 157 def humanize!(options = {}) replace(humanize(options)) end
indent(amount, indent_string = nil, indent_empty_lines = false)
click to toggle source
# File lib/active_object/string.rb, line 161 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)
click to toggle source
# File lib/active_object/string.rb, line 168 def indent!(amount, indent_string = nil, indent_empty_lines = false) replace(indent(amount, indent_string, indent_empty_lines)) end
index_all(pattern)
click to toggle source
# File lib/active_object/string.rb, line 172 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 = {})
click to toggle source
# File lib/active_object/string.rb, line 186 def labelize(options = {}) capitalize = options[:capitalize] || true underscore.tr('_', ' ') .squish .gsub(/([a-z\d]*)/i, &:downcase) .gsub(/\A\w/) { |str| capitalize ? str.upcase : str } .gsub(/ id\z/, ' ID') end
Also aliased as: labelcase
labelize!(options = {})
click to toggle source
# File lib/active_object/string.rb, line 198 def labelize!(options = {}) replace(labelize(options)) end
Also aliased as: labelcase!
last(limit = 1)
click to toggle source
# File lib/active_object/string.rb, line 204 def last(limit = 1) return '' if limit.zero? limit >= length ? self : from(-limit) end
mixedcase?()
click to toggle source
# File lib/active_object/string.rb, line 210 def mixedcase? !upcase? && !downcase? end
ordinal()
click to toggle source
# File lib/active_object/string.rb, line 214 def ordinal to_i.ordinal end
ordinalize()
click to toggle source
# File lib/active_object/string.rb, line 218 def ordinalize to_i.ordinalize end
parameterize(separator: '-')
click to toggle source
# File lib/active_object/string.rb, line 222 def parameterize(separator: '-') underscore.gsub(/\s+/, separator).downcase end
parameterize!(separator: '-')
click to toggle source
# File lib/active_object/string.rb, line 226 def parameterize!(separator: '-') replace(parameterize(separator: separator)) end
pollute(delimiter = '^--^--^')
click to toggle source
# File lib/active_object/string.rb, line 230 def pollute(delimiter = '^--^--^') split('').map { |chr| "#{chr}#{delimiter}" }.join end
pollute!(delimiter = '^--^--^')
click to toggle source
# File lib/active_object/string.rb, line 234 def pollute!(delimiter = '^--^--^') replace(pollute(delimiter)) end
pop()
click to toggle source
# File lib/active_object/string.rb, line 238 def pop self[-1] end
push(string)
click to toggle source
# File lib/active_object/string.rb, line 242 def push(string) replace(concat(string)) end
remove(*patterns)
click to toggle source
# File lib/active_object/string.rb, line 246 def remove(*patterns) string = dup patterns.flatten.each { |pat| pat.is_a?(Range) ? string.slice!(pat) : string.gsub!(pat, '') } string end
remove!(*patterns)
click to toggle source
# File lib/active_object/string.rb, line 252 def remove!(*patterns) replace(remove(*patterns)) end
sample(separator = ' ')
click to toggle source
# File lib/active_object/string.rb, line 264 def sample(separator = ' ') split(separator).sample end
sample!(separator = ' ')
click to toggle source
# File lib/active_object/string.rb, line 268 def sample!(separator = ' ') replace(sample(separator)) end
shift(*patterns)
click to toggle source
# File lib/active_object/string.rb, line 272 def shift(*patterns) return self[0] if patterns.empty? string = dup patterns.flatten.each { |pat| string.sub!(pat, '') } string end
shift!(*patterns)
click to toggle source
# File lib/active_object/string.rb, line 280 def shift!(*patterns) replace(shift(*patterns)) end
shuffle(separator = '')
click to toggle source
# File lib/active_object/string.rb, line 284 def shuffle(separator = '') split(separator).shuffle.join end
shuffle!(separator = '')
click to toggle source
# File lib/active_object/string.rb, line 288 def shuffle!(separator = '') replace(shuffle(separator)) end
sift(chars_to_keep)
click to toggle source
# File lib/active_object/string.rb, line 292 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)
click to toggle source
# File lib/active_object/string.rb, line 303 def sift!(chars_to_keep) replace(sift(chars_to_keep)) end
slugify()
click to toggle source
# File lib/active_object/string.rb, line 307 def slugify to_s.gsub(/[^\x00-\x7F]+/, '') .gsub(/[^\w_ \-]+/i, '') .gsub(/[ \-]+/i, '-') .gsub(/^\-|\-$/i, '') .downcase end
slugify!()
click to toggle source
# File lib/active_object/string.rb, line 315 def slugify! replace(slugify) end
sort()
click to toggle source
# File lib/active_object/string.rb, line 327 def sort chars.sort.join end
sort!()
click to toggle source
# File lib/active_object/string.rb, line 331 def sort! replace(sort) end
squish()
click to toggle source
# File lib/active_object/string.rb, line 319 def squish strip.gsub(/\s+/, ' ') end
squish!()
click to toggle source
# File lib/active_object/string.rb, line 323 def squish! replace(squish) end
titleize()
click to toggle source
# File lib/active_object/string.rb, line 335 def titleize underscore.humanize.gsub(/\b(?<!['’`])[a-z]/) { $&.capitalize } end
Also aliased as: titlecase
titleize!()
click to toggle source
# File lib/active_object/string.rb, line 341 def titleize! replace(titleize) end
Also aliased as: titlecase!
to(position)
click to toggle source
# File lib/active_object/string.rb, line 347 def to(position) self[0..position] end
transliterize()
click to toggle source
# File lib/active_object/string.rb, line 351 def transliterize TRANSLITERATIONS.each_with_object(dup) { |(k, v), str| str.gsub!(k, v) } end
transliterize!()
click to toggle source
# File lib/active_object/string.rb, line 355 def transliterize! TRANSLITERATIONS.each_with_object(self) { |(k, v), str| str.gsub!(k, v) } end
truncate(truncate_at, options = {})
click to toggle source
# File lib/active_object/string.rb, line 359 def truncate(truncate_at, options = {}) return dup unless length > truncate_at seperator = options[:separator] omission = options[: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 = {})
click to toggle source
# File lib/active_object/string.rb, line 375 def truncate_words(words_count, options = {}) sep = options[: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)}#{options[:omissio] || '...'}" end
underscore()
click to toggle source
# File lib/active_object/string.rb, line 383 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!()
click to toggle source
# File lib/active_object/string.rb, line 391 def underscore! replace(underscore) end
unpollute(delimiter = '^--^--^')
click to toggle source
# File lib/active_object/string.rb, line 395 def unpollute(delimiter = '^--^--^') gsub(delimiter, '') end
unpollute!(delimiter = '^--^--^')
click to toggle source
# File lib/active_object/string.rb, line 399 def unpollute!(delimiter = '^--^--^') replace(unpollute(delimiter)) end
unshift(*patterns)
click to toggle source
# File lib/active_object/string.rb, line 407 def unshift(*patterns) string = '' patterns.flatten.each { |pat| string.concat(pat) } string.concat(self) string end
unshift!(*patterns)
click to toggle source
# File lib/active_object/string.rb, line 414 def unshift!(*patterns) replace(unshift(*patterns)) end
upcase?()
click to toggle source
# File lib/active_object/string.rb, line 403 def upcase? upcase == self end