class String unless method_defined?("camelize") def camelize(first_letter_in_uppercase = true) if first_letter_in_uppercase != :lower self.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } else self.to_s[0].chr.downcase + camelize(self)[1..-1] end end end unless method_defined?("ends_with?") def ends_with?(suffix) suffix = suffix.to_s self[-suffix.length, suffix.length] == suffix end end unless method_defined?("humanize") def humanize(options = {}) self.underscore. gsub(/_id$/, ""). tr('_', ' '). gsub(/([a-z\d]*)/i) { |match| match.downcase }. gsub(/^\w/) { |match| match.upcase } if options.fetch(:capitalize, true) end end unless method_defined?("titleize") def titleize self.underscore. humanize. gsub(/\b(?]*>/, "") end def strip_whitespace self.gnix("\t").split(" ").remove_blanks.join(" ") end def truncate_preserving_words(options={}) separator = options[:separator] || '...' max_words = options[:max_words] || nil if max_words words = self.split() return self if words.size < max_words words = words[0..(max_words-1)] words << separator words.join(" ") else max_chars = options[:max_chars] || 30 return self if self.size < max_chars out = self[0..(max_chars-1)].split(" ") out.pop out << separator out.join(" ") end end def unpollute(delimiter="^--^--^") self.gsub(delimiter, "") end def upcase? self.upcase == self end end