Class: IpValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- IpValidator
- Defined in:
- lib/rails_ip_validator.rb
Overview
Validator for email
Instance Method Summary collapse
- #forbidden ⇒ Object
- #forbidden?(key) ⇒ Boolean
-
#validate_each(record, attribute, value) ⇒ Object
main validator for email.
Instance Method Details
#forbidden ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/rails_ip_validator.rb', line 9 def forbidden unless @forbidden.is_a? Array if [:forbidden].nil? # default @forbidden = [] else @forbidden = [:forbidden].is_a?(Array) ? [:forbidden] : [[:forbidden]] end end @forbidden end |
#forbidden?(key) ⇒ Boolean
21 22 23 |
# File 'lib/rails_ip_validator.rb', line 21 def forbidden? key forbidden.include? key end |
#validate_each(record, attribute, value) ⇒ Object
main validator for email
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/rails_ip_validator.rb', line 26 def validate_each(record, attribute, value) unless value.blank? # pre var valid = true begin ip = IPAddress.parse(value.to_s) valid = false if forbidden? :netmask and value =~ /\// valid = false if forbidden? :a and ip.a? valid = false if forbidden? :b and ip.b? valid = false if forbidden? :c and ip.c? valid = false if forbidden? :private and ip.private? valid = false if forbidden? :ipv4 and ip.class == IPAddress::IPv4 valid = false if forbidden? :ipv6 and (ip.class == IPAddress::IPv6 or ip.class == IPAddress::IPv6::Mapped) if [:custom].is_a? Proc valid = false unless [:custom].call(ip) end rescue valid = false end # ip valid record.errors.add(attribute, :invalid) unless valid end end |