lib/lite/ruby/safe/string.rb in lite-ruby-1.3.3 vs lib/lite/ruby/safe/string.rb in lite-ruby-2.0.0

- old
+ new

@@ -149,19 +149,20 @@ def truncate(truncate_at, options = {}) return self unless length > truncate_at omission = options[:omission] || '...' seperator = options[:separator] - size_with_room_for_omission = truncate_at - omission.length + stop = rindex(seperator || '', size_with_room_for_omission) if seperator + "#{self[0, stop || size_with_room_for_omission]}#{omission}" + end - stop = if seperator - rindex(seperator || '', size_with_room_for_omission) || size_with_room_for_omission - else - size_with_room_for_omission - end + def truncate_words(words_count, options = {}) + sep = options[:separator] || /\s+/ + sep = Regexp.escape(sep.to_s) unless sep.is_a?(Regexp) + return dup unless self =~ /\A((?>.+?#{sep}){#{words_count - 1}}.+?)#{sep}.*/m - "#{self[0, stop]}#{omission}" + $1 + (options[:omission] || '...') end def underscore str = dup str.camelize!