Class: EmailValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- EmailValidator
- Defined in:
- lib/email_validator.rb
Overview
Based on work from thelucid.com/2010/01/08/sexy-validation-in-edge-rails-rails-3/
Constant Summary collapse
- @@default_options =
rubocop:disable Style/ClassVars
{ :allow_nil => false, :domain => nil, :require_fqdn => nil, :mode => :loose }
Class Method Summary collapse
- .default_options ⇒ Object
- .invalid?(value, options = {}) ⇒ Boolean
-
.regexp(options = {}) ⇒ Object
Refs: tools.ietf.org/html/rfc2822 : 3.2.
- .valid?(value, options = {}) ⇒ Boolean
Instance Method Summary collapse
Class Method Details
.default_options ⇒ Object
13 14 15 |
# File 'lib/email_validator.rb', line 13 def @@default_options end |
.invalid?(value, options = {}) ⇒ Boolean
24 25 26 |
# File 'lib/email_validator.rb', line 24 def invalid?(value, = {}) !valid?(value, ) end |
.regexp(options = {}) ⇒ Object
Refs:
https://tools.ietf.org/html/rfc2822 : 3.2. Lexical Tokens, 3.4.1. Addr-spec specification
https://tools.ietf.org/html/rfc5321 : 4.1.2. Command Argument Syntax
31 32 33 34 35 36 37 38 |
# File 'lib/email_validator.rb', line 31 def regexp( = {}) = () if [:mode] == :loose return /\A[^\s]+@[^\s]+\z/ if [:domain].nil? return /\A[^\s]+@#{domain_pattern()}\z/ end /\A(?>#{local_part_pattern})@#{domain_pattern()}\z/i end |
.valid?(value, options = {}) ⇒ Boolean
17 18 19 20 21 22 |
# File 'lib/email_validator.rb', line 17 def valid?(value, = {}) = () return true if value.nil? && [:allow_nil] == true return false if value.nil? !!(value =~ regexp()) end |
Instance Method Details
#validate_each(record, attribute, value) ⇒ Object
97 98 99 100 |
# File 'lib/email_validator.rb', line 97 def validate_each(record, attribute, value) = @@default_options.merge(self.) record.errors.add(attribute, [:message] || :invalid) unless self.class.valid?(value, ) end |