module DataCleansing::Cleanse::ClassMethods

Public Instance Methods

after_cleanse(*methods) click to toggle source

Add one or more methods on this object to be called after cleansing is complete on an object After cleansers are executed when cleanse_attributes! is called, but after all other defined cleansers have been executed. They are not called when .cleanse_attribute is called

After cleaners should be used when based on the value of one attribute, one or more of the other attributes need to be modified

# File lib/data_cleansing/cleanse.rb, line 34
def after_cleanse(*methods)
  methods.each do |m|
    raise "Method #{m.inspect} must be a symbol" unless m.is_a?(Symbol)
    data_cleansing_after_cleaners << m unless data_cleansing_after_cleaners.include?(m)
  end
end
cleanse(*args) click to toggle source

Define how to cleanse one or more attributes

# File lib/data_cleansing/cleanse.rb, line 9
def cleanse(*args)
  last       = args.last
  attributes = args.dup
  params     = (last.is_a?(Hash) && last.instance_of?(Hash)) ? attributes.pop.dup : {}
  cleaners   = Array(params.delete(:cleaner))
  raise(ArgumentError, "Mandatory :cleaner parameter is missing: #{params.inspect}") unless cleaners

  cleaner = DataCleansingCleaner.new(cleaners, attributes, params)
  data_cleansing_cleaners << cleaner

  # Create shortcuts to cleaners for each attribute for use by .cleanse_attribute
  attributes.each do |attr|
    (data_cleansing_attribute_cleaners[attr] ||= Concurrent::Array.new) << cleaner
  end
  cleaner
end
cleanse_attribute(attribute_name, value, object=nil) click to toggle source

Returns the value cleansed using the cleaners defined for that attribute in this model and any of it's parents

Parameters

attribute_name
  Name of the attribute within this Class to be cleansed
value
  Value to be cleansed
object
  If supplied the cleansing will be performed within the scope of
  that object so that cleaners can read and write to attributes
  of that object

Warning: If any of the cleaners read or write to other object attributes

then a valid object instance must be supplied
# File lib/data_cleansing/cleanse.rb, line 56
def cleanse_attribute(attribute_name, value, object=nil)
  return if value.nil?

  # Collect parent cleaners first, starting with the top parent
  cleaners = []
  klass    = self
  while klass != Object
    if klass.respond_to?(:data_cleansing_attribute_cleaners)
      cleaners += klass.data_cleansing_attribute_cleaners[:all] || []
      cleaners += klass.data_cleansing_attribute_cleaners[attribute_name.to_sym] || []
    end
    klass = klass.superclass
  end
  # Support Integer values
  cleansed_value = value.is_a?(Integer) ? value : value.dup
  cleaners.reverse_each { |cleaner| cleansed_value = data_cleansing_clean(cleaner, cleansed_value, object) if cleaner }
  cleansed_value
end
data_cleansing_after_cleaners() click to toggle source

Array of cleaners to execute against this model and it's children

# File lib/data_cleansing/cleanse.rb, line 81
def data_cleansing_after_cleaners
  @data_cleansing_after_cleaners ||= Concurrent::Array.new
end
data_cleansing_attribute_cleaners() click to toggle source

Hash of attributes to clean with their corresponding cleaner

# File lib/data_cleansing/cleanse.rb, line 86
def data_cleansing_attribute_cleaners
  @data_cleansing_attribute_cleaners ||= Concurrent::Hash.new
end
data_cleansing_cleaners() click to toggle source

Array of cleaners to execute against this model and it's children

# File lib/data_cleansing/cleanse.rb, line 76
def data_cleansing_cleaners
  @data_cleansing_cleaners ||= Concurrent::Array.new
end

Private Instance Methods

data_cleansing_clean(cleaner_struct, value, binding = nil) click to toggle source

Returns the supplied value cleansed using the supplied cleaner Parameters

binding
  If supplied the cleansing will be performed within the scope of
  that binding so that cleaners can read and write to attributes
  of that binding

No logging of cleansing is performed by this method since the value itself is not modified

# File lib/data_cleansing/cleanse.rb, line 101
def data_cleansing_clean(cleaner_struct, value, binding = nil)
  return if cleaner_struct.nil? || value.nil?
  # Duplicate value in case cleaner uses methods such as gsub!
  new_value = value.is_a?(String) ? value.dup : value
  cleaner_struct.cleaners.each do |name|
    new_value = DataCleansing.clean(name, new_value, cleaner_struct.params, binding)
  end
  new_value
end