# frozen_string_literal: true
module ActiveCleaner
# = 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"
class StringCleaner < BaseCleaner
# Cleans the value.
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
end
end