lib/padrino-helpers/format_helpers.rb in padrino-helpers-0.9.28 vs lib/padrino-helpers/format_helpers.rb in padrino-helpers-0.9.29

- old
+ new

@@ -73,9 +73,25 @@ len = options[:length] - options[:omission].length chars = text (chars.length > options[:length] ? chars[0...len] + options[:omission] : text).to_s end end + + ## + # Truncates words of a given text after a given :length if number of words in text is more than :length (defaults to 30). + # The last words will be replaced with the :omission (defaults to "…") for a total number of words not exceeding :length. + # + # ==== Examples + # + # truncate_words("Once upon a time in a world far far away", :length => 8) => "Once upon a time in a world far..." + # + def truncate_words(text, options={}) + options.reverse_merge!(:length => 30, :omission => "...") + if text + words = text.split() + words[0..(options[:length]-1)].join(' ') + (words.length > options[:length] ? options[:omission] : '') + end + end ## # Wraps the text into lines no longer than line_width width. # This method breaks on the first whitespace character that does not exceed line_width (which is 80 by default). # \ No newline at end of file