Class: DomainnameValidator

Inherits:
HostnameValidator show all
Defined in:
lib/valid_hostname/domainname_validator.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ DomainnameValidator

Returns a new instance of DomainnameValidator.



6
7
8
9
# File 'lib/valid_hostname/domainname_validator.rb', line 6

def initialize(options)
  super({ require_valid_tld: true,
          allow_numeric_hostname: true }.merge(options))
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/valid_hostname/domainname_validator.rb', line 11

def validate_each(record, attribute, value)
  super
  return unless value.is_a? String
  return if options[:allow_numeric_hostname] != true

  labels = value.split '.'
  is_numeric_only = labels[0] =~ /\A\d+\z/

  add_error record, attribute, :single_numeric_hostname_label if is_numeric_only && labels.size == 1
end