validatable 1.3.4 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README CHANGED
@@ -115,4 +115,4 @@ In the above example the attribute "name" is appended to the message.
115
115
  See the tests for more examples
116
116
 
117
117
  == Contributors
118
- Rick Bradley, Anonymous Z, Jason Miller, Ali Aghareza, Xavier Shay, Dan Manges, Karthik Krishnan and Venkat
118
+ Rick Bradley, Anonymous Z, Jason Miller, Ali Aghareza, Xavier Shay, Dan Manges, Karthik Krishnan and Venkat, Clint Bishop
@@ -195,11 +195,15 @@ module Validatable
195
195
  children_to_validate << ChildValidation.new(attribute_to_validate, options[:map] || {}, options[:if] || lambda { true })
196
196
  end
197
197
 
198
- def validate_children(instance, groups) #:nodoc:
198
+ def validate_children(instance, group) #:nodoc:
199
199
  self.children_to_validate.each do |child_validation|
200
200
  next unless child_validation.should_validate?(instance)
201
201
  child = instance.send child_validation.attribute
202
- child.valid?(*groups)
202
+ if (child.respond_to?(:valid_for_group?))
203
+ child.valid_for_group?(group)
204
+ else
205
+ child.valid?
206
+ end
203
207
  child.errors.each do |attribute, messages|
204
208
  if messages.is_a?(String)
205
209
  add_error(instance, child_validation.map[attribute.to_sym] || attribute, messages)
@@ -220,12 +224,16 @@ module Validatable
220
224
  instance.errors.add(attribute, msg)
221
225
  end
222
226
 
227
+ def validation_keys_include?(key)
228
+ validations.map { |validation| validation.key }.include?(key)
229
+ end
230
+
223
231
  protected
224
232
 
225
233
  def add_validations(args, klass) #:nodoc:
226
234
  options = args.last.is_a?(Hash) ? args.pop : {}
227
235
  args.each do |attribute|
228
- new_validation = klass.new attribute, options
236
+ new_validation = klass.new self, attribute, options
229
237
  self.validations << new_validation
230
238
  self.create_valid_method_for_groups new_validation.groups
231
239
  end
@@ -17,17 +17,35 @@ module Validatable
17
17
  @errors ||= Validatable::Errors.new
18
18
  end
19
19
 
20
- protected
21
- def valid_for_group?(*groups) #:nodoc:
20
+ def valid_for_group?(group=nil) #:nodoc:
22
21
  errors.clear
23
- self.class.validate_children(self, groups)
24
- self.validate(groups)
22
+ self.class.validate_children(self, group)
23
+ self.validate(group)
25
24
  errors.empty?
26
25
  end
26
+
27
+ def times_validated(key)
28
+ times_validated_hash[key] || 0
29
+ end
30
+
31
+ def times_validated_hash
32
+ @times_validated_hash ||= {}
33
+ end
34
+
35
+ def increment_times_validated_for(validation)
36
+ if validation.key != nil
37
+ if times_validated_hash[validation.key].nil?
38
+ times_validated_hash[validation.key] = 1
39
+ else
40
+ times_validated_hash[validation.key] += 1
41
+ end
42
+ end
43
+ end
27
44
 
28
- def validate(groups) #:nodoc:
45
+ protected
46
+ def validate(group) #:nodoc:
29
47
  validation_levels.each do |level|
30
- validations_for_level_and_groups(level, groups).each do |validation|
48
+ validations_for_level_and_group(level, group).each do |validation|
31
49
  run_validation(validation) if validation.should_validate?(self)
32
50
  end
33
51
  return unless self.errors.empty?
@@ -37,6 +55,7 @@ module Validatable
37
55
  def run_validation(validation) #:nodoc:
38
56
  validation_result = validation.valid?(self)
39
57
  add_error(validation.attribute, validation.message(self)) unless validation_result
58
+ increment_times_validated_for(validation)
40
59
  validation.run_after_validate(validation_result, self, validation.attribute)
41
60
  end
42
61
 
@@ -44,17 +63,13 @@ module Validatable
44
63
  self.class.add_error(self, attribute, message)
45
64
  end
46
65
 
47
- def validations_for_level_and_groups(level, groups) #:nodoc:
48
- validations_for_level = self.validations.select { |validation| validation.level == level }
49
- return validations_for_level if groups.empty?
50
- validations_for_level.select { |validation| (groups & validation.groups).any? }
66
+ def validations_for_level_and_group(level, group) #:nodoc:
67
+ validations_for_level = self.class.validations.select { |validation| validation.level == level }
68
+ return validations_for_level if group.nil?
69
+ validations_for_level.select { |validation| validation.groups.include?(group) }
51
70
  end
52
71
 
53
72
  def validation_levels #:nodoc:
54
- self.validations.inject([1]) { |accum,validation| accum << validation.level }.uniq.sort
55
- end
56
-
57
- def validations #:nodoc:
58
- @validations ||= self.class.validations.collect { |validation| validation.dup }
73
+ self.class.validations.inject([1]) { |accum,validation| accum << validation.level }.uniq.sort
59
74
  end
60
75
  end
@@ -41,11 +41,11 @@ module Validatable
41
41
  include Understandable
42
42
  include Requireable
43
43
 
44
- option :message, :if, :times, :level, :groups
45
- default :if => lambda { true }, :level => 1, :groups => []
44
+ option :message, :if, :times, :level, :groups, :key
45
+ default :level => 1, :groups => []
46
46
  attr_accessor :attribute
47
47
 
48
- def initialize(attribute, options={})
48
+ def initialize(klass, attribute, options={})
49
49
  must_understand options
50
50
  requires options
51
51
  self.class.all_understandings.each do |understanding|
@@ -54,20 +54,28 @@ module Validatable
54
54
  end
55
55
  self.attribute = attribute
56
56
  self.groups = [self.groups] unless self.groups.is_a?(Array)
57
+ self.key = "#{self.class.name}:#{self.attribute}:#{self.key || "key"}" unless self.times.nil?
58
+ raise_error_if_key_is_dup(klass) unless self.key.nil?
59
+ end
60
+
61
+ def raise_error_if_key_is_dup(klass)
62
+ message = "key #{self.key} must be unique, provide the :key option to specify a unique key"
63
+ raise ArgumentError.new(message) if klass.validation_keys_include? self.key
57
64
  end
58
65
 
59
66
  def should_validate?(instance)
60
- instance.instance_eval(&self.if) && validate_this_time?
67
+ result = validate_this_time?(instance)
68
+ result &&= instance.instance_eval(&self.if) unless self.if.nil?
69
+ result
61
70
  end
62
71
 
63
72
  def message(instance)
64
73
  @message.respond_to?(:call) ? instance.instance_eval(&@message) : @message
65
74
  end
66
75
 
67
- def validate_this_time?
76
+ def validate_this_time?(instance)
68
77
  return true if @times.nil?
69
- self.times -= 1
70
- self.times >= 0
78
+ self.times > instance.times_validated(self.key)
71
79
  end
72
80
 
73
81
  def run_after_validate(result, instance, attribute)
@@ -29,7 +29,7 @@ Gem::manage_gems
29
29
  specification = Gem::Specification.new do |s|
30
30
  s.name = "validatable"
31
31
  s.summary = "Validatable is a library for adding validations."
32
- s.version = "1.3.4"
32
+ s.version = "1.4.0"
33
33
  s.author = 'Jay Fields'
34
34
  s.description = "Validatable is a library for adding validations."
35
35
  s.email = 'validatable-developer@rubyforge.org'
@@ -2,20 +2,45 @@ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
2
 
3
3
  module Functional
4
4
  class ValidatableTest < Test::Unit::TestCase
5
- test "validations are only executed once" do
6
- if_condition = mock
7
- if_condition.expects(:where?).times 2
8
- klass = Class.new do
5
+
6
+ expect ArgumentError do
7
+ Class.new do
8
+ include Validatable
9
+ attr_accessor :name
10
+ validates_presence_of :name, :times => 1
11
+ validates_presence_of :name, :times => 1
12
+ end
13
+ end
14
+
15
+ expect ArgumentError do
16
+ Class.new do
17
+ include Validatable
18
+ attr_accessor :name
19
+ validates_presence_of :name, :times => 1, :key => 'anything'
20
+ validates_presence_of :name, :times => 1, :key => 'anything'
21
+ end
22
+ end
23
+
24
+ expect "is invalid" do
25
+ child_class = Class.new do
9
26
  include Validatable
10
27
  attr_accessor :name, :address
11
- validates_presence_of :name, :if => lambda { if_condition.where? }
12
- validates_presence_of :address, :if => lambda { if_condition.where? }
28
+ validates_presence_of :name
29
+ validates_format_of :address, :with => /.+/
30
+ end
31
+ klass = Class.new do
32
+ include Validatable
33
+ include_validations_for :child
34
+ define_method :child do
35
+ child_class.new
36
+ end
13
37
  end
14
38
  instance = klass.new
15
39
  instance.valid?
40
+ instance.errors.on(:address)
16
41
  end
17
42
 
18
- test "given a child class with validations, when parent class is validated, then the error is in the parent objects error collection" do
43
+ expect "can't be empty" do
19
44
  child_class = Class.new do
20
45
  include Validatable
21
46
  attr_accessor :name, :address
@@ -31,8 +56,7 @@ module Functional
31
56
  end
32
57
  instance = klass.new
33
58
  instance.valid?
34
- assert_equal "is invalid", instance.errors.on(:address)
35
- assert_equal "can't be empty", instance.errors.on(:name)
59
+ instance.errors.on(:name)
36
60
  end
37
61
 
38
62
 
@@ -42,20 +66,16 @@ module Functional
42
66
  attr_accessor :name
43
67
  validates_presence_of :name
44
68
  end
69
+
45
70
  klass = Class.new do
46
71
  include Validatable
47
72
  extend Forwardable
48
-
49
73
  def_delegator :child, :name
50
-
51
74
  validates_true_for :name, :logic => lambda { false }, :level => 2, :message => "invalid message"
52
-
53
75
  include_validations_for :child
54
-
55
76
  define_method :child do
56
77
  @child ||= child_class.new
57
78
  end
58
-
59
79
  end
60
80
  instance = klass.new
61
81
  instance.valid?
@@ -67,15 +87,12 @@ module Functional
67
87
  include Validatable
68
88
  attr_accessor :name
69
89
  validates_presence_of :name
70
-
71
90
  end
91
+
72
92
  klass = Class.new do
73
93
  include Validatable
74
-
75
94
  validates_true_for :address, :logic => lambda { false }, :level => 1, :message => "invalid message"
76
-
77
95
  include_validations_for :child
78
-
79
96
  define_method :child do
80
97
  @child ||= child_class.new
81
98
  end
@@ -87,7 +104,7 @@ module Functional
87
104
  assert_equal "invalid message", instance.errors.on(:address)
88
105
  end
89
106
 
90
- test "given a child class with validations, the error is in the parent objects error collection as the mapped attribute" do
107
+ expect "can't be empty" do
91
108
  child_class = Class.new do
92
109
  include Validatable
93
110
  attr_accessor :name, :address
@@ -102,10 +119,10 @@ module Functional
102
119
  end
103
120
  instance = klass.new
104
121
  instance.valid?
105
- assert_equal "can't be empty", instance.errors.on(:namen)
122
+ instance.errors.on(:namen)
106
123
  end
107
124
 
108
- test "given a child class with validations, the error is in the parent objects error collection when the 'if' evals to true" do
125
+ expect "can't be empty" do
109
126
  child_class = Class.new do
110
127
  include Validatable
111
128
  attr_accessor :name, :address
@@ -120,10 +137,10 @@ module Functional
120
137
  end
121
138
  instance = klass.new
122
139
  instance.valid?
123
- assert_equal "can't be empty", instance.errors.on(:name)
140
+ instance.errors.on(:name)
124
141
  end
125
142
 
126
- test "given a child class with validations, the error is not in the parent objects error collection when the if evals to false" do
143
+ expect true do
127
144
  child_class = Class.new do
128
145
  include Validatable
129
146
  attr_accessor :name, :address
@@ -137,7 +154,7 @@ module Functional
137
154
  end
138
155
  end
139
156
  instance = klass.new
140
- assert_equal true, instance.valid?
157
+ instance.valid?
141
158
  end
142
159
 
143
160
  test "classes only have valid_for_* methods for groups that appear in their validations" do
@@ -166,7 +183,7 @@ module Functional
166
183
  assert_equal nil, instance.errors.on(:name)
167
184
  end
168
185
 
169
- test "after validate is called following a validation" do
186
+ expect "can't be empty twice changed message" do
170
187
  klass = Class.new do
171
188
  include Validatable
172
189
  validates_presence_of :name
@@ -185,39 +202,39 @@ module Functional
185
202
  end
186
203
  instance = klass.new
187
204
  instance.valid?
188
- assert_equal "can't be empty twice changed message", instance.errors.on(:name).join
189
205
  Validatable::ValidatesPresenceOf.after_validations.clear
190
206
  Validatable::ValidationBase.after_validations.clear
207
+ instance.errors.on(:name).join
191
208
  end
192
209
 
193
- test "matching groups are used as validations when multiple groups are given to valid" do
210
+ expect false do
194
211
  klass = Class.new do
195
212
  include Validatable
196
213
  validates_presence_of :name, :groups => :group_one
197
214
  attr_accessor :name
198
215
  end
199
216
  instance = klass.new
200
- assert_equal false, instance.valid_for_group_one?
217
+ instance.valid_for_group_one?
201
218
  end
202
219
 
203
- test "matching groups are used as validations when validations are part of multiple groups" do
220
+ expect false do
204
221
  klass = Class.new do
205
222
  include Validatable
206
223
  validates_presence_of :name, :groups => [:group_one, :group_two]
207
224
  attr_accessor :name
208
225
  end
209
226
  instance = klass.new
210
- assert_equal false, instance.valid_for_group_one?
227
+ instance.valid_for_group_one?
211
228
  end
212
229
 
213
- test "no group given then all validations are used" do
230
+ expect false do
214
231
  klass = Class.new do
215
232
  include Validatable
216
233
  validates_presence_of :name, :groups => :group_one
217
234
  attr_accessor :name
218
235
  end
219
236
  instance = klass.new
220
- assert_equal false, instance.valid?
237
+ instance.valid?
221
238
  end
222
239
 
223
240
  expect true do
@@ -242,7 +259,7 @@ module Functional
242
259
  instance2 = klass.new
243
260
  instance2.valid?
244
261
  end
245
-
262
+
246
263
  expect "name message" do
247
264
  klass = Class.new do
248
265
  include Validatable
@@ -3,8 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
3
3
  module Unit
4
4
  class ValidatableTest < Test::Unit::TestCase
5
5
  expect false do
6
- validation = stub(:valid? => false, :should_validate? => true,
7
- :attribute => "attribute", :message => "message", :level => 1, :run_after_validate => nil)
6
+ validation = stub_everything(:valid? => false, :should_validate? => true, :attribute => "attribute", :level => 1)
8
7
  klass = Class.new do
9
8
  include Validatable
10
9
  validations << validation
@@ -21,6 +20,21 @@ module Unit
21
20
  instance.valid?
22
21
  instance.errors.empty?
23
22
  end
24
-
23
+
24
+ expect false do
25
+ klass = Class.new do
26
+ include Validatable
27
+ end
28
+ klass.validation_keys_include?("anything")
29
+ end
30
+
31
+ expect true do
32
+ validation = stub_everything(:key => "key")
33
+ klass = Class.new do
34
+ include Validatable
35
+ validations << validation
36
+ end
37
+ klass.validation_keys_include?("key")
38
+ end
25
39
  end
26
40
  end
@@ -2,19 +2,20 @@ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
2
 
3
3
  class ValidatesAcceptanceOfTest < Test::Unit::TestCase
4
4
  test "valid acceptance" do
5
- validation = Validatable::ValidatesAcceptanceOf.new :acceptance
5
+ validation = Validatable::ValidatesAcceptanceOf.new stub, :acceptance
6
6
  instance = stub(:acceptance=>'true')
7
7
  assert_equal true, validation.valid?(instance)
8
8
  end
9
9
 
10
10
  test "invalid acceptance" do
11
- validation = Validatable::ValidatesAcceptanceOf.new :acceptance
11
+ validation = Validatable::ValidatesAcceptanceOf.new stub, :acceptance
12
12
  instance = stub(:acceptance=>'false')
13
13
  assert_equal false, validation.valid?(instance)
14
14
  end
15
15
 
16
16
  expect true do
17
- Validatable::ValidatesAcceptanceOf.new(:test).must_understand(:message => nil, :if => nil, :times => nil, :level => nil, :groups => nil)
17
+ options = {:message => nil, :if => nil, :times => nil, :level => nil, :groups => nil}
18
+ Validatable::ValidatesAcceptanceOf.new(stub, :test).must_understand(options)
18
19
  end
19
20
 
20
21
  end
@@ -2,62 +2,62 @@ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
2
 
3
3
  class ValidatesConfirmationOfTest < Test::Unit::TestCase
4
4
  test "valid confirmation" do
5
- validation = Validatable::ValidatesConfirmationOf.new :username
5
+ validation = Validatable::ValidatesConfirmationOf.new stub, :username
6
6
  instance = stub(:username=>"username", :username_confirmation=>"username")
7
7
  assert_equal true, validation.valid?(instance)
8
8
  end
9
9
 
10
10
  test "invalid confirmation" do
11
- validation = Validatable::ValidatesConfirmationOf.new :username
11
+ validation = Validatable::ValidatesConfirmationOf.new stub, :username
12
12
  instance = stub(:username=>"username", :username_confirmation=>"usessrname")
13
13
  assert_equal false, validation.valid?(instance)
14
14
  end
15
15
 
16
16
  test "valid confirmation with case insensitive" do
17
- validation = Validatable::ValidatesConfirmationOf.new :username, :case_sensitive => false
17
+ validation = Validatable::ValidatesConfirmationOf.new stub, :username, :case_sensitive => false
18
18
  instance = stub(:username=>"username", :username_confirmation=>"USERNAME")
19
19
  assert_equal true, validation.valid?(instance)
20
20
  end
21
21
 
22
22
  test "invalid confirmation with case sensitive" do
23
- validation = Validatable::ValidatesConfirmationOf.new :username, :case_sensitive => true
23
+ validation = Validatable::ValidatesConfirmationOf.new stub, :username, :case_sensitive => true
24
24
  instance = stub(:username=>"username", :username_confirmation=>"USERNAME")
25
25
  assert_equal false, validation.valid?(instance)
26
26
  end
27
27
 
28
28
  test "invalid confirmation if value is nil and confirmation is not with case sensitive true" do
29
- validation = Validatable::ValidatesConfirmationOf.new :username, :case_sensitive => true
29
+ validation = Validatable::ValidatesConfirmationOf.new stub, :username, :case_sensitive => true
30
30
  assert_equal false, validation.valid?(stub(:username => nil, :username_confirmation => 'something'))
31
31
  end
32
32
 
33
33
  test "invalid confirmation if confirmation is nil and value is not with case sensitive true" do
34
- validation = Validatable::ValidatesConfirmationOf.new :username, :case_sensitive => true
34
+ validation = Validatable::ValidatesConfirmationOf.new stub, :username, :case_sensitive => true
35
35
  assert_equal false, validation.valid?(stub(:username => 'something', :username_confirmation => nil))
36
36
  end
37
37
 
38
38
  test "valid confirmation if value and confirmation are nil with case sensitive true" do
39
- validation = Validatable::ValidatesConfirmationOf.new :username, :case_sensitive => true
39
+ validation = Validatable::ValidatesConfirmationOf.new stub, :username, :case_sensitive => true
40
40
  assert_equal true, validation.valid?(stub(:username => nil, :username_confirmation => nil))
41
41
  end
42
42
 
43
43
  test "invalid confirmation if value is nil and confirmation is not with case sensitive false" do
44
- validation = Validatable::ValidatesConfirmationOf.new :username, :case_sensitive => false
44
+ validation = Validatable::ValidatesConfirmationOf.new stub, :username, :case_sensitive => false
45
45
  assert_equal false, validation.valid?(stub(:username => nil, :username_confirmation => 'something'))
46
46
  end
47
47
 
48
48
  test "invalid confirmation if confirmation is nil and value is not with case sensitive false" do
49
- validation = Validatable::ValidatesConfirmationOf.new :username, :case_sensitive => false
49
+ validation = Validatable::ValidatesConfirmationOf.new stub, :username, :case_sensitive => false
50
50
  assert_equal false, validation.valid?(stub(:username => 'something', :username_confirmation => nil))
51
51
  end
52
52
 
53
53
  test "valid confirmation if value and confirmation are nil with case sensitive false" do
54
- validation = Validatable::ValidatesConfirmationOf.new :username, :case_sensitive => false
54
+ validation = Validatable::ValidatesConfirmationOf.new stub, :username, :case_sensitive => false
55
55
  assert_equal true, validation.valid?(stub(:username => nil, :username_confirmation => nil))
56
56
  end
57
57
 
58
58
  expect true do
59
59
  options = { :message => nil, :if => nil, :times => nil, :level => nil, :groups => nil, :case_sensitive => nil }
60
- Validatable::ValidatesConfirmationOf.new(:test).must_understand(options)
60
+ Validatable::ValidatesConfirmationOf.new(stub, :test).must_understand(options)
61
61
  end
62
62
 
63
63
  end
@@ -2,34 +2,34 @@ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
2
 
3
3
  class ValidatesFormatOfTest < Test::Unit::TestCase
4
4
  test "when attribute value does not match the given regex, then valid is false" do
5
- validation = Validatable::ValidatesFormatOf.new :name, :with => /book/
5
+ validation = Validatable::ValidatesFormatOf.new stub, :name, :with => /book/
6
6
  assert_equal false, validation.valid?(stub_everything)
7
7
  end
8
8
 
9
9
  test "when attribute value does match the given regex, then valid is true" do
10
- validation = Validatable::ValidatesFormatOf.new :name, :with => /book/
10
+ validation = Validatable::ValidatesFormatOf.new stub, :name, :with => /book/
11
11
  assert_equal true, validation.valid?(stub(:name=>"book"))
12
12
  end
13
13
 
14
14
  test "when attribute value is an integer it should be converted to a string before matching" do
15
- validation = Validatable::ValidatesFormatOf.new :age, :with => /14/
15
+ validation = Validatable::ValidatesFormatOf.new stub, :age, :with => /14/
16
16
  assert_equal true, validation.valid?(stub(:age=>14))
17
17
  end
18
18
 
19
19
  test "when no with is given, then an error is raised during construction" do
20
20
  assert_raises ArgumentError do
21
- validation = Validatable::ValidatesFormatOf.new :age
21
+ validation = Validatable::ValidatesFormatOf.new stub, :age
22
22
  end
23
23
  end
24
24
 
25
25
  expect true do
26
- options = [:message, :if, :times, :level, :groups, :with]
27
- Validatable::ValidatesFormatOf.new(:test, options.to_blank_options_hash).must_understand(options.to_blank_options_hash)
26
+ options = [:message, :if, :times, :level, :groups, :with, :key]
27
+ Validatable::ValidatesFormatOf.new(stub, :test, options.to_blank_options_hash).must_understand(options.to_blank_options_hash)
28
28
  end
29
29
 
30
30
  expect true do
31
31
  options = [:with]
32
- Validatable::ValidatesFormatOf.new(:name, options.to_blank_options_hash).requires(options.to_blank_options_hash)
32
+ Validatable::ValidatesFormatOf.new(stub, :name, options.to_blank_options_hash).requires(options.to_blank_options_hash)
33
33
  end
34
34
 
35
35
  end
@@ -3,74 +3,74 @@ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
3
3
  module Unit
4
4
  class ValidatesLengthOfTest < Test::Unit::TestCase
5
5
  expect false do
6
- validation = Validatable::ValidatesLengthOf.new :username, :maximum => 8
6
+ validation = Validatable::ValidatesLengthOf.new stub, :username, :maximum => 8
7
7
  validation.valid?(stub(:username=>"usernamefdfd"))
8
8
  end
9
9
 
10
10
  test "min length" do
11
- validation = Validatable::ValidatesLengthOf.new :username, :minimum => 2
11
+ validation = Validatable::ValidatesLengthOf.new stub, :username, :minimum => 2
12
12
  instance = stub(:username=>"u")
13
13
  assert_equal false, validation.valid?(instance)
14
14
  end
15
15
 
16
16
  test "valid length" do
17
- validation = Validatable::ValidatesLengthOf.new :username, :minimum => 2, :maximum => 8
17
+ validation = Validatable::ValidatesLengthOf.new stub, :username, :minimum => 2, :maximum => 8
18
18
  instance = stub(:username=>"udfgdf")
19
19
  assert_equal true, validation.valid?(instance)
20
20
  end
21
21
 
22
22
  test "is length is false" do
23
- validation = Validatable::ValidatesLengthOf.new :username, :is => 2
23
+ validation = Validatable::ValidatesLengthOf.new stub, :username, :is => 2
24
24
  instance = stub(:username=>"u")
25
25
  assert_equal false, validation.valid?(instance)
26
26
  end
27
27
 
28
28
  test "is length is true" do
29
- validation = Validatable::ValidatesLengthOf.new :username, :is => 2
29
+ validation = Validatable::ValidatesLengthOf.new stub, :username, :is => 2
30
30
  instance = stub(:username=>"uu")
31
31
  assert_equal true, validation.valid?(instance)
32
32
  end
33
33
 
34
34
  test "within lower bound is true" do
35
- validation = Validatable::ValidatesLengthOf.new :username, :within => 2..4
35
+ validation = Validatable::ValidatesLengthOf.new stub, :username, :within => 2..4
36
36
  instance = stub(:username => "aa")
37
37
  assert_equal true, validation.valid?(instance)
38
38
  end
39
39
 
40
40
  test "within outside lower bound is false" do
41
- validation = Validatable::ValidatesLengthOf.new :username, :within => 2..4
41
+ validation = Validatable::ValidatesLengthOf.new stub, :username, :within => 2..4
42
42
  instance = stub(:username => "a")
43
43
  assert_equal false, validation.valid?(instance)
44
44
  end
45
45
 
46
46
  test "within upper bound is true" do
47
- validation = Validatable::ValidatesLengthOf.new :username, :within => 2..4
47
+ validation = Validatable::ValidatesLengthOf.new stub, :username, :within => 2..4
48
48
  instance = stub(:username => "aaaa")
49
49
  assert_equal true, validation.valid?(instance)
50
50
  end
51
51
 
52
52
  test "within outside upper bound is false" do
53
- validation = Validatable::ValidatesLengthOf.new :username, :within => 2..4
53
+ validation = Validatable::ValidatesLengthOf.new stub, :username, :within => 2..4
54
54
  instance = stub(:username => "aaaaa")
55
55
  assert_equal false, validation.valid?(instance)
56
56
  end
57
57
 
58
58
  test "given nil value, should not be valid" do
59
- validation = Validatable::ValidatesLengthOf.new :username, :within => 2..4
59
+ validation = Validatable::ValidatesLengthOf.new stub, :username, :within => 2..4
60
60
  instance = stub(:username => nil)
61
61
  assert_equal false, validation.valid?(instance)
62
62
  end
63
63
 
64
64
 
65
65
  test "given allow_nil is true, nil value should be valid" do
66
- validation = Validatable::ValidatesLengthOf.new :username, :within => 2..4, :allow_nil => true
66
+ validation = Validatable::ValidatesLengthOf.new stub, :username, :within => 2..4, :allow_nil => true
67
67
  instance = stub(:username => nil)
68
68
  assert_equal true, validation.valid?(instance)
69
69
  end
70
70
 
71
71
  expect true do
72
72
  options = [:message, :if, :times, :level, :groups, :maximum, :minimum, :is, :within, :allow_nil]
73
- Validatable::ValidatesLengthOf.new(:test).must_understand(options.to_blank_options_hash)
73
+ Validatable::ValidatesLengthOf.new(stub, :test).must_understand(options.to_blank_options_hash)
74
74
  end
75
75
 
76
76
  end
@@ -4,50 +4,50 @@ module Unit
4
4
  class ValidatesNumericalityOfTest < Test::Unit::TestCase
5
5
 
6
6
  test "when the value is nil then valid is false" do
7
- validation = Validatable::ValidatesNumericalityOf.new :nothing
7
+ validation = Validatable::ValidatesNumericalityOf.new stub, :nothing
8
8
  instance = stub(:nothing => nil)
9
9
  assert_equal false, validation.valid?(instance)
10
10
  end
11
11
 
12
12
  test "when value is an integer then valid is true" do
13
- validation = Validatable::ValidatesNumericalityOf.new :some_int
13
+ validation = Validatable::ValidatesNumericalityOf.new stub, :some_int
14
14
  instance = stub(:some_int => 50)
15
15
  assert_equal true, validation.valid?(instance)
16
16
  end
17
17
 
18
18
  test "when value is an decimal then valid is true" do
19
- validation = Validatable::ValidatesNumericalityOf.new :some_decimal
19
+ validation = Validatable::ValidatesNumericalityOf.new stub, :some_decimal
20
20
  instance = stub(:some_decimal => 1.23)
21
21
  assert_equal true, validation.valid?(instance)
22
22
  end
23
23
 
24
24
  test "when value is a decimal but only_integer is true, then valid is false" do
25
- validation = Validatable::ValidatesNumericalityOf.new :some_decimal, :only_integer => true
25
+ validation = Validatable::ValidatesNumericalityOf.new stub, :some_decimal, :only_integer => true
26
26
  instance = stub(:some_decimal => 1.23)
27
27
  assert_equal false, validation.valid?(instance)
28
28
  end
29
29
 
30
30
  test "when value is an integer string and only_integer is true, then valid is true" do
31
- validation = Validatable::ValidatesNumericalityOf.new :some_negative_number, :only_integer => true
31
+ validation = Validatable::ValidatesNumericalityOf.new stub, :some_negative_number, :only_integer => true
32
32
  instance = stub(:some_negative_number => "-1")
33
33
  assert_equal true, validation.valid?(instance)
34
34
  end
35
35
 
36
36
  test "when value has non numeric characters then valid is false" do
37
- validation = Validatable::ValidatesNumericalityOf.new :some_non_numeric
37
+ validation = Validatable::ValidatesNumericalityOf.new stub, :some_non_numeric
38
38
  instance = stub(:some_non_numeric => "50F")
39
39
  assert_equal false, validation.valid?(instance)
40
40
  end
41
41
 
42
42
  test "when value is a string with multiple dots then valid is false" do
43
- validation = Validatable::ValidatesNumericalityOf.new :multiple_dots
43
+ validation = Validatable::ValidatesNumericalityOf.new stub, :multiple_dots
44
44
  instance = stub(:multiple_dots => "50.0.0")
45
45
  assert_equal false, validation.valid?(instance)
46
46
  end
47
47
 
48
48
  expect true do
49
49
  options = [:message, :if, :times, :level, :groups, :only_integer]
50
- Validatable::ValidatesNumericalityOf.new(:test).must_understand(options.to_blank_options_hash)
50
+ Validatable::ValidatesNumericalityOf.new(stub, :test).must_understand(options.to_blank_options_hash)
51
51
  end
52
52
 
53
53
  end
@@ -2,22 +2,23 @@ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
2
 
3
3
  class ValidatesPresenceOfTest < Test::Unit::TestCase
4
4
  test "when attribute value does not match the given regex, then valid is false" do
5
- validation = Validatable::ValidatesPresenceOf.new :name
5
+ validation = Validatable::ValidatesPresenceOf.new stub, :name
6
6
  assert_equal false, validation.valid?(stub_everything)
7
7
  end
8
8
 
9
9
  test "when attribute value does match the given regex, then valid is true" do
10
- validation = Validatable::ValidatesPresenceOf.new :name
10
+ validation = Validatable::ValidatesPresenceOf.new stub, :name
11
11
  assert_equal true, validation.valid?(stub(:name=>"book"))
12
12
  end
13
13
 
14
14
  test "when given a true value which is not a String, then valid is true" do
15
- validation = Validatable::ValidatesPresenceOf.new :employee
15
+ validation = Validatable::ValidatesPresenceOf.new stub, :employee
16
16
  assert_equal true, validation.valid?(stub(:employee => stub(:nil? => false)))
17
17
  end
18
18
 
19
19
  expect true do
20
- Validatable::ValidatesPresenceOf.new(:test).must_understand(:message => nil, :if => nil, :times => nil, :level => nil, :groups => nil)
20
+ options = {:message => nil, :if => nil, :times => nil, :level => nil, :groups => nil}
21
+ Validatable::ValidatesPresenceOf.new(stub, :test).must_understand(options)
21
22
  end
22
23
 
23
24
  end
@@ -2,29 +2,29 @@ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
2
 
3
3
  class ValidatesTrueForTest < Test::Unit::TestCase
4
4
  test "when block returns false for attribute value, then valid is false" do
5
- validation = Validatable::ValidatesTrueFor.new :name, :logic => lambda { false }
5
+ validation = Validatable::ValidatesTrueFor.new stub, :name, :logic => lambda { false }
6
6
  assert_equal false, validation.valid?(stub_everything)
7
7
  end
8
8
 
9
9
  test "when block returns true for attribute value, then valid is false" do
10
- validation = Validatable::ValidatesTrueFor.new :name, :logic => lambda { true }
10
+ validation = Validatable::ValidatesTrueFor.new stub, :name, :logic => lambda { true }
11
11
  assert_equal true, validation.valid?(stub_everything)
12
12
  end
13
13
 
14
14
  test "when no logic is given, then an error is raised during construction" do
15
15
  assert_raises ArgumentError do
16
- validation = Validatable::ValidatesTrueFor.new :age
16
+ validation = Validatable::ValidatesTrueFor.new stub, :age
17
17
  end
18
18
  end
19
19
 
20
20
  expect true do
21
- options = [:message, :if, :times, :level, :groups, :logic]
22
- Validatable::ValidatesTrueFor.new(:name, options.to_blank_options_hash).must_understand(options.to_blank_options_hash)
21
+ options = [:message, :if, :times, :level, :groups, :logic, :key]
22
+ Validatable::ValidatesTrueFor.new(stub, :name, options.to_blank_options_hash).must_understand(options.to_blank_options_hash)
23
23
  end
24
24
 
25
25
  expect true do
26
26
  options = [:logic]
27
- Validatable::ValidatesTrueFor.new(:name, options.to_blank_options_hash).requires(options.to_blank_options_hash)
27
+ Validatable::ValidatesTrueFor.new(stub, :name, options.to_blank_options_hash).requires(options.to_blank_options_hash)
28
28
  end
29
29
 
30
30
  end
@@ -2,50 +2,61 @@ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
2
 
3
3
  class ValidationBaseTest < Test::Unit::TestCase
4
4
  expect true do
5
- validation = Validatable::ValidationBase.new :base
5
+ validation = Validatable::ValidationBase.new stub, :base
6
6
  validation.should_validate? Object.new
7
7
  end
8
8
 
9
9
  expect true do
10
- validation = Validatable::ValidationBase.new :base, :times => 1
11
- validation.validate_this_time?
10
+ validation = Validatable::ValidationBase.new stub_everything, :base, :times => 1
11
+ validation.validate_this_time?(stub(:times_validated => 0))
12
12
  end
13
13
 
14
14
  expect true do
15
- validation = Validatable::ValidationBase.new :base
16
- validation.validate_this_time?
15
+ validation = Validatable::ValidationBase.new stub, :base
16
+ validation.validate_this_time?(nil)
17
17
  end
18
18
 
19
19
  expect true do
20
- validation = Validatable::ValidationBase.new :base, :times => 2
21
- validation.validate_this_time?
22
- validation.validate_this_time?
20
+ validation = Validatable::ValidationBase.new stub_everything, :base, :times => 2
21
+ validation.validate_this_time?(stub(:times_validated => 1))
23
22
  end
24
23
 
24
+ expect "Validatable::ValidationBase:base:key" do
25
+ validation = Validatable::ValidationBase.new stub_everything, :base, :times => 1, :key => "key"
26
+ validation.key
27
+ end
28
+
29
+ expect "Validatable::ValidationBase:base:key" do
30
+ validation = Validatable::ValidationBase.new stub_everything, :base, :times => 1
31
+ validation.key
32
+ end
33
+
25
34
  expect false do
26
- validation = Validatable::ValidationBase.new :base, :times => 1
27
- validation.validate_this_time?
28
- validation.validate_this_time?
35
+ validation = Validatable::ValidationBase.new stub_everything, :base, :times => 1
36
+ validation.validate_this_time?(stub(:times_validated => 1))
29
37
  end
30
38
 
31
39
  expect 1 do
32
- validation = Validatable::ValidationBase.new :base
40
+ validation = Validatable::ValidationBase.new stub_everything, :base
33
41
  validation.level
34
42
  end
35
43
 
44
+ expect ArgumentError do
45
+ Validatable::ValidationBase.new stub(:validation_keys_include? => true), :base, :times => 1
46
+ end
47
+
36
48
  expect "some message 100" do
37
- validation = Validatable::ValidationBase.new :base, :message => lambda { "some message #{a_method}" }
49
+ validation = Validatable::ValidationBase.new stub, :base, :message => lambda { "some message #{a_method}" }
38
50
  validation.message(stub(:a_method=>'100'))
39
51
  end
40
52
 
41
- test "invalid option causes raise" do
42
- assert_raises ArgumentError do
43
- Validatable::ValidationBase.new(:base).must_understand(:foo => 1, :bar => 2)
44
- end
53
+ expect ArgumentError do
54
+ Validatable::ValidationBase.new(stub, :base).must_understand(:foo => 1, :bar => 2)
45
55
  end
46
56
 
47
57
  expect true do
48
- Validatable::ValidationBase.new(:base).must_understand(:message => nil, :if => nil, :times => nil, :level => nil, :groups => nil)
58
+ options = {:message => nil, :if => nil, :times => nil, :level => nil, :groups => nil, :key => nil}
59
+ Validatable::ValidationBase.new(stub, :base).must_understand(options)
49
60
  end
50
61
 
51
62
  end
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.0
2
+ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: validatable
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.3.4
7
- date: 2007-05-31 00:00:00 -04:00
6
+ version: 1.4.0
7
+ date: 2007-06-02 00:00:00 -04:00
8
8
  summary: Validatable is a library for adding validations.
9
9
  require_paths:
10
10
  - lib