class PAK::ValidatesHostname::DomainnameValidator

Public Class Methods

new(options) click to toggle source
# File lib/validates_hostname.rb, line 246
def initialize(options)
  opts = {
    :require_valid_tld       => true,
    :allow_numeric_hostname  => true
  }.merge(options)
  super(opts)
end

Public Instance Methods

add_error(record, attr_name, message, *interpolators) click to toggle source
# File lib/validates_hostname.rb, line 272
def add_error(record, attr_name, message, *interpolators)
  args = {
    :default => [DEFAULT_ERROR_MSG[message], options[:message]],
    :scope   => [:errors, :messages]
  }.merge(interpolators.last.is_a?(Hash) ? interpolators.pop : {})
  record.errors.add(attr_name, I18n.t( message, args ))
end
validate_each(record, attribute, value) click to toggle source
# File lib/validates_hostname.rb, line 254
def validate_each(record, attribute, value)
  super

  if value.is_a?(String)
    labels = value.split '.'
    labels.each do |label|
      # CHECK 1: if there is only one label it cannot be numeric even
      #          though numeric hostnames are allowed
      if options[:allow_numeric_hostname] == true
        is_numeric_only = labels[0] =~ /\A\d+\z/
        if is_numeric_only and labels.size == 1
          add_error(record, attribute, :single_numeric_hostname_label)
        end
      end
    end
  end
end