class ActiveCleaner::StringCleaner

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"

Public Instance Methods

clean_value(old_value, _record = nil) click to toggle source

Cleans the value.

# 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