lib/email_parser/validator.rb in email_parser-0.1.1 vs lib/email_parser/validator.rb in email_parser-0.1.2
- old
+ new
@@ -1,26 +1,25 @@
require "active_model"
require "email_parser"
class EmailValidator < ActiveModel::EachValidator
- attr_reader :allow_nil
-
class << self
attr_accessor :default_parser_options
end
self.default_parser_options = {}
def initialize(*_args)
super
- parser_options = options.dup
- @allow_nil = parser_options.delete(:allow_nil)
+ 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
def validate_each(record, attribute, value)
if value.nil?
- return if allow_nil
+ return if options[:allow_nil]
record.errors.add(attribute, :blank)
return
end