class EmailAddressValidator
Public Instance Methods
validate_each(record, attribute, value)
click to toggle source
# File lib/validator/email_address_validator.rb, line 5 def validate_each(record, attribute, value) rules = Array(options[:with] || []) if rules.empty? # We are using the old syntax, assume regular expressions rules = [build_rule_from_format(options[:format])] end invalidate(record, attribute) unless all_rules_pass?(rules, value) end
Private Instance Methods
all_rules_pass?(rules, address)
click to toggle source
# File lib/validator/email_address_validator.rb, line 17 def all_rules_pass?(rules, address) evaluators = rules.map { |rule| build_evaluator(rule) } results = evaluators.map { |evaluator| evaluator.call(address) } results.all? end
build_evaluator(rule)
click to toggle source
Returns an evaluator for the given rule. Evaluators are objects that respond to `#call` with an arity of 1; the raw attribute value in question.
The return-value of `#call` should be `true` if the address passes the given rule, false otherwise.
# File lib/validator/email_address_validator.rb, line 28 def build_evaluator(rule) case rule when Regexp proc { |a| a =~ rule } else rule end end
build_rule_from_format(format)
click to toggle source
# File lib/validator/email_address_validator.rb, line 37 def build_rule_from_format(format) proc { |attribute_value| address = ActiveModelEmailAddressValidator::EmailAddress.new(attribute_value) address.valid?(format) } end
invalidate(record, attribute)
click to toggle source
# File lib/validator/email_address_validator.rb, line 45 def invalidate(record, attribute) record.errors.add(attribute, :invalid) end