Class: IpaddrValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/ipaddr_validator.rb,
lib/ipaddr_validator/engine.rb,
lib/ipaddr_validator/version.rb

Defined Under Namespace

Classes: Engine

Constant Summary collapse

VERSION =
'0.0.2'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_optionsObject



19
20
21
# File 'lib/ipaddr_validator.rb', line 19

def default_options
  { ipv4: true, ipv6: false, array: false }
end

.valid?(value, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/ipaddr_validator.rb', line 5

def valid?(value, options = {})
  options = default_options.merge(options)
  array =
    if options[:array]
      return false unless value.is_a?(Array)
      value
    else
      [value]
    end
  array.all? do |value|
    validate_single_ipaddr(value, options)
  end
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



45
46
47
48
49
# File 'lib/ipaddr_validator.rb', line 45

def validate_each(record, attribute, value)
  unless self.class.valid?(value, options)
    record.errors.add(attribute, options[:message] || :invalid_ipaddr)
  end
end