Module: ActiveRecord::Validations::ClassMethods

Defined in:
lib/validates_as_postal_code.rb

Instance Method Summary collapse

Instance Method Details

#validates_as_postal_code(*attr_names) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/validates_as_postal_code.rb', line 11

def validates_as_postal_code(*attr_names)
  regExpStr = '\d{5}(-\d{4})?'
  configuration = {
    :message   => 'is an invalid postal code',
    :with      => /^#{regExpStr}$/,
    :allow_nil => false }
  
  configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
  
  configuration[:with] = /^(#{regExpStr})?$/ if configuration[:allow_nil]

  validates_format_of attr_names, configuration
end