Class: IpAddressValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/ip_as_int/ip_address_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attr, value) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# File 'lib/ip_as_int/ip_address_validator.rb', line 3

def validate_each(record, attr, value)
  invalid = instance_variable_get("@#{attr}_invalid")
  invalid = begin
    ::IpAsInt.ip2a(value)
    false
  rescue
    true
  end if invalid.nil?
  record.errors.add(attr,"has to be a valid IP address") if invalid
end