Class: EmailValidator

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

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ EmailValidator

Returns a new instance of EmailValidator.



2
3
4
5
6
# File 'lib/louche/validators/email_validator.rb', line 2

def initialize(options)
  options.reverse_merge!(message: :invalid_email)
  options.reverse_merge!(regex: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i)
  super
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



8
9
10
11
12
# File 'lib/louche/validators/email_validator.rb', line 8

def validate_each(record, attribute, value)
  unless value =~ options.fetch(:regex)
    record.errors.add(attribute, options.fetch(:message), value: value)
  end
end