Sha256: 328117239d9ea6f01cad0e8a067b2e2adaa896faf96154d6c8736d0728d479be

Contents?: true

Size: 817 Bytes

Versions: 1

Compression:

Stored size: 817 Bytes

Contents

require 'mail'

module ActiveModel
  module Validations
    class EmailValidator < ActiveModel::EachValidator
      def initialize(options)
        super(options.reverse_merge(allow_blank: false))
      end

      def validate_each(record, attr_name, value)
        unless value
          record.errors.add(attr_name, :not_a_email, options) unless options[:allow_blank]
          return
        end

        record.errors.add(attr_name, :not_a_email, options) unless valid_email(value)
      end

      private
      def valid_email( value )
        begin
          return false if value == ''
          parsed = Mail::Address.new( value )
          return parsed.address == value && parsed.local != parsed.address
        rescue Mail::Field::ParseError
          return false
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
glamorous_validation_jp-0.0.1 lib/glamorous_validation_jp/email_validator.rb