Class: PAK::ValidatesHostname::DomainnameValidator

Inherits:
HostnameValidator
  • Object
show all
Defined in:
lib/validates_hostname.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ DomainnameValidator

Returns a new instance of DomainnameValidator.



246
247
248
249
250
251
252
# File 'lib/validates_hostname.rb', line 246

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

Instance Method Details

#add_error(record, attr_name, message, *interpolators) ⇒ Object



270
271
272
273
274
275
276
# File 'lib/validates_hostname.rb', line 270

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) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/validates_hostname.rb', line 254

def validate_each(record, attribute, value)
  super

  if value.is_a?(String)
    labels = value.split '.'
    # 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