Module: Normatron::Extensions::ActiveRecord::InstanceMethods

Defined in:
lib/normatron/extensions/active_record.rb

Instance Method Summary collapse

Instance Method Details

#apply_normalizationsObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/normatron/extensions/active_record.rb', line 49

def apply_normalizations
  return unless self.class.normalize_rules
  
  listed_filters = Normatron.configuration.filters

  self.class.normalize_rules.each do |attribute, filters|
    value = send("#{attribute}_before_type_cast") || send(attribute)

    filters.each do |filter, args|
      if self.respond_to? filter
        value = send(filter, value, *args)
      elsif listed_filters[filter].kind_of? Module
        value = listed_filters[filter].call(value, *args)
      elsif listed_filters[filter].kind_of? Proc
        value = listed_filters[filter].call(value, *args)
      else
        raise UnknownFilterError
      end
    end

    write_attribute attribute, value
  end
end