class ActiveCleaner::BaseCleaner

The base cleaner.

Every cleaner inherit from it.

class MyCleaner < ActiveCleaner::BaseCleaner

  def clean_value(old_value, record = nil)
    old_value.gsub("foo", "bar")
  end

end

Attributes

attr_name[R]

Attribute name

options[R]

Options given to the cleaner.

Public Class Methods

kind() click to toggle source

The kind of the cleaner.

# File lib/active_cleaner/base_cleaner.rb, line 32
def self.kind
  @kind ||= name.split("::").last.underscore.sub(/_cleaner$/, "").to_sym
end
new(attr_name, options = {}) click to toggle source

Accepts options that will be made available through the options reader.

# File lib/active_cleaner/base_cleaner.rb, line 24
def initialize(attr_name, options = {})
  @attr_name = attr_name
  @options = {
    nilify: false,
  }.merge(options).freeze
end

Public Instance Methods

==(other) click to toggle source

Test whether or not two cleaners are equal.

# File lib/active_cleaner/base_cleaner.rb, line 69
def ==(other)
  kind == other.kind && attr_name == other.attr_name && options == other.options
end
clean(record) click to toggle source

Cleans the record by extracting the value of the field, cleaning it, and setting it back.

# File lib/active_cleaner/base_cleaner.rb, line 42
def clean(record)
  value = record.read_attribute_for_cleaning(attr_name)

  new_value = clean_value(value, record)

  new_value = nil if @options[:nilify] && nilify_value?(new_value, record)

  record.write_attribute_after_cleaning(attr_name, new_value) unless new_value == value
end
clean_value(_old_value, _record = nil) click to toggle source

Cleans the value.

It is expected that the returned value is the cleaned value.

The method needs to be implemented in the subclasses.

# File lib/active_cleaner/base_cleaner.rb, line 57
def clean_value(_old_value, _record = nil)
  raise NotImplementedError, "Subclasses must implement a clean(value, record=nil) method."
end
kind() click to toggle source

The kind of the cleaner.

# File lib/active_cleaner/base_cleaner.rb, line 37
def kind
  self.class.kind
end
nilify_value?(value, _record = nil) click to toggle source

Tests whether or not the value should be nilified.

This can be changed in the subclasses.

# File lib/active_cleaner/base_cleaner.rb, line 64
def nilify_value?(value, _record = nil)
  value == ""
end