/lib/fail_fast/_commands/has_email.rb

https://github.com/alainravet/fail_fast · Ruby · 36 lines · 26 code · 7 blank · 3 comment · 1 complexity · 672a549c03d5ba51e14c47cf665dd89c MD5 · raw file

  1. class FailFast
  2. module HasEmail
  3. # Usage
  4. # has_email_for 'test/admin_email'
  5. #
  6. def has_email_for(raw_key, *params)
  7. p = key_value_regexp_options(raw_key, params)
  8. key, options = p.key, p.options
  9. return false unless has_value_for raw_key, :message => options[:message]
  10. value = value_for_deep_key(key)
  11. failure = EmailValidator.invalid_email_address?(value)
  12. if failure
  13. add_error ErrorDetails.new(key, :not_an_email, value, options[:message])
  14. end
  15. !failure
  16. end
  17. end
  18. module EmailValidator #:nodoc:
  19. VALID_EMAIL_ADDRESS_REGEXP = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/
  20. def self.valid_email_address?(email)
  21. email.strip!
  22. !!(email =~ VALID_EMAIL_ADDRESS_REGEXP)
  23. end
  24. def self.invalid_email_address?(email)
  25. !valid_email_address?(email)
  26. end
  27. end
  28. end
  29. FailFast.send :include, FailFast::HasEmail