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



34
35
36
37
38
39
40
# 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) ⇒ Object

Define how to cleanse one or more attributes

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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) ⇒ 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


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

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_cleanersObject

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



82
83
84
# File 'lib/data_cleansing/cleanse.rb', line 82

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

#data_cleansing_attribute_cleanersObject

Hash of attributes to clean with their corresponding cleaner



87
88
89
# File 'lib/data_cleansing/cleanse.rb', line 87

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

#data_cleansing_cleanersObject

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



77
78
79
# File 'lib/data_cleansing/cleanse.rb', line 77

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