activevalidators 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.bundle/config ADDED
@@ -0,0 +1,2 @@
1
+ ---
2
+ BUNDLE_DISABLE_SHARED_GEMS: "1"
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,56 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ActiveValidators (1.0.0)
5
+ activemodel (>= 3.0.0)
6
+ activerecord (>= 3.0.0)
7
+ mail
8
+
9
+ GEM
10
+ remote: http://rubygems.org/
11
+ specs:
12
+ activemodel (3.0.3)
13
+ activesupport (= 3.0.3)
14
+ builder (~> 2.1.2)
15
+ i18n (~> 0.4)
16
+ activerecord (3.0.3)
17
+ activemodel (= 3.0.3)
18
+ activesupport (= 3.0.3)
19
+ arel (~> 2.0.2)
20
+ tzinfo (~> 0.3.23)
21
+ activesupport (3.0.3)
22
+ arel (2.0.4)
23
+ builder (2.1.2)
24
+ diff-lcs (1.1.2)
25
+ i18n (0.4.2)
26
+ mail (2.2.10)
27
+ activesupport (>= 2.3.6)
28
+ i18n (~> 0.4.1)
29
+ mime-types (~> 1.16)
30
+ treetop (~> 1.4.8)
31
+ mime-types (1.16)
32
+ polyglot (0.3.1)
33
+ rspec (2.1.0)
34
+ rspec-core (~> 2.1.0)
35
+ rspec-expectations (~> 2.1.0)
36
+ rspec-mocks (~> 2.1.0)
37
+ rspec-core (2.1.0)
38
+ rspec-expectations (2.1.0)
39
+ diff-lcs (~> 1.1.2)
40
+ rspec-mocks (2.1.0)
41
+ treetop (1.4.9)
42
+ polyglot (>= 0.3.1)
43
+ tzinfo (0.3.23)
44
+
45
+ PLATFORMS
46
+ ruby
47
+
48
+ DEPENDENCIES
49
+ ActiveValidators!
50
+ activemodel (>= 3.0.0)
51
+ activerecord (>= 3.0.0)
52
+ bundler
53
+ mail
54
+ rspec
55
+ rspec-core
56
+ rspec-expectations
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Franck Verrot
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ ActiveValidators
2
+ ================
3
+
4
+ Collection of ActiveModel/ActiveRecord validations
5
+
6
+ Installation (Rails 3)
7
+ ----------------------
8
+
9
+ In your Gemfile:
10
+
11
+ gem 'activevalidators', :require => 'active_validators'
12
+
13
+
14
+ In your models, the gem provides new validators like `email`, or `url`:
15
+
16
+ class User
17
+ validates :email_address, :email => true
18
+ validates :link_url, :url => true
19
+ end
20
+
21
+ Exhaustive list of supported validators:
22
+
23
+ * `email` : checks the email based on the `mail` gem
24
+ * `url` : checks the url based on a regular expression
25
+
26
+
27
+ Todo
28
+ ----
29
+
30
+ Lots of improvements can be made:
31
+
32
+ * Add I18n of error messages
33
+ * Implement new validators
34
+ * ...
35
+
36
+ Note on Patches/Pull Requests
37
+ -----------------------------
38
+
39
+ * Fork the project.
40
+ * Make your feature addition or bug fix.
41
+ * Add tests for it. This is important so I don't break it in a
42
+ future version unintentionally.
43
+ * Commit, do not mess with rakefile, version, or history.
44
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
45
+ * Send me a pull request. Bonus points for topic branches.
46
+
47
+ Copyright
48
+ ---------
49
+
50
+ Copyright (c) 2010 Franck Verrot. MIT LICENSE. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,62 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.unshift File.expand_path("../lib", __FILE__)
3
+
4
+ require 'rubygems'
5
+ require 'rubygems/specification'
6
+ require 'active_validators'
7
+
8
+ def gemspec
9
+ @gemspec ||= begin
10
+ file = File.expand_path('../activevalidators.gemspec', __FILE__)
11
+ eval(File.read(file), binding, file)
12
+ end
13
+ end
14
+
15
+ begin
16
+ require 'rspec/core/rake_task'
17
+
18
+ desc "Run specs"
19
+ RSpec::Core::RakeTask.new do |t|
20
+ t.rspec_opts = %w(-fs --color)
21
+ t.ruby_opts = %w(-w)
22
+ end
23
+
24
+ namespace :spec do
25
+ task :clean do
26
+ rm_rf 'tmp'
27
+ rm_rf 'pkg'
28
+ end
29
+
30
+ desc "Run the full spec suite"
31
+ task :full => ["clean", "spec"]
32
+ end
33
+
34
+ rescue LoadError
35
+ task :spec do
36
+ abort "Run `gem install rspec` to be able to run specs"
37
+ end
38
+ end
39
+
40
+ desc "install the gem locally"
41
+ task :install => :package do
42
+ sh %{gem install pkg/#{gemspec.name}-#{gemspec.version}}
43
+ end
44
+
45
+ desc "validate the gemspec"
46
+ task :gemspec do
47
+ gemspec.validate
48
+ end
49
+
50
+ desc "Build the gem"
51
+ task :gem => [:gemspec, :build] do
52
+ mkdir_p "pkg"
53
+ sh "gem build activevalidators.gemspec"
54
+ mv "#{gemspec.full_name}.gem", "pkg"
55
+ end
56
+
57
+ desc "Install ActiveValidators"
58
+ task :install => :gem do
59
+ sh "gem install pkg/#{gemspec.full_name}.gem"
60
+ end
61
+
62
+ task :default => :spec
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib/', __FILE__)
3
+ $:.unshift lib unless $:.include?(lib)
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "activevalidators"
7
+ s.version = '1.0.0'
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Franck Verrot"]
10
+ s.email = ["franck@verrot.fr"]
11
+ s.homepage = "http://github.com/cesario/activevalidators"
12
+ s.summary = %q{Collection of ActiveModel/ActiveRecord validations}
13
+ s.description = %q{ActiveValidators is a collection of ActiveModel/ActiveRecord validations}
14
+
15
+ s.add_development_dependency "bundler"
16
+ s.add_development_dependency "rspec"
17
+ s.add_development_dependency "rspec-core"
18
+ s.add_development_dependency "rspec-expectations"
19
+ s.add_dependency 'activerecord', '>= 3.0.0'
20
+ s.add_dependency 'activemodel', '>= 3.0.0'
21
+ s.add_dependency 'mail'
22
+
23
+
24
+ s.files = `git ls-files`.split("\n")
25
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
26
+ s.require_paths = ["lib"]
27
+ end
28
+
@@ -0,0 +1 @@
1
+ Autotest.add_discovery { "rspec2" }
@@ -0,0 +1,18 @@
1
+ require 'mail'
2
+ module ActiveModel
3
+ module Validations
4
+ class EmailValidator < EachValidator
5
+ def validate_each(record,attribute,value)
6
+ begin
7
+ m = Mail::Address.new(value)
8
+ r = m.domain && m.address == value
9
+ t = m.__send__(:tree)
10
+ r &&= (t.domain.dot_atom_text.elements.size > 1)
11
+ rescue Exception => e
12
+ r = false
13
+ end
14
+ record.errors[attribute] << (options[:message] || "is invalid") unless r
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,14 @@
1
+ module ActiveModel
2
+ module Validations
3
+ class RespondToValidator < EachValidator
4
+ RESERVED_OPTIONS = [:if, :unless]
5
+ def validate_each(record,attribute,value)
6
+ responders = options.dup
7
+ RESERVED_OPTIONS.each do |opt,should_apply| responders.delete(opt) end
8
+ responders.each do |method,dummy|
9
+ record.errors[attribute] << (options[:message] || "is invalid") unless value.respond_to? method
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ module ActiveModel
2
+ module Validations
3
+ class UrlValidator < EachValidator
4
+ def validate_each(record, attribute, value)
5
+ unless value =~ /^https?:\/\/(?i)[a-z0-9]+([-.]{1}[a-z0-9]+)*.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/
6
+ record.errors[attribute] << (options[:message] || "is invalid")
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ require 'active_support'
2
+ require 'active_model'
3
+ require 'active_record'
4
+ require 'active_support/all'
5
+ require 'active_model/validations'
6
+ #Eager autoload the library's validators into AR::Validations
7
+ module ActiveModel
8
+ module Validations
9
+ extend ActiveSupport::Autoload
10
+ autoload :EmailValidator
11
+ autoload :UrlValidator
12
+ autoload :RespondToValidator
13
+ end
14
+ end
@@ -0,0 +1,7 @@
1
+ module Models
2
+ class EmailValidatorModel
3
+ include ActiveModel::Validations
4
+ attr_accessor :email
5
+ validates :email, :email => true
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Models
2
+ class RespondToValidatorModel
3
+ include ActiveModel::Validations
4
+ attr_accessor :responder, :global_condition, :local_condition
5
+ validates :responder, :respond_to => { :call => true, :if => :local_condition }, :if => :global_condition
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Models
2
+ class UrlValidatorModel
3
+ include ActiveModel::Validations
4
+ attr_accessor :url
5
+ validates :url, :url => true
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ require 'active_validators'
2
+
3
+ %w(models).each do |directory|
4
+ Dir["#{File.dirname(__FILE__)}/#{directory}/*.rb"].each {|f| require f}
5
+ end
6
+
7
+ RSpec.configure do |config|
8
+ end
@@ -0,0 +1,17 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
2
+
3
+ describe "Email Validation" do
4
+ it "accepts valid emails" do
5
+ model = Models::EmailValidatorModel.new
6
+ model.email = 'franck@verrot.fr'
7
+ model.valid?.should == true
8
+ model.should have(0).errors
9
+ end
10
+
11
+ it "rejected invalid emails" do
12
+ model = Models::EmailValidatorModel.new
13
+ model.email = 'franck.fr'
14
+ model.valid?.should == false
15
+ model.should have(1).errors
16
+ end
17
+ end
@@ -0,0 +1,23 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
2
+
3
+ describe "Respond To Validation" do
4
+ it "respond_to?" do
5
+ model = Models::RespondToValidatorModel.new
6
+ model.responder = lambda {}
7
+ model.global_condition = true
8
+ model.local_condition = true
9
+
10
+ model.valid?.should == true
11
+ model.should have(0).errors
12
+ end
13
+
14
+ it "does not respond_to?" do
15
+ model = Models::RespondToValidatorModel.new
16
+ model.responder = 42
17
+ model.global_condition = true
18
+ model.local_condition = true
19
+
20
+ model.valid?.should == false
21
+ model.should have(1).errors
22
+ end
23
+ end
@@ -0,0 +1,17 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
2
+
3
+ describe "Url Validation" do
4
+ it "accepts valid urls" do
5
+ model = Models::UrlValidatorModel.new
6
+ model.url = 'http://www.verrot.fr'
7
+ model.valid?.should be(true)
8
+ model.should have(0).errors
9
+ end
10
+
11
+ it "rejected invalid urls" do
12
+ model = Models::UrlValidatorModel.new
13
+ model.url = 'http://^^^^.fr'
14
+ model.valid?.should be(false)
15
+ model.should have(1).errors
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,184 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activevalidators
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Franck Verrot
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-11-28 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: bundler
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :development
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: rspec
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :development
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec-core
48
+ prerelease: false
49
+ requirement: &id003 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ type: :development
58
+ version_requirements: *id003
59
+ - !ruby/object:Gem::Dependency
60
+ name: rspec-expectations
61
+ prerelease: false
62
+ requirement: &id004 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ type: :development
71
+ version_requirements: *id004
72
+ - !ruby/object:Gem::Dependency
73
+ name: activerecord
74
+ prerelease: false
75
+ requirement: &id005 !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ segments:
81
+ - 3
82
+ - 0
83
+ - 0
84
+ version: 3.0.0
85
+ type: :runtime
86
+ version_requirements: *id005
87
+ - !ruby/object:Gem::Dependency
88
+ name: activemodel
89
+ prerelease: false
90
+ requirement: &id006 !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ segments:
96
+ - 3
97
+ - 0
98
+ - 0
99
+ version: 3.0.0
100
+ type: :runtime
101
+ version_requirements: *id006
102
+ - !ruby/object:Gem::Dependency
103
+ name: mail
104
+ prerelease: false
105
+ requirement: &id007 !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ segments:
111
+ - 0
112
+ version: "0"
113
+ type: :runtime
114
+ version_requirements: *id007
115
+ description: ActiveValidators is a collection of ActiveModel/ActiveRecord validations
116
+ email:
117
+ - franck@verrot.fr
118
+ executables: []
119
+
120
+ extensions: []
121
+
122
+ extra_rdoc_files: []
123
+
124
+ files:
125
+ - .bundle/config
126
+ - .gitignore
127
+ - Gemfile
128
+ - Gemfile.lock
129
+ - LICENSE
130
+ - README.md
131
+ - Rakefile
132
+ - activevalidators.gemspec
133
+ - autotest/discover.rb
134
+ - lib/active_model/validations/email_validator.rb
135
+ - lib/active_model/validations/respond_to_validator.rb
136
+ - lib/active_model/validations/url_validator.rb
137
+ - lib/active_validators.rb
138
+ - spec/models/email_validator_model.rb
139
+ - spec/models/respond_to_validator_model.rb
140
+ - spec/models/url_validator_model.rb
141
+ - spec/spec_helper.rb
142
+ - spec/specs/email_spec.rb
143
+ - spec/specs/respond_to_spec.rb
144
+ - spec/specs/url_spec.rb
145
+ has_rdoc: true
146
+ homepage: http://github.com/cesario/activevalidators
147
+ licenses: []
148
+
149
+ post_install_message:
150
+ rdoc_options: []
151
+
152
+ require_paths:
153
+ - lib
154
+ required_ruby_version: !ruby/object:Gem::Requirement
155
+ none: false
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ segments:
160
+ - 0
161
+ version: "0"
162
+ required_rubygems_version: !ruby/object:Gem::Requirement
163
+ none: false
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ segments:
168
+ - 0
169
+ version: "0"
170
+ requirements: []
171
+
172
+ rubyforge_project:
173
+ rubygems_version: 1.3.7
174
+ signing_key:
175
+ specification_version: 3
176
+ summary: Collection of ActiveModel/ActiveRecord validations
177
+ test_files:
178
+ - spec/models/email_validator_model.rb
179
+ - spec/models/respond_to_validator_model.rb
180
+ - spec/models/url_validator_model.rb
181
+ - spec/spec_helper.rb
182
+ - spec/specs/email_spec.rb
183
+ - spec/specs/respond_to_spec.rb
184
+ - spec/specs/url_spec.rb