validatable 1.1.0 → 1.1.1

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
@@ -16,79 +16,61 @@ You may use, copy and redistribute this library under the same terms as Ruby its
16
16
 
17
17
  == Examples
18
18
 
19
- === Test Helper
20
-
21
- require 'test/unit'
22
- require 'rubygems'
23
- require 'mocha'
24
- require File.dirname(__FILE__) + '/../lib/validatable'
25
-
26
- class << Test::Unit::TestCase
27
- def test(name, &block)
28
- test_name = :"test_#{name.gsub(' ','_')}"
29
- raise ArgumentError, "#{test_name} is already defined" if self.instance_methods.include? test_name.to_s
30
- define_method test_name, &block
31
- end
32
-
33
- def expect(expected_value, &block)
34
- define_method :"test_#{caller.first.split("/").last}" do
35
- assert_equal expected_value, instance_eval(&block)
36
- end
37
- end
38
- end
39
-
40
- === Validations
41
-
42
- require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
43
-
44
- module Functional
45
- class ValidatesPresenceOfTest < Test::Unit::TestCase
46
- test "given no name, when validated, then error is in the objects error collection" do
47
- klass = Class.new do
48
- include Validatable
49
- attr_accessor :name
50
- validates_presence_of :name
51
- end
52
- instance = klass.new
53
- instance.valid?
54
- assert_equal "can't be empty", instance.errors.on(:name)
55
- end
56
- end
57
- end
58
-
59
- require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
60
-
61
- module Functional
62
- class ValidatesFormatOfTest < Test::Unit::TestCase
63
- test "given invalid name format, when validated, then error is in the objects error collection" do
64
- klass = Class.new do
65
- include Validatable
66
- attr_accessor :name
67
- validates_format_of :name, :with => /.+/
68
- end
69
- instance = klass.new
70
- instance.valid?
71
- assert_equal "is invalid", instance.errors.on(:name)
72
- end
73
-
74
- test "given invalid name format and nil name, when validated, then error is in the objects error collection" do
75
- klass = Class.new do
76
- include Validatable
77
- attr_accessor :name
78
- validates_format_of :name, :with => /.+/, :if => Proc.new { !name.nil? }
79
- end
80
- assert_equal true, klass.new.valid?
81
- end
82
-
83
- test "given invalid name format and a name, when validated, then error is in the objects error collection" do
84
- klass = Class.new do
85
- include Validatable
86
- attr_accessor :name
87
- validates_format_of :name, :with => /.+/, :if => Proc.new { name.nil? }
88
- end
89
- assert_equal false, klass.new.valid?
90
- end
91
- end
92
- end
19
+ Validation of an entire hierarchy of objects with errors aggregated at the root object.
20
+
21
+ class Person
22
+ include Validatable
23
+ validates_presence_of :name
24
+ attr_accessor :name
25
+ end
26
+
27
+ class PersonPresenter
28
+ include Validatable
29
+ include_validations_for :person
30
+ attr_accessor :person
31
+
32
+ def initialize(person)
33
+ @person = person
34
+ end
35
+ end
36
+
37
+ presenter = PersonPresenter.new(Person.new)
38
+ presenter.valid? #=> false
39
+ presenter.errors.on(:name) #=> "can't be blank"
40
+
41
+ Validations that turn off after X times of failed attempts.
42
+
43
+ class Person
44
+ include Validatable
45
+ validates_presence_of :name, :times => 1
46
+ attr_accessor :name
47
+ end
48
+
49
+ person = Person.new
50
+ person.valid? #=> false
51
+ person.valid? #=> true
52
+
53
+ Validations can be given levels. If a validation fails on a level the validations for subsequent levels will not be executed.
54
+
55
+ class Person
56
+ include Validatable
57
+ validates_presence_of :name, :level => 1, :message => "name message"
58
+ validates_presence_of :address, :level => 2
59
+ attr_accessor :name, :address
60
+ end
61
+
62
+ person = Person.new
63
+ person.valid? #=> false
64
+ person.errors.on(:name) #=> "name message"
65
+ person.errors.on(:address) #=> nil
66
+
67
+ Similar to Rails, Validatable also supports conditional validation.
68
+
69
+ class Person
70
+ include Validatable
71
+ attr_accessor :name
72
+ validates_format_of :name, :with => /.+/, :if => Proc.new { !name.nil? }
73
+ end
74
+ Person.new.valid? #=> true
93
75
 
94
76
  See the tests for more examples
@@ -10,6 +10,14 @@ module Validatable
10
10
  # end
11
11
  #
12
12
  # A regular expression must be provided or else an exception will be raised.
13
+ #
14
+ # Configuration options:
15
+ #
16
+ # * message - The message to add to the errors collection when the validation fails
17
+ # * times - The number of times the validation applies
18
+ # * level - The level at which the validation should occur
19
+ # * if - A block that when executed must return true of the validation will not occur
20
+ # * with - The regular expression used to validate the format
13
21
  def validates_format_of(*args)
14
22
  add_validations(args, ValidatesFormatOf) do |validation, options|
15
23
  validation.with = options[:with]
@@ -28,6 +36,10 @@ module Validatable
28
36
  #
29
37
  # Configuration options:
30
38
  #
39
+ # * message - The message to add to the errors collection when the validation fails
40
+ # * times - The number of times the validation applies
41
+ # * level - The level at which the validation should occur
42
+ # * if - A block that when executed must return true of the validation will not occur
31
43
  # * minimum - The minimum size of the attribute
32
44
  # * maximum - The maximum size of the attribute
33
45
  def validates_length_of(*args)
@@ -46,6 +58,13 @@ module Validatable
46
58
  # validates_acceptance_of :terms_of_service
47
59
  # validates_acceptance_of :eula, :message => "must be abided"
48
60
  # end
61
+ #
62
+ # Configuration options:
63
+ #
64
+ # * message - The message to add to the errors collection when the validation fails
65
+ # * times - The number of times the validation applies
66
+ # * level - The level at which the validation should occur
67
+ # * if - A block that when executed must return true of the validation will not occur
49
68
  def validates_acceptance_of(*args)
50
69
  add_validations(args, ValidatesAcceptanceOf)
51
70
  end
@@ -64,6 +83,13 @@ module Validatable
64
83
  # View:
65
84
  # <%= password_field "person", "password" %>
66
85
  # <%= password_field "person", "password_confirmation" %>
86
+ #
87
+ # Configuration options:
88
+ #
89
+ # * message - The message to add to the errors collection when the validation fails
90
+ # * times - The number of times the validation applies
91
+ # * level - The level at which the validation should occur
92
+ # * if - A block that when executed must return true of the validation will not occur
67
93
  def validates_confirmation_of(*args)
68
94
  add_validations(args, ValidatesConfirmationOf)
69
95
  end
@@ -78,6 +104,13 @@ module Validatable
78
104
  # end
79
105
  #
80
106
  # The first_name attribute must be in the object and it cannot be nil or empty.
107
+ #
108
+ # Configuration options:
109
+ #
110
+ # * message - The message to add to the errors collection when the validation fails
111
+ # * times - The number of times the validation applies
112
+ # * level - The level at which the validation should occur
113
+ # * if - A block that when executed must return true of the validation will not occur
81
114
  def validates_presence_of(*args)
82
115
  add_validations(args, ValidatesPresenceOf)
83
116
  end
@@ -85,6 +118,11 @@ module Validatable
85
118
  # call-seq: include_validations_for(*args)
86
119
  #
87
120
  # Validates the specified attributes.
121
+ # class Person
122
+ # include Validatable
123
+ # validates_presence_of :name
124
+ # attr_accessor :name
125
+ # end
88
126
  #
89
127
  # class PersonPresenter
90
128
  # include Validatable
@@ -95,6 +133,10 @@ module Validatable
95
133
  # @person = person
96
134
  # end
97
135
  # end
136
+ #
137
+ # presenter = PersonPresenter.new(Person.new)
138
+ # presenter.valid? #=> false
139
+ # presenter.errors.on(:name) #=> "can't be blank"
98
140
  #
99
141
  # The person attribute will be validated. If person is invalid the errors will be added to the PersonPresenter errors collection.
100
142
  def include_validations_for(*args)
@@ -123,6 +165,10 @@ module Validatable
123
165
  end
124
166
  end
125
167
 
168
+ def validations #:nodoc:
169
+ @validations ||= []
170
+ end
171
+
126
172
  protected
127
173
  def add_validations(args, klass) #:nodoc:
128
174
  options = args.last.is_a?(Hash) ? args.pop : {}
@@ -136,10 +182,6 @@ module Validatable
136
182
  def children_to_validate #:nodoc:
137
183
  @children_to_validate ||= []
138
184
  end
139
-
140
- def validations #:nodoc:
141
- @validations ||= []
142
- end
143
185
  end
144
186
 
145
187
  def self.included(klass) #:nodoc:
@@ -151,8 +193,8 @@ module Validatable
151
193
  # Returns true if no errors were added otherwise false.
152
194
  def valid?
153
195
  errors.clear
154
- self.class.validate(self)
155
196
  self.class.validate_children(self)
197
+ self.validate
156
198
  errors.empty?
157
199
  end
158
200
 
@@ -162,4 +204,28 @@ module Validatable
162
204
  def errors
163
205
  @errors ||= Validatable::Errors.new
164
206
  end
207
+
208
+ protected
209
+ def validate #:nodoc:
210
+ validation_levels.sort.each do |level|
211
+ validations_for_level(level).each do |validation|
212
+ if validation.should_validate?(self)
213
+ self.errors.add(validation.attribute, validation.message) unless validation.valid?(self)
214
+ end
215
+ end
216
+ return unless self.errors.empty?
217
+ end
218
+ end
219
+
220
+ def validation_levels #:nodoc:
221
+ self.validations.collect { |validation| validation.level }.uniq
222
+ end
223
+
224
+ def validations_for_level(level) #:nodoc:
225
+ self.validations.select { |validation| validation.level == level }
226
+ end
227
+
228
+ def validations #:nodoc:
229
+ @validations ||= self.class.validations.collect { |validation| validation.dup }
230
+ end
165
231
  end
@@ -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.1.0"
32
+ s.version = "1.1.1"
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'
@@ -33,6 +33,18 @@ module Functional
33
33
  instance.valid?
34
34
  end
35
35
 
36
+ expect false do
37
+ klass = Class.new do
38
+ include Validatable
39
+ validates_presence_of :name, :times => 1
40
+ attr_accessor :name
41
+ end
42
+ instance1 = klass.new
43
+ instance1.valid?
44
+ instance2 = klass.new
45
+ instance2.valid?
46
+ end
47
+
36
48
  expect "name message" do
37
49
  klass = Class.new do
38
50
  include Validatable
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.8.11
2
+ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: validatable
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.1.0
7
- date: 2007-02-11 00:00:00 -05:00
6
+ version: 1.1.1
7
+ date: 2007-02-13 00:00:00 -05:00
8
8
  summary: Validatable is a library for adding validations.
9
9
  require_paths:
10
10
  - lib
@@ -25,6 +25,7 @@ required_ruby_version: !ruby/object:Gem::Version::Requirement
25
25
  platform: ruby
26
26
  signing_key:
27
27
  cert_chain:
28
+ post_install_message:
28
29
  authors:
29
30
  - Jay Fields
30
31
  files: