Class: ActiveCleaner::TextCleaner

Inherits:
BaseCleaner show all
Defined in:
lib/active_cleaner/text_cleaner.rb

Instance Attribute Summary

Attributes inherited from BaseCleaner

#attr_name, #options

Instance Method Summary collapse

Methods inherited from BaseCleaner

#==, #clean, #initialize, kind, #kind, #nilify_value?

Constructor Details

This class inherits a constructor from ActiveCleaner::BaseCleaner

Instance Method Details

#clean_value(old_value, record = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/active_cleaner/text_cleaner.rb', line 6

def clean_value(old_value, record=nil)
  unless old_value.nil?
    value = old_value.dup

    value.strip!

    # clean the new lines mess among OS
    value.gsub!(/\r\n|\r/, "\n")

    # protect stuff to keep with a markup
    value.gsub!(/\n/, "__NEW_LINE__")

    value.gsub!(/\s+/, " ")
    value.gsub!(/ ?__NEW_LINE__ ?/, "__NEW_LINE__")
    value.gsub!(/(__NEW_LINE__){3,}/, "__NEW_LINE____NEW_LINE__")

    # reverse the safe markup
    value.gsub!(/__NEW_LINE__/, "\n")

    value
  end
end