Module: DataCleansing::Cleanse::ClassMethods

Defined in:
lib/data_cleansing/cleanse.rb

Instance Method Summary collapse

Instance Method Details

#after_cleanse(*methods) ⇒ Object

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



33
34
35
36
37
38
# File 'lib/data_cleansing/cleanse.rb', line 33

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) ⇒ Object

Define how to cleanse one or more attributes

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/data_cleansing/cleanse.rb', line 8

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] ||= ThreadSafe::Array.new) << cleaner
  end
  cleaner
end

#cleanse_attribute(attribute_name, value, object = nil) ⇒ Object

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


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/data_cleansing/cleanse.rb', line 55

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
  cleansed_value = value.dup
  cleaners.reverse_each {|cleaner| cleansed_value = data_cleansing_clean(cleaner, cleansed_value, object) if cleaner}
  cleansed_value
end

#data_cleansing_after_cleanersObject

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



79
80
81
# File 'lib/data_cleansing/cleanse.rb', line 79

def data_cleansing_after_cleaners
  @data_cleansing_after_cleaners ||= ThreadSafe::Array.new
end

#data_cleansing_attribute_cleanersObject

Hash of attributes to clean with their corresponding cleaner



84
85
86
# File 'lib/data_cleansing/cleanse.rb', line 84

def data_cleansing_attribute_cleaners
  @data_cleansing_attribute_cleaners ||= ThreadSafe::Hash.new
end

#data_cleansing_cleanersObject

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



74
75
76
# File 'lib/data_cleansing/cleanse.rb', line 74

def data_cleansing_cleaners
  @data_cleansing_cleaners ||= ThreadSafe::Array.new
end