misc_validators 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Aurélien Malisart
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.
@@ -0,0 +1,34 @@
1
+ == MiscValidators
2
+
3
+ A compilation of simple validators we need in most rails apps (AR based).
4
+
5
+ == Examples
6
+
7
+ Validation of an email address:
8
+
9
+ class MyModel < ActiveRecord::Base
10
+ validates_email :my_email_field
11
+ end
12
+
13
+ Validation of an URL:
14
+
15
+ class MyModel < ActiveRecord::Base
16
+ validates_url :my_url_field
17
+ end
18
+
19
+ == Installation
20
+
21
+ As a gem:
22
+
23
+ sudo sources -a http://gems.github.com
24
+ sudo gem install aurels-misc_validators
25
+
26
+ As a plugin in your app:
27
+
28
+ script/plugin install git://github.com/aurels/misc_validators.git
29
+
30
+ If you wanna run the tests (Test::Unit):
31
+
32
+ rake test
33
+
34
+ Copyright (c) 2009 Aurélien Malisart, released under the MIT license
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rake/rdoctask'
5
+
6
+ desc 'Default: run unit tests.'
7
+ task :default => :test
8
+
9
+ desc 'Test the misc_validators plugin.'
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs << 'lib'
12
+ t.libs << 'test'
13
+ t.pattern = 'test/**/*_test.rb'
14
+ t.verbose = true
15
+ end
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'misc_validators'
2
+ ActiveRecord::Base.send(:include, ActiveRecord::MiscValidators)
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,23 @@
1
+ module ActiveRecord
2
+ module MiscValidators
3
+ def self.included(base)
4
+ base.extend(ClassMethods)
5
+ end
6
+
7
+ module ClassMethods
8
+ def validates_email(field_sym, options = {})
9
+ validates_format_of(field_sym,
10
+ options.merge(
11
+ :with => /(\A(\s*)\Z)|(\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z)/i
12
+ ))
13
+ end
14
+
15
+ def validates_url(field_sym, options = {})
16
+ validates_format_of(field_sym,
17
+ options.merge(
18
+ :with => /^(http|https):\/\/[a-z0-9]+([-.]{1}[a-z0-9]+)*.[a-z]{2,5}(:[0-9]+)?(([0-9]{1,5})?\/.*)?$/ix
19
+ ))
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,38 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_support'
4
+ require 'active_support/test_case'
5
+ require 'activerecord'
6
+ require 'init'
7
+
8
+ # ActiveRecord setup ===========================================================
9
+
10
+ ActiveRecord::Base.establish_connection({
11
+ :adapter => 'sqlite3',
12
+ :dbfile => 'test.db'
13
+ })
14
+
15
+ ActiveRecord::Schema.define do
16
+ create_table "users", :force => true do |t|
17
+ t.column "email", :string
18
+ end
19
+
20
+ create_table "bookmarks", :force => true do |t|
21
+ t.column "url", :string
22
+ end
23
+ end
24
+
25
+ # Utils models =================================================================
26
+
27
+ class User < ActiveRecord::Base
28
+ validates_email :email
29
+ end
30
+
31
+ class Bookmark < ActiveRecord::Base
32
+ validates_url :url
33
+ end
34
+
35
+ # Units tests ==================================================================
36
+
37
+ require 'unit/validates_email_test'
38
+ require 'unit/validates_url_test'
@@ -0,0 +1,25 @@
1
+ class MiscValidatorsValidatesEmailTest < ActiveSupport::TestCase
2
+ def test_truth
3
+ assert true
4
+ end
5
+
6
+ def test_good_email
7
+ assert User.new(:email => "dude.lastname@yahoo.cz").valid?
8
+ assert User.new(:email => "dude@gmail.com").valid?
9
+ assert User.new(:email => "dude@gmail.co.uk").valid?
10
+ assert User.new(:email => "Francois.Stephany@gmail.com").valid?
11
+ assert User.new(:email => "Francois.Stephay@gmail.co.uk").valid?
12
+ assert User.new(:email => "dude-blip@gmail.com").valid?
13
+ assert User.new(:email => "dude_blop@gmail.com").valid?
14
+ assert User.new(:email => "dude789@gmail.com").valid?
15
+ end
16
+
17
+ def test_wrong_email
18
+ assert !User.new(:email => "thisisnotanemail").valid?
19
+ assert !User.new(:email => "@thisisnotanemail").valid?
20
+ assert !User.new(:email => "thisisnotanemail@").valid?
21
+ assert !User.new(:email => "thisisnotanemail@gmail").valid?
22
+ assert !User.new(:email => "thisisnotanemail@hotmail@gmail.com").valid?
23
+ assert !User.new(:email => "this.is.not.anemail").valid?
24
+ end
25
+ end
@@ -0,0 +1,27 @@
1
+ class MiscValidatorsValidatesUrlTest < ActiveSupport::TestCase
2
+ def test_truth
3
+ assert true
4
+ end
5
+
6
+ def test_good_url
7
+ assert Bookmark.new(:url => "http://www.google.com").valid?
8
+ assert Bookmark.new(:url => "http://google.com").valid?
9
+ assert Bookmark.new(:url => "https://www.google.com").valid?
10
+ assert Bookmark.new(:url => "http://www.google.com/test").valid?
11
+ assert Bookmark.new(:url => "http://www.google.com/test-blop").valid?
12
+ assert Bookmark.new(:url => "http://www.google.com/~aurels").valid?
13
+ assert Bookmark.new(:url => "http://localhost").valid?
14
+ assert Bookmark.new(:url => "http://www.google.com:890/").valid?
15
+ assert Bookmark.new(:url => "http://www.google.com:80").valid?
16
+ assert Bookmark.new(:url => "http://www.google.com/blop:8080").valid?
17
+ end
18
+
19
+ def test_wrong_url
20
+ assert !Bookmark.new(:url => "www.google.com").valid?
21
+ assert !Bookmark.new(:url => "google").valid?
22
+ assert !Bookmark.new(:url => "ftp://www.google.com").valid?
23
+ assert !Bookmark.new(:url => "ftp://www.google.com").valid?
24
+ assert !Bookmark.new(:url => "http://www.google.com:").valid?
25
+ assert !Bookmark.new(:url => "http://www.goo:gle.com").valid?
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: misc_validators
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.2"
5
+ platform: ruby
6
+ authors:
7
+ - "Aur\xC3\xA9lien Malisart"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-10 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A collection of misc usefull validators for ActiveRecord::Base (email, URL)
17
+ email: aurelien.malisart@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - lib/misc_validators.rb
26
+ - test/misc_validators_test.rb
27
+ - test/unit/validates_email_test.rb
28
+ - test/unit/validates_url_test.rb
29
+ - LICENSE
30
+ - README.rdoc
31
+ - Rakefile
32
+ - install.rb
33
+ - init.rb
34
+ has_rdoc: true
35
+ homepage: http://github.com/aurels/misc_validators
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "1.2"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.3.5
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: A collection of misc usefull validators for ActiveRecord::Base
62
+ test_files: []
63
+