Class: ActiveCleaner::StringCleaner

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

Overview

StringCleaner

Cleans a string by squishing all the extra space characters.

It turns " A \n \t title \t " into "A title".

Options

:nilify

Whether or not set the field to nil when the field was or is cleaned to "". Default to false.

Example

class Article
  include ActiveCleaner

  clean :title, as: :string
end

article = Article.new(title: "   My  \n  \t Title   \t  ")
article.save
article.title
# => "My Title"

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

Cleans the value.



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/active_cleaner/string_cleaner.rb', line 31

def clean_value(old_value, _record = nil)
  case old_value
  when String
    value = old_value.dup

    value.strip!
    value.gsub!(/\s+/, " ")

    value
  else
    old_value
  end
end