kebab-project/kangal

View on GitHub
lib/kangal/email.rb

Summary

Maintainability
A
55 mins
Test Coverage
require 'active_model'
require 'active_model/validations'
require 'mail'

class EmailValidator < ActiveModel::EachValidator

  def validate_each(object, attribute, value)

    return if options[:allow_nil] && value.nil?
    return if options[:allow_blank] && value.blank?

    begin
      m = Mail::Address.new(value)
      r = m.domain && m.address == value
      t = m.__send__(:tree)
      r &&= (t.domain.dot_atom_text.elements.size > 1)
    rescue Exception => e
      r = false
    end
    object.errors.add attribute, (options[:message] || I18n.t(:invalid, :scope => 'kangal.validations.email')) unless r
  end

end