class ActiveModel::Validations::IpValidator

Public Instance Methods

check_validity!() click to toggle source
# File lib/active_validators/active_model/validations/ip_validator.rb, line 9
def check_validity!
  raise ArgumentError, "Unknown IP validator format #{options[:format].inspect}" unless [:v4, :v6].include? options[:format]
end
validate_each(record, attribute, value) click to toggle source
# File lib/active_validators/active_model/validations/ip_validator.rb, line 4
def validate_each(record, attribute, value)
  value_str = value.to_s # might be an IPAddr
  record.errors.add(attribute) if value_str.blank? || !regex.match(value_str)
end

Private Instance Methods

ipv4_regex() click to toggle source
# File lib/active_validators/active_model/validations/ip_validator.rb, line 23
def ipv4_regex
  # Extracted from ruby 1.9.2
  regex256 = 
    /0
    |1(?:[0-9][0-9]?)?
    |2(?:[0-4][0-9]?|5[0-5]?|[6-9])?
    |[3-9][0-9]?/x
  /\A(#{regex256})\.(#{regex256})\.(#{regex256})\.(#{regex256})\z/
end
ipv6_regex() click to toggle source
# File lib/active_validators/active_model/validations/ip_validator.rb, line 33
def ipv6_regex
  require 'resolv'
  Resolv::IPv6::Regex
end
regex() click to toggle source
# File lib/active_validators/active_model/validations/ip_validator.rb, line 14
def regex
  case options[:format]
  when :v4
    ipv4_regex
  when :v6
    ipv6_regex
  end
end