Class: EmailValidator

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

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*_args) ⇒ EmailValidator

Returns a new instance of EmailValidator.



11
12
13
14
15
16
17
# File 'lib/email_parser/validator.rb', line 11

def initialize(*_args)
  super
  parser_options = options.each_with_object({}) do |(k, v), h|
    h[k] = v if EmailParser::OPTIONS.include?(k)
  end
  @parser = EmailParser.new(**self.class.default_parser_options.merge(parser_options))
end

Class Attribute Details

.default_parser_optionsObject

Returns the value of attribute default_parser_options.



6
7
8
# File 'lib/email_parser/validator.rb', line 6

def default_parser_options
  @default_parser_options
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/email_parser/validator.rb', line 19

def validate_each(record, attribute, value)
  if value.nil?
    return if options[:allow_nil]

    record.errors.add(attribute, :blank)
    return
  end

  return if @parser.valid?(value)

  record.errors.add(attribute, :invalid)
end