Module: HasNormalizedFields::ClassMethods

Defined in:
lib/has_normalized_fields.rb

Instance Method Summary collapse

Instance Method Details

#has_normalized_attributes(args = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/has_normalized_fields.rb', line 29

def has_normalized_attributes(args = {})

  if args.blank? || !args.is_a?(Hash)
    raise ArgumentError, 'Must define the fields you want to be normalize with has_normalized_attributes :field_one => "phone", :field_two => "zipcode"'
  end

  args.each do |field, normalization_type|
    define_method "#{field.to_s}=" do |value|
      if value.present?
        value.send("normalize_#{normalization_type.downcase}".to_sym)
      end
      super value
    end
  end

end