lib/active_cleaner/text_cleaner.rb in activecleaner-0.3.1 vs lib/active_cleaner/text_cleaner.rb in activecleaner-0.3.2
- old
+ new
@@ -1,10 +1,38 @@
-# encoding: utf-8
+# frozen_string_literal: true
module ActiveCleaner
+ # = TextCleaner
+ #
+ # Cleans a string by squishing all the extra space characters, but preserves new lines
+ # (with a max of 2 successive new lines).
+ #
+ # Useful when the field is rendered with the +simple_format+ Rails helper.
+ #
+ # It turns <tt>" My first line \n My second line \t "</tt> into <tt>"My first line\nMy second line"</tt>.
+ #
+ # == Options
+ #
+ # [:nilify]
+ # Whether or not set the field to +nil+ when the field was or is cleaned to <tt>""</tt>.
+ # Default to +false+.
+ #
+ # == Example
+ #
+ # class Article
+ # include ActiveCleaner
+ #
+ # clean :body, as: :text
+ # end
+ #
+ # article = Article.new(body: " My first paragraph \n \n \t \n My second paragraph, \n longer. \t ")
+ # article.save
+ # article.body
+ # # => "My first paragraph\n\nMy second paragraph,\nlonger."
class TextCleaner < BaseCleaner
- def clean_value(old_value, record=nil)
+ # Cleans the value.
+ def clean_value(old_value, _record = nil)
case old_value
when String
value = old_value.dup
value.strip!