Module: HasNormalizedAttributes::ActiveRecord::ClassMethods

Defined in:
lib/has_normalized_attributes.rb

Instance Method Summary collapse

Instance Method Details

#has_normalized_attributes(args = {}) ⇒ Object



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

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?
        normalized_value = value.send("normalize_#{normalization_type.downcase}".to_sym)
      else
        normalized_value = value
      end
      super normalized_value
    end
  end

end