validatable 1.6.4 → 1.6.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,9 @@
1
+ module Validatable
2
+ class IncludedValidation #:nodoc:
3
+ attr_accessor :attribute
4
+
5
+ def initialize(attribute)
6
+ @attribute = attribute
7
+ end
8
+ end
9
+ end
@@ -194,7 +194,37 @@ module Validatable
194
194
  add_validations(args, ValidatesTrueFor)
195
195
  end
196
196
 
197
- # call-seq: include_validations_for(attribute_to_validate, options = {})
197
+ # call-seq: include_validations_from(attribute)
198
+ #
199
+ # Includes all the validations that are defined on the attribute.
200
+ # class Person
201
+ # include Validatable
202
+ # validates_presence_of :name
203
+ # end
204
+ #
205
+ # class PersonPresenter
206
+ # include Validatable
207
+ # include_validataions_from :person
208
+ # attr_accessor :person
209
+ # def name
210
+ # person.name
211
+ # end
212
+ #
213
+ # def initialize(person)
214
+ # @person = person
215
+ # end
216
+ # end
217
+ #
218
+ # presenter = PersonPresenter.new(Person.new)
219
+ # presenter.valid? #=> false
220
+ # presenter.errors.on(:name) #=> "can't be blank"
221
+ #
222
+ # The name attribute whose validations should be added.
223
+ def include_validations_from(attribute_to_validate, options = {})
224
+ validations_to_include << IncludedValidation.new(attribute_to_validate)
225
+ end
226
+
227
+ # call-seq: include_errors_from(attribute_to_validate, options = {})
198
228
  #
199
229
  # Validates the specified attributes.
200
230
  # class Person
@@ -205,7 +235,7 @@ module Validatable
205
235
  #
206
236
  # class PersonPresenter
207
237
  # include Validatable
208
- # include_validations_for :person, :map => { :name => :namen }, :if => lambda { not person.nil? }
238
+ # include_errors_from :person, :map => { :name => :namen }, :if => lambda { not person.nil? }
209
239
  # attr_accessor :person
210
240
  #
211
241
  # def initialize(person)
@@ -224,10 +254,28 @@ module Validatable
224
254
  #
225
255
  # * map - A hash that maps attributes of the child to attributes of the parent.
226
256
  # * if - A block that when executed must return true of the validation will not occur.
227
- def include_validations_for(attribute_to_validate, options = {})
257
+ def include_errors_from(attribute_to_validate, options = {})
228
258
  children_to_validate << ChildValidation.new(attribute_to_validate, options[:map] || {}, options[:if] || lambda { true })
229
259
  end
230
260
 
261
+ def include_validations_for(attribute_to_validate, options = {}) #:nodoc:
262
+ puts "include_validations_for is deprecated; use include_errors_from instead"
263
+ children_to_validate << ChildValidation.new(attribute_to_validate, options[:map] || {}, options[:if] || lambda { true })
264
+ end
265
+
266
+ # call-seq: before_validation(&block)
267
+ #
268
+ # Is called before valid? or valid_for_*?
269
+ #
270
+ # class Person
271
+ # include Validatable
272
+ # before_validation do
273
+ # self.name = "default name"
274
+ # end
275
+ #
276
+ # attr_accessor :name
277
+ # end
278
+ #
231
279
  def before_validation(&block)
232
280
  before_validations << block
233
281
  end
@@ -1,5 +1,6 @@
1
- class Object
2
- module InstanceExecHelper; end
1
+ class Object #:nodoc:
2
+ module InstanceExecHelper #:nodoc:
3
+ end
3
4
  include InstanceExecHelper
4
5
  def instance_eval_with_params(*args, &block)
5
6
  begin
@@ -4,6 +4,7 @@ require File.expand_path(File.dirname(__FILE__) + '/errors')
4
4
  require File.expand_path(File.dirname(__FILE__) + '/validatable_class_methods')
5
5
  require File.expand_path(File.dirname(__FILE__) + '/macros')
6
6
  require File.expand_path(File.dirname(__FILE__) + '/validatable_instance_methods')
7
+ require File.expand_path(File.dirname(__FILE__) + '/included_validation')
7
8
  require File.expand_path(File.dirname(__FILE__) + '/child_validation')
8
9
  require File.expand_path(File.dirname(__FILE__) + '/understandable')
9
10
  require File.expand_path(File.dirname(__FILE__) + '/requireable')
@@ -15,7 +16,4 @@ require File.expand_path(File.dirname(__FILE__) + '/validations/validates_confir
15
16
  require File.expand_path(File.dirname(__FILE__) + '/validations/validates_length_of')
16
17
  require File.expand_path(File.dirname(__FILE__) + '/validations/validates_true_for')
17
18
  require File.expand_path(File.dirname(__FILE__) + '/validations/validates_numericality_of')
18
- require File.expand_path(File.dirname(__FILE__) + '/validations/validates_each')
19
- require File.expand_path(File.dirname(__FILE__) + '/validation_assertion')
20
- require File.expand_path(File.dirname(__FILE__) + '/validation_assertion_collector')
21
- require File.expand_path(File.dirname(__FILE__) + '/validatable_assertions')
19
+ require File.expand_path(File.dirname(__FILE__) + '/validations/validates_each')
@@ -34,7 +34,9 @@ module Validatable
34
34
  end
35
35
 
36
36
  def all_validations
37
- return validations + self.superclass.all_validations if self.superclass.respond_to? :all_validations
37
+ if self.respond_to?(:superclass) && self.superclass.respond_to?(:all_validations)
38
+ return validations + self.superclass.all_validations
39
+ end
38
40
  validations
39
41
  end
40
42
 
@@ -50,6 +52,10 @@ module Validatable
50
52
  validations.map { |validation| validation.key }.include?(key)
51
53
  end
52
54
 
55
+ def validations_to_include
56
+ @validations_to_include ||= []
57
+ end
58
+
53
59
  protected
54
60
 
55
61
  def add_validations(args, klass)
@@ -40,6 +40,22 @@ module Validatable
40
40
  end
41
41
  end
42
42
 
43
+ # call-seq: validate_only(key)
44
+ #
45
+ # Only executes a specified validation. The argument should follow a pattern based on the key of the validation.
46
+ # Examples:
47
+ # * validates_presence_of :name can be run with obj.validate_only("presence_of/name")
48
+ # * validates_presence_of :birthday, :key => "a key" can be run with obj.validate_only("presence_of/a key")
49
+ def validate_only(key)
50
+ validation_name, attribute_name = key.split("/")
51
+ validation_name = validation_name.split("_").collect{|word| word.capitalize}.join
52
+ validation_key = "#{self.class.name}/Validatable::Validates#{validation_name}/#{attribute_name}"
53
+ validation = self.class.all_validations.find { |validation| validation.key == validation_key }
54
+ raise ArgumentError.new("validation with key #{validation_key} could not be found") if validation.nil?
55
+ errors.clear
56
+ run_validation(validation)
57
+ end
58
+
43
59
  protected
44
60
  def times_validated_hash #:nodoc:
45
61
  @times_validated_hash ||= {}
@@ -61,7 +77,7 @@ module Validatable
61
77
  validation.run_after_validate(validation_result, self, validation.attribute)
62
78
  end
63
79
 
64
- def run_before_validations
80
+ def run_before_validations #:nodoc:
65
81
  self.class.all_before_validations.each do |block|
66
82
  instance_eval &block
67
83
  end
@@ -72,11 +88,18 @@ module Validatable
72
88
  end
73
89
 
74
90
  def validations_for_level_and_group(level, group) #:nodoc:
75
- validations_for_level = self.class.all_validations.select { |validation| validation.level == level }
91
+ validations_for_level = self.all_validations.select { |validation| validation.level == level }
76
92
  return validations_for_level.select { |validation| validation.groups.empty? } if group.nil?
77
93
  validations_for_level.select { |validation| validation.groups.include?(group) }
78
94
  end
79
95
 
96
+ def all_validations #:nodoc:
97
+ res = self.class.validations_to_include.inject(self.class.all_validations) do |result, included_validation_class|
98
+ result += self.send(included_validation_class.attribute).all_validations
99
+ result
100
+ end
101
+ end
102
+
80
103
  def validation_levels #:nodoc:
81
104
  self.class.all_validations.inject([1]) { |result, validation| result << validation.level }.uniq.sort
82
105
  end
@@ -54,8 +54,8 @@ 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?
57
+ self.key = "#{klass.name}/#{self.class.name}/#{self.key || self.attribute}"
58
+ raise_error_if_key_is_dup(klass)
59
59
  end
60
60
 
61
61
  def raise_error_if_key_is_dup(klass)
@@ -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.6.4"
32
+ s.version = "1.6.6"
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'
@@ -1,6 +1,44 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
2
 
3
3
  functional_tests do
4
+
5
+ expect "can't be empty" do
6
+ child_class = Module.new do
7
+ include Validatable
8
+ validates_presence_of :name
9
+ end
10
+ klass = Class.new do
11
+ include Validatable
12
+ include_validations_from :child
13
+ define_method :child do
14
+ child_class
15
+ end
16
+ attr_accessor :name
17
+ end
18
+ instance = klass.new
19
+ instance.valid?
20
+ instance.errors.on(:name)
21
+ end
22
+
23
+ expect "can't be empty" do
24
+ child_class = Module.new do
25
+ include Validatable
26
+ validates_presence_of :name
27
+ end
28
+ klass = Class.new do
29
+ include Validatable
30
+ validates_presence_of :address
31
+ include_validations_from :child
32
+ define_method :child do
33
+ child_class
34
+ end
35
+ attr_accessor :name, :address
36
+ end
37
+ instance = klass.new
38
+ instance.valid?
39
+ instance.errors.on(:address)
40
+ end
41
+
4
42
  expect :is_set do
5
43
  klass = Class.new do
6
44
  include Validatable
@@ -91,7 +129,7 @@ functional_tests do
91
129
  end
92
130
  klass = Class.new do
93
131
  include Validatable
94
- include_validations_for :child
132
+ include_errors_from :child
95
133
  define_method :child do
96
134
  child_class.new
97
135
  end
@@ -110,7 +148,7 @@ functional_tests do
110
148
  end
111
149
  klass = Class.new do
112
150
  include Validatable
113
- include_validations_for :child
151
+ include_errors_from :child
114
152
  define_method :child do
115
153
  child_class.new
116
154
  end
@@ -133,7 +171,7 @@ functional_tests do
133
171
  extend Forwardable
134
172
  def_delegator :child, :name
135
173
  validates_true_for :name, :logic => lambda { false }, :level => 2, :message => "invalid message"
136
- include_validations_for :child
174
+ include_errors_from :child
137
175
  define_method :child do
138
176
  @child ||= child_class.new
139
177
  end
@@ -153,7 +191,7 @@ functional_tests do
153
191
  klass = Class.new do
154
192
  include Validatable
155
193
  validates_true_for :address, :logic => lambda { false }, :level => 1, :message => "invalid message"
156
- include_validations_for :child
194
+ include_errors_from :child
157
195
  define_method :child do
158
196
  @child ||= child_class.new
159
197
  end
@@ -173,7 +211,7 @@ functional_tests do
173
211
  end
174
212
  klass = Class.new do
175
213
  include Validatable
176
- include_validations_for :child, :map => {:name => :namen}
214
+ include_errors_from :child, :map => {:name => :namen}
177
215
  define_method :child do
178
216
  child_class.new
179
217
  end
@@ -191,7 +229,7 @@ functional_tests do
191
229
  end
192
230
  klass = Class.new do
193
231
  include Validatable
194
- include_validations_for :child, :if => lambda { true }
232
+ include_errors_from :child, :if => lambda { true }
195
233
  define_method :child do
196
234
  child_class.new
197
235
  end
@@ -209,7 +247,7 @@ functional_tests do
209
247
  end
210
248
  klass = Class.new do
211
249
  include Validatable
212
- include_validations_for :child, :if => lambda { false }
250
+ include_errors_from :child, :if => lambda { false }
213
251
  define_method :child do
214
252
  child_class.new
215
253
  end
@@ -356,4 +394,44 @@ functional_tests do
356
394
  instance.valid?
357
395
  instance.errors.on(:address)
358
396
  end
397
+
398
+ expect "Mod::Klass/Validatable::ValidatesPresenceOf/name" do
399
+ module Mod
400
+ class Klass
401
+ include Validatable
402
+ validates_presence_of :name
403
+ end
404
+ end
405
+ Mod::Klass.validations.first.key
406
+ end
407
+
408
+ expect "/Validatable::ValidatesPresenceOf/custom key" do
409
+ klass = Class.new do
410
+ include Validatable
411
+ validates_presence_of :name, :key => "custom key"
412
+ end
413
+ klass.validations.first.key
414
+ end
415
+
416
+ expect "can't be empty" do
417
+ klass = Class.new do
418
+ include Validatable
419
+ validates_presence_of :name, :address
420
+ attr_accessor :name, :address
421
+ end
422
+ instance = klass.new
423
+ instance.validate_only("presence_of/name")
424
+ instance.errors.on(:name)
425
+ end
426
+
427
+ expect nil do
428
+ klass = Class.new do
429
+ include Validatable
430
+ validates_presence_of :name, :address
431
+ attr_accessor :name, :address
432
+ end
433
+ instance = klass.new
434
+ instance.validate_only("presence_of/name")
435
+ instance.errors.on(:address)
436
+ end
359
437
  end
@@ -7,12 +7,6 @@ require 'set'
7
7
  require File.dirname(__FILE__) + '/../lib/validatable'
8
8
 
9
9
  class << Test::Unit::TestCase
10
- def test(name, &block)
11
- test_name = :"test_#{name.gsub(' ','_')}"
12
- raise ArgumentError, "#{test_name} is already defined" if self.instance_methods.include? test_name.to_s
13
- define_method test_name, &block
14
- end
15
-
16
10
  def expect(expected_value, &block)
17
11
  define_method :"test_#{caller.first.split("/").last}" do
18
12
  begin
@@ -2,20 +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 stub, :acceptance
5
+ validation = Validatable::ValidatesAcceptanceOf.new stub_everything, :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 stub, :acceptance
11
+ validation = Validatable::ValidatesAcceptanceOf.new stub_everything, :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
17
  options = {:message => nil, :if => nil, :times => nil, :level => nil, :groups => nil}
18
- Validatable::ValidatesAcceptanceOf.new(stub, :test).must_understand(options)
18
+ Validatable::ValidatesAcceptanceOf.new(stub_everything, :test).must_understand(options)
19
19
  end
20
20
 
21
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 stub, :username
5
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :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 stub, :username
11
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :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 stub, :username, :case_sensitive => false
17
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :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 stub, :username, :case_sensitive => true
23
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :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 stub, :username, :case_sensitive => true
29
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :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 stub, :username, :case_sensitive => true
34
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :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 stub, :username, :case_sensitive => true
39
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :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 stub, :username, :case_sensitive => false
44
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :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 stub, :username, :case_sensitive => false
49
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :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 stub, :username, :case_sensitive => false
54
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :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(stub, :test).must_understand(options)
60
+ Validatable::ValidatesConfirmationOf.new(stub_everything, :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 stub, :name, :with => /book/
5
+ validation = Validatable::ValidatesFormatOf.new stub_everything, :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 stub, :name, :with => /book/
10
+ validation = Validatable::ValidatesFormatOf.new stub_everything, :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 stub, :age, :with => /14/
15
+ validation = Validatable::ValidatesFormatOf.new stub_everything, :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 stub, :age
21
+ validation = Validatable::ValidatesFormatOf.new stub_everything, :age
22
22
  end
23
23
  end
24
24
 
25
25
  expect true do
26
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)
27
+ Validatable::ValidatesFormatOf.new(stub_everything, :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(stub, :name, options.to_blank_options_hash).requires(options.to_blank_options_hash)
32
+ Validatable::ValidatesFormatOf.new(stub_everything, :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 stub, :username, :maximum => 8
6
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :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 stub, :username, :minimum => 2
11
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :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 stub, :username, :minimum => 2, :maximum => 8
17
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :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 stub, :username, :is => 2
23
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :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 stub, :username, :is => 2
29
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :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 stub, :username, :within => 2..4
35
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :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 stub, :username, :within => 2..4
41
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :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 stub, :username, :within => 2..4
47
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :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 stub, :username, :within => 2..4
53
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :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 stub, :username, :within => 2..4
59
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :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 stub, :username, :within => 2..4, :allow_nil => true
66
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :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(stub, :test).must_understand(options.to_blank_options_hash)
73
+ Validatable::ValidatesLengthOf.new(stub_everything, :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 stub, :nothing
7
+ validation = Validatable::ValidatesNumericalityOf.new stub_everything, :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 stub, :some_int
13
+ validation = Validatable::ValidatesNumericalityOf.new stub_everything, :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 stub, :some_decimal
19
+ validation = Validatable::ValidatesNumericalityOf.new stub_everything, :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 stub, :some_decimal, :only_integer => true
25
+ validation = Validatable::ValidatesNumericalityOf.new stub_everything, :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 stub, :some_negative_number, :only_integer => true
31
+ validation = Validatable::ValidatesNumericalityOf.new stub_everything, :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 stub, :some_non_numeric
37
+ validation = Validatable::ValidatesNumericalityOf.new stub_everything, :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 stub, :multiple_dots
43
+ validation = Validatable::ValidatesNumericalityOf.new stub_everything, :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(stub, :test).must_understand(options.to_blank_options_hash)
50
+ Validatable::ValidatesNumericalityOf.new(stub_everything, :test).must_understand(options.to_blank_options_hash)
51
51
  end
52
52
 
53
53
  end
@@ -2,23 +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 stub, :name
5
+ validation = Validatable::ValidatesPresenceOf.new stub_everything, :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 stub, :name
10
+ validation = Validatable::ValidatesPresenceOf.new stub_everything, :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 stub, :employee
15
+ validation = Validatable::ValidatesPresenceOf.new stub_everything, :employee
16
16
  assert_equal true, validation.valid?(stub(:employee => stub(:nil? => false)))
17
17
  end
18
18
 
19
19
  expect true do
20
20
  options = {:message => nil, :if => nil, :times => nil, :level => nil, :groups => nil}
21
- Validatable::ValidatesPresenceOf.new(stub, :test).must_understand(options)
21
+ Validatable::ValidatesPresenceOf.new(stub_everything, :test).must_understand(options)
22
22
  end
23
23
 
24
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 stub, :name, :logic => lambda { false }
5
+ validation = Validatable::ValidatesTrueFor.new stub_everything, :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 stub, :name, :logic => lambda { true }
10
+ validation = Validatable::ValidatesTrueFor.new stub_everything, :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 stub, :age
16
+ validation = Validatable::ValidatesTrueFor.new stub_everything, :age
17
17
  end
18
18
  end
19
19
 
20
20
  expect true do
21
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)
22
+ Validatable::ValidatesTrueFor.new(stub_everything, :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(stub, :name, options.to_blank_options_hash).requires(options.to_blank_options_hash)
27
+ Validatable::ValidatesTrueFor.new(stub_everything, :name, options.to_blank_options_hash).requires(options.to_blank_options_hash)
28
28
  end
29
29
 
30
30
  end
@@ -2,7 +2,7 @@ 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 stub, :base
5
+ validation = Validatable::ValidationBase.new stub_everything, :base
6
6
  validation.should_validate? Object.new
7
7
  end
8
8
 
@@ -12,7 +12,7 @@ class ValidationBaseTest < Test::Unit::TestCase
12
12
  end
13
13
 
14
14
  expect true do
15
- validation = Validatable::ValidationBase.new stub, :base
15
+ validation = Validatable::ValidationBase.new stub_everything, :base
16
16
  validation.validate_this_time?(nil)
17
17
  end
18
18
 
@@ -21,16 +21,6 @@ class ValidationBaseTest < Test::Unit::TestCase
21
21
  validation.validate_this_time?(stub(:times_validated => 1))
22
22
  end
23
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
-
34
24
  expect false do
35
25
  validation = Validatable::ValidationBase.new stub_everything, :base, :times => 1
36
26
  validation.validate_this_time?(stub(:times_validated => 1))
@@ -42,21 +32,21 @@ class ValidationBaseTest < Test::Unit::TestCase
42
32
  end
43
33
 
44
34
  expect ArgumentError do
45
- Validatable::ValidationBase.new stub(:validation_keys_include? => true), :base, :times => 1
35
+ Validatable::ValidationBase.new stub_everything(:validation_keys_include? => true), :base, :times => 1
46
36
  end
47
37
 
48
38
  expect "some message 100" do
49
- validation = Validatable::ValidationBase.new stub, :base, :message => lambda { "some message #{a_method}" }
39
+ validation = Validatable::ValidationBase.new stub_everything, :base, :message => lambda { "some message #{a_method}" }
50
40
  validation.message(stub(:a_method=>'100'))
51
41
  end
52
42
 
53
43
  expect ArgumentError do
54
- Validatable::ValidationBase.new(stub, :base).must_understand(:foo => 1, :bar => 2)
44
+ Validatable::ValidationBase.new(stub_everything, :base).must_understand(:foo => 1, :bar => 2)
55
45
  end
56
46
 
57
47
  expect true do
58
48
  options = {:message => nil, :if => nil, :times => nil, :level => nil, :groups => nil, :key => nil}
59
- Validatable::ValidationBase.new(stub, :base).must_understand(options)
49
+ Validatable::ValidationBase.new(stub_everything, :base).must_understand(options)
60
50
  end
61
51
 
62
52
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: validatable
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.6.4
7
- date: 2007-08-15 00:00:00 -04:00
6
+ version: 1.6.6
7
+ date: 2007-11-04 00:00:00 -04:00
8
8
  summary: Validatable is a library for adding validations.
9
9
  require_paths:
10
10
  - lib
@@ -31,16 +31,14 @@ authors:
31
31
  files:
32
32
  - lib/child_validation.rb
33
33
  - lib/errors.rb
34
+ - lib/included_validation.rb
34
35
  - lib/macros.rb
35
36
  - lib/object_extension.rb
36
37
  - lib/requireable.rb
37
38
  - lib/understandable.rb
38
39
  - lib/validatable.rb
39
- - lib/validatable_assertions.rb
40
40
  - lib/validatable_class_methods.rb
41
41
  - lib/validatable_instance_methods.rb
42
- - lib/validation_assertion.rb
43
- - lib/validation_assertion_collector.rb
44
42
  - lib/validations/validates_acceptance_of.rb
45
43
  - lib/validations/validates_confirmation_of.rb
46
44
  - lib/validations/validates_each.rb
@@ -51,8 +49,6 @@ files:
51
49
  - lib/validations/validates_true_for.rb
52
50
  - lib/validations/validation_base.rb
53
51
  - test/all_tests.rb
54
- - test/test_helper.rb
55
- - test/functional/validatable_assertions_test.rb
56
52
  - test/functional/validatable_test.rb
57
53
  - test/functional/validates_acceptance_of_test.rb
58
54
  - test/functional/validates_confirmation_of_test.rb
@@ -62,7 +58,7 @@ files:
62
58
  - test/functional/validates_numericality_of_test.rb
63
59
  - test/functional/validates_presence_of_test.rb
64
60
  - test/functional/validates_true_for_test.rb
65
- - test/functional/validation_assertion_collector_test.rb
61
+ - test/test_helper.rb
66
62
  - test/unit/errors_test.rb
67
63
  - test/unit/understandable_test.rb
68
64
  - test/unit/validatable_test.rb
@@ -1,77 +0,0 @@
1
- module Validatable
2
- module Assertions
3
-
4
- def create_message_for_assertion(assertion) #:nodoc:
5
- message = "#{assertion.klass} does not contain a #{assertion.validation_type} for #{assertion.attribute}"
6
- message += " with options #{assertion.options.inspect}" unless assertion.options == {}
7
- message
8
- end
9
-
10
- def create_backtrace #:nodoc:
11
- backtrace = caller
12
- backtrace.shift
13
- backtrace.shift
14
- backtrace
15
- end
16
-
17
- def validation_matching_proc(assertion) #:nodoc:
18
- lambda do |validation|
19
- result = assertion.validation_type === validation
20
- result &&= assertion.attribute == validation.attribute
21
- assertion.options.each_pair do |key, value|
22
- validation_value = validation.send key if validation.respond_to? key
23
- result = false if validation_value.nil?
24
- result &&= validation_value == value
25
- end
26
- result
27
- end
28
- end
29
-
30
- def self.included(klass) #:nodoc:
31
- Test::Unit::TestCase.class_eval do
32
- def self.create_test_name(assertion) #:nodoc:
33
- "test#{assertion.validation_type.to_s.gsub(/Validatable::/,'').gsub(/([A-Z])/, '_\1').downcase}_#{assertion.attribute}"
34
- end
35
-
36
- def self.define_test_method name, &block #:nodoc:
37
- class_eval do
38
- define_method name, &block
39
- end
40
- end
41
- end
42
-
43
- Class.class_eval do
44
- # call-seq: must_validate
45
- #
46
- # class FooTest < Test::Unit::TestCase
47
- # include Validatable::Assertions
48
- #
49
- # Foo.must_validate do
50
- # presence_of :name
51
- # format_of(:name).with(/^[A-Z]/)
52
- # numericality_of(:age).only_integer(true)
53
- # end
54
- # end
55
- #
56
- # The above code creates a test for each line in the block given to must_validate.
57
- # If the Foo class does not contain a presence of validation for name,
58
- # an error with the text "Foo does not contain a Validatable::ValidatesPresenceOf for name" will be raised.
59
- #
60
- # Clearly this solution has limitations. Any validates_true_for validation cannot be tested using
61
- # this DSL style of testing. Furthermore, any validation that uses an :if argument cannot use this DSL,
62
- # since those validations require an instance to eval the :if argument against. However, for validations
63
- # that are not validates_true_for and do not rely on an :if argument, the ValidatableAssertions can
64
- # replace various existing success and failure validation tests.
65
- def must_validate(&block)
66
- test_class = eval "self", block.binding
67
- ValidationAssertionCollector.gather(self, &block).each do |assertion|
68
- test_class.define_test_method test_class.create_test_name(assertion) do
69
- validation = assertion.klass.validations.find &validation_matching_proc(assertion)
70
- add_failure create_message_for_assertion(assertion), create_backtrace if validation.nil?
71
- end
72
- end
73
- end
74
- end
75
- end
76
- end
77
- end
@@ -1,7 +0,0 @@
1
- class ValidationAssertion #:nodoc:
2
- attr_accessor :klass, :validation_type, :options, :attribute
3
-
4
- def initialize(klass, validation_type, attribute)
5
- self.klass, self.validation_type, self.attribute, self.options = klass, validation_type, attribute, {}
6
- end
7
- end
@@ -1,43 +0,0 @@
1
- class ValidationAssertionCollector #:nodoc:
2
- def self.gather(klass, &block)
3
- collector = self.new(klass)
4
- collector.instance_eval(&block)
5
- collector.assertions
6
- end
7
-
8
- def self.define_method_for_validation_method(validation_method, validatable_class)
9
- define_method validation_method.gsub(/^validates_/,'').to_sym do |attribute|
10
- assertions << ValidationAssertion.new(self.klass, validatable_class, attribute)
11
- define_methods_on_assertion_to_collect_options validatable_class, assertions.last
12
- end
13
- end
14
-
15
- Validatable::Macros.public_instance_methods.sort.grep(/^validates_/).each do |validation_method|
16
- next if validation_method == 'validates_true_for'
17
- validatable_class = Validatable.const_get(validation_method.split(/_/).collect { |word| word.capitalize}.join)
18
- define_method_for_validation_method(validation_method, validatable_class)
19
- end
20
-
21
- attr_accessor :klass
22
-
23
- def initialize(klass)
24
- self.klass = klass
25
- end
26
-
27
- def define_methods_on_assertion_to_collect_options(validatable_class, assertion)
28
- validatable_class.all_understandings.each do |option|
29
- next if option == :if
30
- class << assertion; self; end.instance_eval do
31
- define_method option do |value|
32
- self.options.merge!(option=>value)
33
- self
34
- end
35
- end
36
- end
37
- assertion
38
- end
39
-
40
- def assertions
41
- @assertions ||= []
42
- end
43
- end
@@ -1,98 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
-
3
- class Validatable::AssertionsTest < Test::Unit::TestCase
4
- expect "Klass does not contain a ValidationType for Attribute" do
5
- klass = Class.new do
6
- include Validatable::Assertions
7
- end
8
- klass.new.create_message_for_assertion(stub(:klass=>"Klass", :validation_type=>"ValidationType", :attribute=>"Attribute", :options=>{}))
9
- end
10
-
11
- expect "Klass does not contain a ValidationType for Attribute" do
12
- klass = Class.new do
13
- include Validatable::Assertions
14
- end
15
- klass.new.create_message_for_assertion(stub(:klass=>"Klass", :validation_type=>"ValidationType", :attribute=>"Attribute", :options=>{}))
16
- end
17
-
18
- expect false do
19
- klass = Class.new do
20
- include Validatable::Assertions
21
- end
22
- assertion = stub(:validation_type=>stub(:=== => true), :attribute => "attribute", :options=>{:level => 1, :times => 2})
23
- validation = stub(:validation_type=>"validation_type", :attribute => "attribute", :level => 2, :times => 2)
24
- klass.new.validation_matching_proc(assertion).call(validation)
25
- end
26
-
27
- expect false do
28
- klass = Class.new do
29
- include Validatable::Assertions
30
- end
31
- assertion = stub(:validation_type=>stub(:=== => true), :attribute => "non matching attribute", :options=>{})
32
- validation = stub(:validation_type=>"validation_type", :attribute => nil)
33
- klass.new.validation_matching_proc(assertion).call(validation)
34
- end
35
-
36
- expect false do
37
- klass = Class.new do
38
- include Validatable::Assertions
39
- end
40
- assertion = stub(:validation_type=>"non matching validation_type", :options=>{})
41
- validation = stub(:validation_type=>"validation_type")
42
- klass.new.validation_matching_proc(assertion).call(validation)
43
- end
44
-
45
- expect true do
46
- klass = Class.new do
47
- include Validatable::Assertions
48
- end
49
- assertion = stub(:validation_type=>stub(:=== => true), :attribute => "attribute", :options=>{:level => 1, :times => 2})
50
- validation = stub(:validation_type=>"validation_type", :attribute => "attribute", :level => 1, :times => 2)
51
- klass.new.validation_matching_proc(assertion).call(validation)
52
- end
53
-
54
- expect true do
55
- Class.new do
56
- include Validatable::Assertions
57
- end
58
-
59
- Class.respond_to? :must_validate
60
- end
61
-
62
- expect "test_validates_presence_of_name" do
63
- test_class = Class.new(Test::Unit::TestCase) do
64
- include Validatable::Assertions
65
- end
66
- test_class.create_test_name(stub(:validation_type=>Validatable::ValidatesPresenceOf, :attribute=>:name))
67
- end
68
-
69
- expect true do
70
- test_class = Class.new(Test::Unit::TestCase) do
71
- include Validatable::Assertions
72
- end
73
-
74
- test_class.define_test_method :some_test do
75
- true
76
- end
77
-
78
- test_class.instance_methods.include? "some_test"
79
- end
80
-
81
- expect true do
82
- klass = Class.new do
83
- include Validatable
84
- end
85
- test_class = Class.new Test::Unit::TestCase do
86
- include Validatable::Assertions
87
-
88
- klass.must_validate do
89
- presence_of :anything
90
- end
91
-
92
- end
93
-
94
- test_class.any_instance.expects(:add_failure)
95
- test_class.new(:test_validates_presence_of_anything).test_validates_presence_of_anything
96
- true
97
- end
98
- end
@@ -1,49 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
-
3
- class ValidationAssertionCollectorTest < Test::Unit::TestCase
4
- expect 1 do
5
- assertions = ValidationAssertionCollector.gather(Class.new) do
6
- presence_of :name
7
- end
8
- assertions.size
9
- end
10
-
11
- expect "save this message" do
12
- assertions = ValidationAssertionCollector.gather(Class.new) do
13
- presence_of(:name).message("ignore this message").message("save this message")
14
- end
15
- assertions.first.options[:message]
16
- end
17
-
18
- expect :class_being_validated do
19
- assertions = ValidationAssertionCollector.gather(:class_being_validated) do
20
- presence_of(:name)
21
- end
22
- assertions.first.klass
23
- end
24
-
25
- expect NoMethodError do
26
- assertions = ValidationAssertionCollector.gather(Class.new) do
27
- presence_of(:name).invalid_option
28
- end
29
- end
30
-
31
- expect NoMethodError do
32
- assertions = ValidationAssertionCollector.gather(Class.new) do
33
- true_for(:name)
34
- end
35
- end
36
-
37
- expect NoMethodError do
38
- assertions = ValidationAssertionCollector.gather(Class.new) do
39
- presence_of(:name).if lambda { true }
40
- end
41
- end
42
-
43
- expect Validatable::ValidatesPresenceOf do
44
- assertions = ValidationAssertionCollector.gather(Class.new) do
45
- presence_of(:name)
46
- end
47
- assertions.first.validation_type
48
- end
49
- end