Class: ActiveModel::Validations::LicensePlateValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/license_plate_validator/active_model.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/license_plate_validator/active_model.rb', line 7

def validate_each(record, attribute, value)
  country = case(options[:country])
            when Proc then options[:country].call(record)
            when nil then nil
            else options[:country].to_sym
            end

  license_plate = ::LicensePlateValidator.new(value.to_s, { country: country })

  if !license_plate.valid?
    record.errors.add(attribute, :invalid_license_plate, message: options[:message])
  end
end