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 =
- {} 
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.default_options ⇒ Object
| 18 19 20 | # File 'lib/email_validator.rb', line 18 def self. @@default_options end | 
.regexp(options = {}) ⇒ Object
| 6 7 8 9 10 11 12 | # File 'lib/email_validator.rb', line 6 def self.regexp( = {}) = .merge() name_validation = [:strict_mode] ? "-\\p{L}\\d+._" : "^@\\s" /\A\s*([#{name_validation}]{1,64})@((?:[-\p{L}\d]+\.)+\p{L}{2,})\s*\z/i end | 
.valid?(value, options = {}) ⇒ Boolean
| 14 15 16 | # File 'lib/email_validator.rb', line 14 def self.valid?(value, = {}) !!(value =~ regexp()) end | 
Instance Method Details
#validate_each(record, attribute, value) ⇒ Object
| 22 23 24 25 26 27 28 | # File 'lib/email_validator.rb', line 22 def validate_each(record, attribute, value) = @@default_options.merge(self.) unless self.class.valid?(value, self.) record.errors.add(attribute, [:message] || :invalid) end end |