custom_validators 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ -Ispec
2
+ --color
3
+ --formatter progress
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Tarsoly András
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.rdoc ADDED
@@ -0,0 +1,74 @@
1
+ = Custom Validators
2
+
3
+ An ever growing collection of custom validators for ActiveModel 3.0.
4
+
5
+ == Preface
6
+
7
+ Rails 3.0 have a nice new way to validate instance attributes against built-in and
8
+ custom validators. For example, you could say:
9
+
10
+ class Foo << ActiveRecord::Base
11
+ validates :name, :presence => true, :uniqueness => true, :format => /(.*)/
12
+ end
13
+
14
+ This equals the following:
15
+
16
+ class Foo << ActiveRecord::Base
17
+ validates_presence_of :name
18
+ validates_uniqueness_of :name
19
+ validates_format_of :name, :with => /(.*)/
20
+ end
21
+
22
+ You can do this now without using ActiveRecord either, using only ActiveModel's validators
23
+ to validata your model's attributes:
24
+
25
+ class Foo
26
+ include ActiveModel::Validators
27
+
28
+ attr_accessor :name
29
+ validates :name, :presence => true, :uniqueness => true
30
+ end
31
+
32
+ For more information on validators in Rails 3.0, please visit this excellent post by Jamie Hill:
33
+ http://thelucid.com/2010/01/08/sexy-validation-in-edge-rails-rails-3/
34
+
35
+
36
+ == Install
37
+
38
+ Install as a gem:
39
+
40
+ # gem install custom_validators
41
+
42
+ Or as a plugin
43
+
44
+ # rails plugin install http://github.com/tarsolya/custom-validators.git
45
+
46
+
47
+ == Usage
48
+
49
+ Supported validators:
50
+
51
+ * email - Simple validator for e-mail address format, not RFC compliant, though.
52
+
53
+ # validates :contact, :email => true
54
+
55
+ * real_id - Validates attribute against the new Real ID format used on Blizzard's Battle.net 2.0
56
+
57
+ # validates :bnet_id, :real_id = true
58
+
59
+ == Requirements
60
+
61
+ * ActiveModel 3.0
62
+
63
+ == How to contribute
64
+
65
+ * Fork.
66
+ * Include your custom validator and name it properly.
67
+ * Please do add specs for it. It should be pretty simple,
68
+ just check out the already included ones.
69
+ * Commit your changes, but leave Rakefile, VERSION or HISTORY alone.
70
+ * Send me a pull request. Bonus points for topic branches.
71
+
72
+ == Copyright
73
+
74
+ Copyright (c) 2010 Tarsoly András. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,46 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rake'
4
+ require 'yaml'
5
+
6
+ require 'rake/rdoctask'
7
+ require 'rspec/core/rake_task'
8
+ require 'rspec/core/version'
9
+
10
+ begin
11
+ require 'jeweler'
12
+ Jeweler::Tasks.new do |gem|
13
+ gem.name = "custom_validators"
14
+ gem.summary = %Q{An ever growing collection for custom validators for ActiveModel 3.0}
15
+ gem.description = %Q{An ever growing collection for custom validators for ActiveModel 3.0}
16
+ gem.email = "tarsolya@gmail.com"
17
+ gem.homepage = "http://github.com/tarsolya/custom_validators"
18
+ gem.authors = ["Tarsoly András"]
19
+ gem.add_development_dependency "rspec", "~> 2.0.0"
20
+ gem.add_development_dependency "activemodel", "~> 3.0.0"
21
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
22
+ end
23
+ Jeweler::GemcutterTasks.new
24
+ rescue LoadError
25
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
26
+ end
27
+
28
+ Rspec::Core::RakeTask.new(:spec)
29
+
30
+ Rspec::Core::RakeTask.new(:rcov) do |spec|
31
+ spec.rcov = true
32
+ end
33
+
34
+ task :spec => :check_dependencies
35
+
36
+ task :default => :spec
37
+
38
+ require 'rake/rdoctask'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "custom_validators #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ #:nodoc:#
2
+ require File.join( File.dirname(__FILE__), 'lib', 'custom_validators' )
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ #:nodoc:#
@@ -0,0 +1,4 @@
1
+ #:nodoc:#
2
+
3
+ require 'custom_validators/email'
4
+ require 'custom_validators/real_id'
@@ -0,0 +1,14 @@
1
+ # Simple e-mail format validator
2
+ #
3
+ # === Usage
4
+ #
5
+ # validates :foo, :email => true
6
+ #
7
+ class EmailValidator < ActiveModel::EachValidator
8
+
9
+ def validate_each(record, attribute, value) #:nodoc:#
10
+ record.errors[attribute] << (options[:message] || "is not an email") unless
11
+ value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
12
+ end
13
+
14
+ end
@@ -0,0 +1,14 @@
1
+ # Real ID format validator for Blizzard's Battle.net 2.0
2
+ #
3
+ # === Usage
4
+ #
5
+ # validates :foo, :real_id => true
6
+ #
7
+ class RealIdValidator < ActiveModel::EachValidator
8
+
9
+ def validate_each(record, attribute, value) #:nodoc:#
10
+ record.errors[attribute] << (options[:message] || "is not a Real ID") unless
11
+ value =~ /^([a-zA-Z]{3,})\.([a-z]{3,})$/i
12
+ end
13
+
14
+ end
@@ -0,0 +1,87 @@
1
+ # encoding: utf-8
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
4
+ require 'active_model'
5
+ require 'custom_validators'
6
+
7
+ class EmailValidatorTest
8
+ include ActiveModel::Validations
9
+
10
+ attr_accessor :email, :real_id
11
+
12
+ validates :email, :email => true
13
+ end
14
+
15
+ class RealIdValidatorTest
16
+ include ActiveModel::Validations
17
+
18
+ attr_accessor :real_id
19
+
20
+ validates :real_id, :real_id => true
21
+ end
22
+
23
+ describe "Custom Email Validator" do
24
+
25
+ before :each do
26
+ @foo = EmailValidatorTest.new
27
+ end
28
+
29
+ it "should validate a proper email address" do
30
+ @foo.email = 'foo@bar.com'
31
+ @foo.valid?.should be_true
32
+ end
33
+
34
+ it "should invalidate bad email address" do
35
+ @foo.email = 'imnot@agood.email@address'
36
+ @foo.valid?.should be_false
37
+ end
38
+
39
+ end
40
+
41
+ describe "Custom Real ID Validator" do
42
+
43
+ before :each do
44
+ @foo = RealIdValidatorTest.new
45
+ end
46
+
47
+ it "should validate a proper Real ID" do
48
+ @foo.real_id = 'tarsolya.tarsolya'
49
+ @foo.valid?.should be_true
50
+ end
51
+
52
+ it "should validate a capitalized Real ID" do
53
+ @foo.real_id = 'Capitalized.butnothere'
54
+ @foo.valid?.should be_true
55
+ end
56
+
57
+ it "should validate a minimum length Real ID" do
58
+ @foo.real_id = 'foo.bar'
59
+ @foo.valid?.should be_true
60
+ end
61
+
62
+ it "should invalidate a generic bad Real ID" do
63
+ @foo.real_id = 'this.cantbe.good'
64
+ @foo.valid?.should be_false
65
+ end
66
+
67
+ it "should invalidate a Real ID without a dot" do
68
+ @foo.real_id = 'wemusthaveadot'
69
+ @foo.valid?.should be_false
70
+ end
71
+
72
+ it "should invalidate a Real ID with numbers in it" do
73
+ @foo.real_id = 'andwe88.dontwant888numbers'
74
+ @foo.valid?.should be_false
75
+ end
76
+
77
+ it "should invalidate a Real ID with accented chars in it" do
78
+ @foo.real_id = 'andáccented.chars'
79
+ @foo.valid?.should be_false
80
+ end
81
+
82
+ it "should invalidate a Real ID with too short segments" do
83
+ @foo.real_id = 'a.bc'
84
+ @foo.valid?.should be_false
85
+ end
86
+
87
+ end
@@ -0,0 +1,48 @@
1
+ $LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
2
+ require 'rspec/core'
3
+
4
+ $LOAD_PATH << File.expand_path('../../../rspec-expectations/lib', __FILE__)
5
+ $LOAD_PATH << File.expand_path('../../../rspec-mocks/lib', __FILE__)
6
+ require 'rspec/expectations'
7
+ require 'rspec/mocks'
8
+
9
+ begin
10
+ require 'autotest'
11
+ rescue LoadError
12
+ raise "You must install autotest to use it"
13
+ end
14
+
15
+ require 'autotest/rspec2'
16
+
17
+ module Rspec
18
+ module Core
19
+ module Matchers
20
+ def fail
21
+ raise_error(::Rspec::Expectations::ExpectationNotMetError)
22
+ end
23
+
24
+ def fail_with(message)
25
+ raise_error(::Rspec::Expectations::ExpectationNotMetError, message)
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ def use_formatter(new_formatter)
32
+ original_formatter = Rspec.configuration.formatter
33
+ Rspec.configuration.instance_variable_set(:@formatter, new_formatter)
34
+ yield
35
+ ensure
36
+ Rspec.configuration.instance_variable_set(:@formatter, original_formatter)
37
+ end
38
+
39
+ def in_editor?
40
+ ENV.has_key?('TM_MODE') || ENV.has_key?('EMACS') || ENV.has_key?('VIM')
41
+ end
42
+
43
+ Rspec.configure do |c|
44
+ c.color_enabled = !in_editor?
45
+ c.exclusion_filter = { :ruby => lambda {|version|
46
+ !(RUBY_VERSION.to_s =~ /^#{version.to_s}/)
47
+ }}
48
+ end
data/spec/specs.watchr ADDED
@@ -0,0 +1,58 @@
1
+ # Run me with:
2
+ #
3
+ # $ watchr specs.watchr
4
+
5
+ # --------------------------------------------------
6
+ # Convenience Methods
7
+ # --------------------------------------------------
8
+ def all_test_files
9
+ Dir['spec/**/*_spec.rb']
10
+ end
11
+
12
+ def run_test_matching(thing_to_match)
13
+ matches = all_test_files.grep(/#{thing_to_match}/i)
14
+ if matches.empty?
15
+ puts "Sorry, thanks for playing, but there were no matches for #{thing_to_match}"
16
+ else
17
+ run matches.join(' ')
18
+ end
19
+ end
20
+
21
+ def run(files_to_run)
22
+ puts("Running: #{files_to_run}")
23
+ system("clear;rspec -cfs #{files_to_run}")
24
+ no_int_for_you
25
+ end
26
+
27
+ def run_all_tests
28
+ run(all_test_files.join(' '))
29
+ end
30
+
31
+ # --------------------------------------------------
32
+ # Watchr Rules
33
+ # --------------------------------------------------
34
+ watch('^spec/(.*)_spec\.rb') { |m| run_test_matching(m[1]) }
35
+ watch('^lib/(.*)\.rb') { |m| run_test_matching(m[1]) }
36
+ watch('^spec/spec_helper\.rb') { run_all_tests }
37
+
38
+ # --------------------------------------------------
39
+ # Signal Handling
40
+ # --------------------------------------------------
41
+
42
+ def no_int_for_you
43
+ @sent_an_int = nil
44
+ end
45
+
46
+ Signal.trap 'INT' do
47
+ if @sent_an_int then
48
+ puts " A second INT? Ok, I get the message. Shutting down now."
49
+ exit
50
+ else
51
+ puts " Did you just send me an INT? Ugh. I'll quit for real if you do it again."
52
+ @sent_an_int = true
53
+ Kernel.sleep 1.5
54
+ run_all_tests
55
+ end
56
+ end
57
+
58
+ # vim:ft=ruby
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ #:nodoc:#
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: custom_validators
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
+ - "Tarsoly Andr\xC3\xA1s"
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-06 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 0
30
+ - 0
31
+ version: 2.0.0
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: activemodel
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 3
43
+ - 0
44
+ - 0
45
+ version: 3.0.0
46
+ type: :development
47
+ version_requirements: *id002
48
+ description: An ever growing collection for custom validators for ActiveModel 3.0
49
+ email: tarsolya@gmail.com
50
+ executables: []
51
+
52
+ extensions: []
53
+
54
+ extra_rdoc_files:
55
+ - LICENSE
56
+ - README.rdoc
57
+ files:
58
+ - .document
59
+ - .gitignore
60
+ - .rspec
61
+ - LICENSE
62
+ - README.rdoc
63
+ - Rakefile
64
+ - VERSION
65
+ - init.rb
66
+ - install.rb
67
+ - lib/custom_validators.rb
68
+ - lib/custom_validators/email.rb
69
+ - lib/custom_validators/real_id.rb
70
+ - spec/custom_validators_spec.rb
71
+ - spec/spec_helper.rb
72
+ - spec/specs.watchr
73
+ - uninstall.rb
74
+ has_rdoc: true
75
+ homepage: http://github.com/tarsolya/custom_validators
76
+ licenses: []
77
+
78
+ post_install_message:
79
+ rdoc_options:
80
+ - --charset=UTF-8
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ requirements: []
98
+
99
+ rubyforge_project:
100
+ rubygems_version: 1.3.6
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: An ever growing collection for custom validators for ActiveModel 3.0
104
+ test_files:
105
+ - spec/custom_validators_spec.rb
106
+ - spec/spec_helper.rb