Class: EmailValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/email_validator.rb

Overview

Constant Summary collapse

@@default_options =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_optionsObject



18
19
20
# File 'lib/email_validator.rb', line 18

def self.default_options
  @@default_options
end

.regexp(options = {}) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/email_validator.rb', line 6

def self.regexp(options = {})
  options = default_options.merge(options)

  name_validation = options[:strict_mode] ? "-\\p{L}\\d+._" : "^@\\s"

  /\A\s*([#{name_validation}]{1,64})@((?:[-\p{L}\d]+\.)+\p{L}{2,})\s*\z/i
end

.valid?(value, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/email_validator.rb', line 14

def self.valid?(value, options = {})
  !!(value =~ regexp(options))
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/email_validator.rb', line 22

def validate_each(record, attribute, value)
  options = @@default_options.merge(self.options)

  unless self.class.valid?(value, self.options)
    record.errors.add(attribute, options[:message] || :invalid)
  end
end