jnunemaker-validatable 1.8.0 → 1.8.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -3,12 +3,13 @@ require 'rake'
3
3
 
4
4
  require 'jeweler'
5
5
  Jeweler::Tasks.new do |gem|
6
- gem.name = "jnunemaker-validatable"
7
- gem.summary = %Q{Validatable is a library for adding validations.}
8
- gem.email = "nunemaker@gmail.com"
9
- gem.homepage = "http://github.com/jnunemaker/validatable"
10
- gem.authors = ['Jay Fields', 'John Nunemaker']
11
- gem.files = FileList['lib/**/*.rb', '[A-Z]*', 'test/**/*'].to_a
6
+ gem.name = "jnunemaker-validatable"
7
+ gem.summary = %Q{Validatable is a library for adding validations.}
8
+ gem.description = %Q{Validatable is a library for adding validations.}
9
+ gem.email = "nunemaker@gmail.com"
10
+ gem.homepage = "http://github.com/jnunemaker/validatable"
11
+ gem.authors = ['Jay Fields', 'John Nunemaker']
12
+ gem.files = FileList['lib/**/*.rb', '[A-Z]*', 'test/**/*'].to_a
12
13
  end
13
14
 
14
15
  Jeweler::GemcutterTasks.new
@@ -1,5 +1,5 @@
1
1
  ---
2
- :major: 1
3
2
  :minor: 8
3
+ :patch: 1
4
+ :major: 1
4
5
  :build:
5
- :patch: 0
@@ -18,5 +18,7 @@ require 'validatable/validations/validates_confirmation_of'
18
18
  require 'validatable/validations/validates_length_of'
19
19
  require 'validatable/validations/validates_true_for'
20
20
  require 'validatable/validations/validates_numericality_of'
21
+ require 'validatable/validations/validates_exclusion_of'
22
+ require 'validatable/validations/validates_inclusion_of'
21
23
  require 'validatable/validations/validates_each'
22
24
  require 'validatable/validations/validates_associated'
@@ -193,6 +193,14 @@ module Validatable
193
193
  def validates_true_for(*args)
194
194
  add_validations(args, ValidatesTrueFor)
195
195
  end
196
+
197
+ def validates_exclusion_of(*args)
198
+ add_validations(args, ValidatesExclusionOf)
199
+ end
200
+
201
+ def validates_inclusion_of(*args)
202
+ add_validations(args, ValidatesInclusionOf)
203
+ end
196
204
 
197
205
  # call-seq: validates_associated(*args)
198
206
  #
@@ -0,0 +1,17 @@
1
+ module Validatable
2
+ class ValidatesExclusionOf < ValidationBase #:nodoc:
3
+ required_option :within
4
+
5
+ def valid?(instance)
6
+ value = instance.send(attribute)
7
+ return true if allow_nil && value.nil?
8
+ return true if allow_blank && value.blank?
9
+
10
+ !within.include?(value)
11
+ end
12
+
13
+ def message(instance)
14
+ super || "is reserved"
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module Validatable
2
+ class ValidatesInclusionOf < ValidationBase
3
+ required_option :within
4
+
5
+ def valid?(instance)
6
+ value = instance.send(attribute)
7
+ return true if allow_nil && value.nil?
8
+ return true if allow_blank && value.blank?
9
+
10
+ within.include?(value)
11
+ end
12
+
13
+ def message(instance)
14
+ super || "is not in the list"
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,29 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ module Functional
4
+ class ValidatesExclusionOfTest < Test::Unit::TestCase
5
+ test "given excluded state, when validated, then error is in the objects error collection" do
6
+ klass = Class.new do
7
+ include Validatable
8
+ attr_accessor :state
9
+ validates_exclusion_of :state, :within => %w(in out)
10
+ end
11
+ instance = klass.new
12
+ instance.state = 'in'
13
+ instance.valid?
14
+ assert_equal "is reserved", instance.errors.on(:state)
15
+ end
16
+
17
+ test "given non-excluded state, when validated, then no error is in the objects error collection" do
18
+ klass = Class.new do
19
+ include Validatable
20
+ attr_accessor :state
21
+ validates_exclusion_of :state, :within => %w(in out)
22
+ end
23
+ instance = klass.new
24
+ instance.state = 'foo'
25
+ instance.valid?
26
+ assert_nil instance.errors.on(:state)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ module Functional
4
+ class ValidatesInclusionOfTest < Test::Unit::TestCase
5
+ test "given state not in list, when validated, then error is in the objects error collection" do
6
+ klass = Class.new do
7
+ include Validatable
8
+ attr_accessor :state
9
+ validates_inclusion_of :state, :within => %w(in out)
10
+ end
11
+ instance = klass.new
12
+ instance.state = 'foo'
13
+ instance.valid?
14
+ assert_equal "is not in the list", instance.errors.on(:state)
15
+ end
16
+
17
+ test "given state in list, when validated, then no error is in the objects error collection" do
18
+ klass = Class.new do
19
+ include Validatable
20
+ attr_accessor :state
21
+ validates_inclusion_of :state, :within => %w(in out)
22
+ end
23
+ instance = klass.new
24
+ instance.state = 'in'
25
+ instance.valid?
26
+ assert_nil instance.errors.on(:state)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,23 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ Expectations do
4
+ expect false do
5
+ validation = Validatable::ValidatesExclusionOf.new stub_everything, :state, :within => %w(in out)
6
+ validation.valid?(stub(:state => 'in'))
7
+ end
8
+
9
+ expect true do
10
+ validation = Validatable::ValidatesExclusionOf.new stub_everything, :state, :within => %w(in out)
11
+ validation.valid?(stub(:state => 'foo'))
12
+ end
13
+
14
+ expect true do
15
+ validation = Validatable::ValidatesExclusionOf.new stub_everything, :state, :within => %w(in out), :allow_blank => true
16
+ validation.valid?(stub(:state => ''))
17
+ end
18
+
19
+ expect true do
20
+ validation = Validatable::ValidatesExclusionOf.new stub_everything, :state, :within => %w(in out), :allow_nil => true
21
+ validation.valid?(stub(:state => nil))
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ Expectations do
4
+ expect true do
5
+ validation = Validatable::ValidatesInclusionOf.new stub_everything, :state, :within => %w(in out)
6
+ validation.valid?(stub(:state => 'in'))
7
+ end
8
+
9
+ expect false do
10
+ validation = Validatable::ValidatesInclusionOf.new stub_everything, :state, :within => %w(in out)
11
+ validation.valid?(stub(:state => 'foo'))
12
+ end
13
+
14
+ expect true do
15
+ validation = Validatable::ValidatesInclusionOf.new stub_everything, :state, :within => %w(in out), :allow_blank => true
16
+ validation.valid?(stub(:state => ''))
17
+ end
18
+
19
+ expect true do
20
+ validation = Validatable::ValidatesInclusionOf.new stub_everything, :state, :within => %w(in out), :allow_nil => true
21
+ validation.valid?(stub(:state => nil))
22
+ end
23
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jnunemaker-validatable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.0
4
+ version: 1.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jay Fields
@@ -10,11 +10,11 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-10-21 00:00:00 -04:00
13
+ date: 2009-11-01 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies: []
16
16
 
17
- description:
17
+ description: Validatable is a library for adding validations.
18
18
  email: nunemaker@gmail.com
19
19
  executables: []
20
20
 
@@ -40,7 +40,9 @@ files:
40
40
  - lib/validatable/validations/validates_associated.rb
41
41
  - lib/validatable/validations/validates_confirmation_of.rb
42
42
  - lib/validatable/validations/validates_each.rb
43
+ - lib/validatable/validations/validates_exclusion_of.rb
43
44
  - lib/validatable/validations/validates_format_of.rb
45
+ - lib/validatable/validations/validates_inclusion_of.rb
44
46
  - lib/validatable/validations/validates_length_of.rb
45
47
  - lib/validatable/validations/validates_numericality_of.rb
46
48
  - lib/validatable/validations/validates_presence_of.rb
@@ -51,7 +53,9 @@ files:
51
53
  - test/functional/test_validates_associated.rb
52
54
  - test/functional/test_validates_confirmation_of.rb
53
55
  - test/functional/test_validates_each.rb
56
+ - test/functional/test_validates_exclusion_of.rb
54
57
  - test/functional/test_validates_format_of.rb
58
+ - test/functional/test_validates_inclusion_of.rb
55
59
  - test/functional/test_validates_length_of.rb
56
60
  - test/functional/test_validates_numericality_of.rb
57
61
  - test/functional/test_validates_presence_of.rb
@@ -63,7 +67,9 @@ files:
63
67
  - test/unit/test_validates_acceptance_of.rb
64
68
  - test/unit/test_validates_associated.rb
65
69
  - test/unit/test_validates_confirmation_of.rb
70
+ - test/unit/test_validates_exclusion_of.rb
66
71
  - test/unit/test_validates_format_of.rb
72
+ - test/unit/test_validates_inclusion_of.rb
67
73
  - test/unit/test_validates_length_of.rb
68
74
  - test/unit/test_validates_numericality_of.rb
69
75
  - test/unit/test_validates_presence_of.rb
@@ -103,7 +109,9 @@ test_files:
103
109
  - test/functional/test_validates_associated.rb
104
110
  - test/functional/test_validates_confirmation_of.rb
105
111
  - test/functional/test_validates_each.rb
112
+ - test/functional/test_validates_exclusion_of.rb
106
113
  - test/functional/test_validates_format_of.rb
114
+ - test/functional/test_validates_inclusion_of.rb
107
115
  - test/functional/test_validates_length_of.rb
108
116
  - test/functional/test_validates_numericality_of.rb
109
117
  - test/functional/test_validates_presence_of.rb
@@ -115,7 +123,9 @@ test_files:
115
123
  - test/unit/test_validates_acceptance_of.rb
116
124
  - test/unit/test_validates_associated.rb
117
125
  - test/unit/test_validates_confirmation_of.rb
126
+ - test/unit/test_validates_exclusion_of.rb
118
127
  - test/unit/test_validates_format_of.rb
128
+ - test/unit/test_validates_inclusion_of.rb
119
129
  - test/unit/test_validates_length_of.rb
120
130
  - test/unit/test_validates_numericality_of.rb
121
131
  - test/unit/test_validates_presence_of.rb