Sha256: 210d5e223928acdeb65fa7597a0591a313a2cd863a7bea6e88361ef1e42b7387

Contents?: true

Size: 690 Bytes

Versions: 3

Compression:

Stored size: 690 Bytes

Contents

class DomainNameValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    key = :"activerecord.errors.models.#{record.class.name.downcase}.attributes.#{attribute.to_s}.invalid"
    record.errors[attribute] << I18n.t(key, :default => :"activerecord.errors.messages.invalid") unless ValidatesHost::DomainName.new(value).valid?
  end
end

module ValidatesHost
  class DomainName
    def initialize(domain_name)
      @domain_name = domain_name
    end

    def valid?
      return true if @domain_name.nil?
      @domain_name =~ /^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/
    end

    def domain_name
      @domain_name
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
validates_host-0.3.1 lib/validates_host/domain_name_validator.rb
validates_host-0.3.0 lib/validates_host/domain_name_validator.rb
validates_host-0.2.0 lib/validates_host/domain_name_validator.rb