email_validator 1.4.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4d15cdeca6720498d1bdd8dc2d3a07db1cafb8c5
4
- data.tar.gz: bf391b40bf7e65cbd21c73e438fb30cf8d8ecac7
3
+ metadata.gz: 46498bfe9a730667a04d6defd887df5d2db1e2d9
4
+ data.tar.gz: 8d82a1910be79a0301fb8bea538e48ce39e1783e
5
5
  SHA512:
6
- metadata.gz: f6da0bd3866fb08aa5083853d8f67c392d328fa7590d641dc970025e6b5ece0d56b07be73e9edb8c9cd7d12af9c130f319e8553ffe59e46cb8c61563167cf058
7
- data.tar.gz: 295f35413dcb7b7eaef6ce56856ca8aa9651bad8c6cf65c6b3196de049630385e6cc5efdb4b49a170d203e96ad4605e4164556898e23768cf0dc6a84910b9524
6
+ metadata.gz: 5021fee86809281a8931171f82cde476dc5aff110c904c0a6d1c1aa57b8c06757dcd77215d28c5c66cd43e27da643b58e241bfb1a930da0d5110f86a976a28a5
7
+ data.tar.gz: 90b1b4a2b6b6cc9a7a80edbbf5adabd15ce0bb0218a289da766c40fe3d4ab53de192641cbaf946e769ce3dcf606ff0b7df638ac16b1e7181daf28c8e74d60847
@@ -1,8 +1,7 @@
1
1
  rvm:
2
- - 1.8.7
3
- - 1.9.2
4
2
  - 1.9.3
5
3
  - 2.0.0
6
- - jruby-18mode
4
+ - 2.1.3
5
+ - ruby-head
7
6
  - jruby-19mode
8
- - ree
7
+ - rbx-2
@@ -0,0 +1,7 @@
1
+ # HEAD
2
+
3
+ # 1.5.0
4
+
5
+ * Add a class method for simpler validation [TiteiKo and cluesque #19]
6
+ * RSpec 3.0 syntax [strivedi183 #17]
7
+ * Create Changes.md
data/README.md CHANGED
@@ -34,6 +34,23 @@ Or you can do this in a specific `validates` call:
34
34
  validates :my_email_attribute, :email => {:strict_mode => true}
35
35
  ```
36
36
 
37
+ ## Validation outside a model
38
+
39
+ If you need to validate an email outside a model, you can get the regexp :
40
+
41
+ ### Normal mode
42
+
43
+ ```ruby
44
+ EmailValidator.regexp # returns the regex
45
+ EmailValidator.valid?('narf@example.com') # boolean
46
+ ```
47
+
48
+ ### Strict mode
49
+
50
+ ```ruby
51
+ EmailValidator.regexp(:strict_mode => true)
52
+ ```
53
+
37
54
  ## Thread safety
38
55
 
39
56
  This gem is thread safe, with one caveat: `EmailValidator.default_options` must be configured before use in a multi-threaded environment. If you configure `default_options` in a Rails initializer file, then you're good to go since initializers are run before worker threads are spawned.
data/Rakefile CHANGED
@@ -1,10 +1,4 @@
1
1
  require 'bundler'
2
- require 'rake/testtask'
3
- begin
4
- require 'rdoc/task'
5
- rescue LoadError
6
- require 'rake/rdoctask' # deprecated in Ruby 1.9.3 but needed for Ruby 1.8.7 and JRuby
7
- end
8
2
  require 'rspec/core/rake_task'
9
3
 
10
4
  Bundler::GemHelper.install_tasks
@@ -13,13 +7,4 @@ RSpec::Core::RakeTask.new do |t|
13
7
  t.rspec_opts = %w(--format documentation --colour)
14
8
  end
15
9
 
16
- Rake::RDocTask.new do |rdoc|
17
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
18
-
19
- rdoc.rdoc_dir = 'rdoc'
20
- rdoc.title = "email_validator #{version}"
21
- rdoc.rdoc_files.include('README*')
22
- rdoc.rdoc_files.include('lib/**/*.rb')
23
- end
24
-
25
10
  task :default => 'spec'
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = %q{email_validator}
3
- s.version = "1.4.0"
3
+ s.version = "1.5.0"
4
4
  s.authors = ["Brian Alexander"]
5
5
  s.description = %q{An email validator for Rails 3+. See homepage for details: http://github.com/balexand/email_validator}
6
6
  s.email = %q{balexand@gmail.com}
@@ -18,5 +18,6 @@ Gem::Specification.new do |s|
18
18
 
19
19
  s.add_development_dependency("rake")
20
20
  s.add_development_dependency("rspec", ">= 0")
21
- end
22
21
 
22
+ s.add_development_dependency('rubysl', '~> 2.0') if RUBY_ENGINE == 'rbx'
23
+ end
@@ -2,14 +2,26 @@
2
2
  class EmailValidator < ActiveModel::EachValidator
3
3
  @@default_options = {}
4
4
 
5
+ def self.regexp(options = {})
6
+ options = default_options.merge(options)
7
+
8
+ name_validation = options[:strict_mode] ? "-a-z0-9+._" : "^@\\s"
9
+
10
+ /\A\s*([#{name_validation}]{1,64})@((?:[-a-z0-9]+\.)+[a-z]{2,})\s*\z/i
11
+ end
12
+
13
+ def self.valid?(value, options = {})
14
+ !!(value =~ regexp(options))
15
+ end
16
+
5
17
  def self.default_options
6
18
  @@default_options
7
19
  end
8
20
 
9
21
  def validate_each(record, attribute, value)
10
22
  options = @@default_options.merge(self.options)
11
- name_validation = options[:strict_mode] ? "-a-z0-9+._" : "^@\\s"
12
- unless value =~ /\A\s*([#{name_validation}]{1,64})@((?:[-a-z0-9]+\.)+[a-z]{2,})\s*\z/i
23
+
24
+ unless self.class.valid?(value, self.options)
13
25
  record.errors.add(attribute, options[:message] || :invalid)
14
26
  end
15
27
  end
@@ -51,11 +51,23 @@ describe EmailValidator do
51
51
  ].each do |email|
52
52
 
53
53
  it "#{email.inspect} should be valid" do
54
- TestUser.new(:email => email).should be_valid
54
+ expect(TestUser.new(:email => email)).to be_valid
55
55
  end
56
56
 
57
57
  it "#{email.inspect} should be valid in strict_mode" do
58
- StrictUser.new(:email => email).should be_valid
58
+ expect(StrictUser.new(:email => email)).to be_valid
59
+ end
60
+
61
+ it "#{email.inspect} should match the regexp" do
62
+ expect(email =~ EmailValidator.regexp).to be_truthy
63
+ end
64
+
65
+ it "#{email.inspect} should match the strict regexp" do
66
+ expect(email =~ EmailValidator.regexp(:strict_mode => true)).to be_truthy
67
+ end
68
+
69
+ it "#{email.inspect} should pass the class tester" do
70
+ expect(EmailValidator.valid?(email)).to be_truthy
59
71
  end
60
72
 
61
73
  end
@@ -89,11 +101,23 @@ describe EmailValidator do
89
101
  ].each do |email|
90
102
 
91
103
  it "#{email.inspect} should not be valid" do
92
- TestUser.new(:email => email).should_not be_valid
104
+ expect(TestUser.new(:email => email)).not_to be_valid
93
105
  end
94
106
 
95
107
  it "#{email.inspect} should not be valid in strict_mode" do
96
- StrictUser.new(:email => email).should_not be_valid
108
+ expect(StrictUser.new(:email => email)).not_to be_valid
109
+ end
110
+
111
+ it "#{email.inspect} should not match the regexp" do
112
+ expect(email =~ EmailValidator.regexp).to be_falsy
113
+ end
114
+
115
+ it "#{email.inspect} should not match the strict regexp" do
116
+ expect(email =~ EmailValidator.regexp(:strict_mode => true)).to be_falsy
117
+ end
118
+
119
+ it "#{email.inspect} should fail the class tester" do
120
+ expect(EmailValidator.valid?(email)).to be_falsy
97
121
  end
98
122
 
99
123
  end
@@ -110,11 +134,19 @@ describe EmailValidator do
110
134
  ].each do |email|
111
135
 
112
136
  it "#{email.inspect} should be valid" do
113
- TestUser.new(:email => email).should be_valid
137
+ expect(TestUser.new(:email => email)).to be_valid
114
138
  end
115
139
 
116
140
  it "#{email.inspect} should not be valid in strict_mode" do
117
- StrictUser.new(:email => email).should_not be_valid
141
+ expect(StrictUser.new(:email => email)).not_to be_valid
142
+ end
143
+
144
+ it "#{email.inspect} should match the regexp" do
145
+ expect(email =~ EmailValidator.regexp).to be_truthy
146
+ end
147
+
148
+ it "#{email.inspect} should not match the strict regexp" do
149
+ expect(email =~ EmailValidator.regexp(:strict_mode => true)).to be_falsy
118
150
  end
119
151
 
120
152
  end
@@ -127,7 +159,7 @@ describe EmailValidator do
127
159
  before { subject.valid? }
128
160
 
129
161
  it "should add the default message" do
130
- subject.errors[:email].should include "is invalid"
162
+ expect(subject.errors[:email]).to include "is invalid"
131
163
  end
132
164
  end
133
165
 
@@ -136,22 +168,22 @@ describe EmailValidator do
136
168
  before { subject.valid? }
137
169
 
138
170
  it "should add the customized message" do
139
- subject.errors[:email_address].should include "is not looking very good!"
171
+ expect(subject.errors[:email_address]).to include "is not looking very good!"
140
172
  end
141
173
  end
142
174
  end
143
175
 
144
176
  describe "nil email" do
145
177
  it "should not be valid when :allow_nil option is missing" do
146
- TestUser.new(:email => nil).should_not be_valid
178
+ expect(TestUser.new(:email => nil)).not_to be_valid
147
179
  end
148
180
 
149
181
  it "should be valid when :allow_nil options is set to true" do
150
- TestUserAllowsNil.new(:email => nil).should be_valid
182
+ expect(TestUserAllowsNil.new(:email => nil)).to be_valid
151
183
  end
152
184
 
153
185
  it "should not be valid when :allow_nil option is set to false" do
154
- TestUserAllowsNilFalse.new(:email => nil).should_not be_valid
186
+ expect(TestUserAllowsNilFalse.new(:email => nil)).not_to be_valid
155
187
  end
156
188
  end
157
189
 
@@ -160,7 +192,7 @@ describe EmailValidator do
160
192
  before { require 'email_validator/strict' }
161
193
 
162
194
  it "should validate using strict mode" do
163
- TestUser.new(:email => "&'*+-./=?^_{}~@other-valid-characters-in-local.net").should_not be_valid
195
+ expect(TestUser.new(:email => "&'*+-./=?^_{}~@other-valid-characters-in-local.net")).not_to be_valid
164
196
  end
165
197
  end
166
198
  end
metadata CHANGED
@@ -1,55 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: email_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Alexander
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-29 00:00:00.000000000 Z
11
+ date: 2014-12-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  description: 'An email validator for Rails 3+. See homepage for details: http://github.com/balexand/email_validator'
@@ -59,9 +59,10 @@ extensions: []
59
59
  extra_rdoc_files:
60
60
  - LICENSE
61
61
  files:
62
- - .document
63
- - .gitignore
64
- - .travis.yml
62
+ - ".document"
63
+ - ".gitignore"
64
+ - ".travis.yml"
65
+ - Changes.md
65
66
  - Gemfile
66
67
  - LICENSE
67
68
  - README.md
@@ -80,17 +81,17 @@ require_paths:
80
81
  - lib
81
82
  required_ruby_version: !ruby/object:Gem::Requirement
82
83
  requirements:
83
- - - '>='
84
+ - - ">="
84
85
  - !ruby/object:Gem::Version
85
86
  version: '0'
86
87
  required_rubygems_version: !ruby/object:Gem::Requirement
87
88
  requirements:
88
- - - '>='
89
+ - - ">="
89
90
  - !ruby/object:Gem::Version
90
91
  version: '0'
91
92
  requirements: []
92
93
  rubyforge_project:
93
- rubygems_version: 2.0.0
94
+ rubygems_version: 2.4.4
94
95
  signing_key:
95
96
  specification_version: 4
96
97
  summary: An email validator for Rails 3+.