dm-validations 0.10.1 → 0.10.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +23 -18
- data/VERSION +1 -0
- data/dm-validations.gemspec +188 -0
- data/lib/dm-validations.rb +25 -75
- data/lib/dm-validations/auto_validate.rb +7 -7
- data/lib/dm-validations/contextual_validators.rb +4 -18
- data/lib/dm-validations/exceptions.rb +3 -1
- data/lib/dm-validations/support/context.rb +40 -1
- data/lib/dm-validations/validation_errors.rb +2 -13
- data/lib/dm-validations/validators/absent_field_validator.rb +2 -2
- data/lib/dm-validations/validators/acceptance_validator.rb +14 -16
- data/lib/dm-validations/validators/confirmation_validator.rb +17 -15
- data/lib/dm-validations/validators/format_validator.rb +18 -11
- data/lib/dm-validations/validators/generic_validator.rb +45 -15
- data/lib/dm-validations/validators/length_validator.rb +2 -11
- data/lib/dm-validations/validators/method_validator.rb +2 -2
- data/lib/dm-validations/validators/numeric_validator.rb +8 -6
- data/lib/dm-validations/validators/primitive_validator.rb +1 -1
- data/lib/dm-validations/validators/uniqueness_validator.rb +13 -9
- data/lib/dm-validations/validators/within_validator.rb +4 -3
- data/spec/fixtures/basketball_player.rb +0 -3
- data/spec/fixtures/bill_of_landing.rb +1 -1
- data/spec/fixtures/city.rb +0 -1
- data/spec/fixtures/company.rb +1 -3
- data/spec/fixtures/corporate_world.rb +0 -6
- data/spec/fixtures/country.rb +0 -1
- data/spec/fixtures/event.rb +1 -1
- data/spec/fixtures/page.rb +2 -2
- data/spec/fixtures/reservation.rb +1 -1
- data/spec/fixtures/scm_operation.rb +0 -8
- data/spec/integration/absent_field_validator/absent_field_validator_spec.rb +6 -2
- data/spec/integration/acceptance_validator/acceptance_validator_spec.rb +36 -36
- data/spec/integration/automatic_validation/custom_messages_for_inferred_validation_spec.rb +2 -2
- data/spec/integration/automatic_validation/disabling_inferred_validation_spec.rb +4 -4
- data/spec/integration/automatic_validation/inferred_boolean_properties_validation_spec.rb +2 -2
- data/spec/integration/automatic_validation/inferred_float_property_validation_spec.rb +2 -0
- data/spec/integration/automatic_validation/inferred_format_validation_spec.rb +3 -1
- data/spec/integration/automatic_validation/inferred_integer_properties_validation_spec.rb +2 -0
- data/spec/integration/automatic_validation/inferred_length_validation_spec.rb +6 -2
- data/spec/integration/automatic_validation/inferred_presence_validation_spec.rb +8 -4
- data/spec/integration/automatic_validation/inferred_primitive_validation_spec.rb +3 -1
- data/spec/integration/automatic_validation/spec_helper.rb +6 -6
- data/spec/integration/block_validator/block_validator_spec.rb +1 -1
- data/spec/integration/conditional_validation/if_condition_spec.rb +3 -1
- data/spec/integration/confirmation_validator/confirmation_validator_spec.rb +3 -1
- data/spec/integration/datamapper_models/association_validation_spec.rb +4 -1
- data/spec/integration/datamapper_models/inheritance_spec.rb +6 -2
- data/spec/integration/duplicated_validations/duplicated_validations_spec.rb +3 -1
- data/spec/integration/format_validator/email_format_validator_spec.rb +5 -1
- data/spec/integration/format_validator/format_validator_spec.rb +5 -1
- data/spec/integration/format_validator/regexp_validator_spec.rb +5 -1
- data/spec/integration/format_validator/url_format_validator_spec.rb +8 -4
- data/spec/integration/length_validator/default_value_spec.rb +4 -2
- data/spec/integration/length_validator/equality_spec.rb +3 -1
- data/spec/integration/length_validator/error_message_spec.rb +3 -1
- data/spec/integration/length_validator/maximum_spec.rb +3 -1
- data/spec/integration/length_validator/minimum_spec.rb +3 -1
- data/spec/integration/length_validator/range_spec.rb +3 -1
- data/spec/integration/method_validator/method_validator_spec.rb +3 -1
- data/spec/integration/numeric_validator/equality_with_float_type_spec.rb +5 -1
- data/spec/integration/numeric_validator/equality_with_integer_type_spec.rb +5 -1
- data/spec/integration/numeric_validator/float_type_spec.rb +3 -1
- data/spec/integration/numeric_validator/gt_with_float_type_spec.rb +3 -1
- data/spec/integration/numeric_validator/gte_with_float_type_spec.rb +3 -1
- data/spec/integration/numeric_validator/integer_only_true_spec.rb +11 -9
- data/spec/integration/numeric_validator/integer_type_spec.rb +10 -8
- data/spec/integration/numeric_validator/lt_with_float_type_spec.rb +3 -1
- data/spec/integration/numeric_validator/lte_with_float_type_spec.rb +3 -1
- data/spec/integration/primitive_validator/primitive_validator_spec.rb +3 -1
- data/spec/integration/pure_ruby_objects/plain_old_ruby_object_validation_spec.rb +6 -6
- data/spec/integration/required_field_validator/association_spec.rb +9 -4
- data/spec/integration/required_field_validator/boolean_type_value_spec.rb +24 -18
- data/spec/integration/required_field_validator/date_type_value_spec.rb +21 -19
- data/spec/integration/required_field_validator/datetime_type_value_spec.rb +21 -19
- data/spec/integration/required_field_validator/float_type_value_spec.rb +21 -19
- data/spec/integration/required_field_validator/integer_type_value_spec.rb +17 -15
- data/spec/integration/required_field_validator/shared_examples.rb +5 -3
- data/spec/integration/required_field_validator/string_type_value_spec.rb +20 -16
- data/spec/integration/required_field_validator/text_type_value_spec.rb +10 -6
- data/spec/integration/uniqueness_validator/uniqueness_validator_spec.rb +23 -25
- data/spec/integration/within_validator/within_validator_spec.rb +7 -3
- data/spec/public/resource_spec.rb +5 -6
- data/spec/rcov.opts +6 -0
- data/spec/spec.opts +2 -0
- data/spec/unit/contextual_validators/emptiness_spec.rb +1 -1
- data/spec/unit/contextual_validators/execution_spec.rb +9 -16
- data/spec/unit/generic_validator/equality_operator_spec.rb +3 -3
- data/spec/unit/validation_errors/adding_spec.rb +1 -1
- data/spec/unit/validation_errors/emptiness_spec.rb +1 -1
- data/spec/unit/validation_errors/enumerable_spec.rb +1 -1
- data/tasks/ci.rake +1 -0
- data/tasks/metrics.rake +36 -0
- data/tasks/spec.rake +41 -0
- data/tasks/yard.rake +9 -0
- data/tasks/yardstick.rake +19 -0
- metadata +36 -18
- data/History.rdoc +0 -45
- data/Manifest.txt +0 -139
- data/TODO +0 -16
- data/lib/dm-validations/version.rb +0 -5
- data/tasks/install.rb +0 -13
- data/tasks/spec.rb +0 -25
data/Rakefile
CHANGED
@@ -1,24 +1,29 @@
|
|
1
|
-
require '
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
SUDO = (WINDOWS || JRUBY) ? '' : ('sudo' unless ENV['SUDOLESS'])
|
4
|
+
begin
|
5
|
+
gem 'jeweler', '~> 1.4'
|
6
|
+
require 'jeweler'
|
7
7
|
|
8
|
-
|
8
|
+
Jeweler::Tasks.new do |gem|
|
9
|
+
gem.name = 'dm-validations'
|
10
|
+
gem.summary = 'Library for performing validations on DM models and pure Ruby object'
|
11
|
+
gem.description = gem.summary
|
12
|
+
gem.email = 'vandenberg.guy [a] gmail [d] com'
|
13
|
+
gem.homepage = 'http://github.com/datamapper/dm-more/tree/master/%s' % gem.name
|
14
|
+
gem.authors = [ 'Guy van den Berg' ]
|
9
15
|
|
10
|
-
|
11
|
-
EMAIL = 'vandenberg.guy [a] gmail [d] com'
|
12
|
-
GEM_NAME = 'dm-validations'
|
13
|
-
GEM_VERSION = DataMapper::Validations::VERSION
|
14
|
-
GEM_DEPENDENCIES = [['dm-core', GEM_VERSION]]
|
15
|
-
GEM_CLEAN = %w[ log pkg coverage ]
|
16
|
-
GEM_EXTRAS = { :has_rdoc => true, :extra_rdoc_files => %w[ README.rdoc LICENSE TODO History.rdoc ] }
|
16
|
+
gem.rubyforge_project = 'datamapper'
|
17
17
|
|
18
|
-
|
19
|
-
PROJECT_URL = "http://github.com/datamapper/dm-more/tree/master/#{GEM_NAME}"
|
20
|
-
PROJECT_DESCRIPTION = PROJECT_SUMMARY = 'Library for performing validations on DM models and pure Ruby object'
|
18
|
+
gem.add_dependency 'dm-core', '~> 0.10.2'
|
21
19
|
|
22
|
-
|
23
|
-
|
20
|
+
gem.add_development_dependency 'rspec', '~> 1.2.9'
|
21
|
+
gem.add_development_dependency 'yard', '~> 0.4.0'
|
22
|
+
end
|
23
|
+
|
24
|
+
Jeweler::GemcutterTasks.new
|
25
|
+
|
26
|
+
FileList['tasks/**/*.rake'].each { |task| import task }
|
27
|
+
rescue LoadError
|
28
|
+
puts 'Jeweler (or a dependency) not available. Install it with: gem install jeweler'
|
24
29
|
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.10.2
|
@@ -0,0 +1,188 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{dm-validations}
|
8
|
+
s.version = "0.10.2"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Guy van den Berg"]
|
12
|
+
s.date = %q{2009-12-11}
|
13
|
+
s.description = %q{Library for performing validations on DM models and pure Ruby object}
|
14
|
+
s.email = %q{vandenberg.guy [a] gmail [d] com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
"LICENSE",
|
21
|
+
"README.rdoc",
|
22
|
+
"Rakefile",
|
23
|
+
"VERSION",
|
24
|
+
"dm-validations.gemspec",
|
25
|
+
"lib/dm-validations.rb",
|
26
|
+
"lib/dm-validations/auto_validate.rb",
|
27
|
+
"lib/dm-validations/contextual_validators.rb",
|
28
|
+
"lib/dm-validations/exceptions.rb",
|
29
|
+
"lib/dm-validations/formats/email.rb",
|
30
|
+
"lib/dm-validations/formats/url.rb",
|
31
|
+
"lib/dm-validations/support/context.rb",
|
32
|
+
"lib/dm-validations/support/object.rb",
|
33
|
+
"lib/dm-validations/validation_errors.rb",
|
34
|
+
"lib/dm-validations/validators/absent_field_validator.rb",
|
35
|
+
"lib/dm-validations/validators/acceptance_validator.rb",
|
36
|
+
"lib/dm-validations/validators/block_validator.rb",
|
37
|
+
"lib/dm-validations/validators/confirmation_validator.rb",
|
38
|
+
"lib/dm-validations/validators/format_validator.rb",
|
39
|
+
"lib/dm-validations/validators/generic_validator.rb",
|
40
|
+
"lib/dm-validations/validators/length_validator.rb",
|
41
|
+
"lib/dm-validations/validators/method_validator.rb",
|
42
|
+
"lib/dm-validations/validators/numeric_validator.rb",
|
43
|
+
"lib/dm-validations/validators/primitive_validator.rb",
|
44
|
+
"lib/dm-validations/validators/required_field_validator.rb",
|
45
|
+
"lib/dm-validations/validators/uniqueness_validator.rb",
|
46
|
+
"lib/dm-validations/validators/within_validator.rb",
|
47
|
+
"spec/fixtures/barcode.rb",
|
48
|
+
"spec/fixtures/basketball_court.rb",
|
49
|
+
"spec/fixtures/basketball_player.rb",
|
50
|
+
"spec/fixtures/beta_tester_account.rb",
|
51
|
+
"spec/fixtures/bill_of_landing.rb",
|
52
|
+
"spec/fixtures/boat_dock.rb",
|
53
|
+
"spec/fixtures/city.rb",
|
54
|
+
"spec/fixtures/company.rb",
|
55
|
+
"spec/fixtures/corporate_world.rb",
|
56
|
+
"spec/fixtures/country.rb",
|
57
|
+
"spec/fixtures/currency.rb",
|
58
|
+
"spec/fixtures/ethernet_frame.rb",
|
59
|
+
"spec/fixtures/event.rb",
|
60
|
+
"spec/fixtures/g3_concert.rb",
|
61
|
+
"spec/fixtures/jabberwock.rb",
|
62
|
+
"spec/fixtures/kayak.rb",
|
63
|
+
"spec/fixtures/lernean_hydra.rb",
|
64
|
+
"spec/fixtures/mathematical_function.rb",
|
65
|
+
"spec/fixtures/memory_object.rb",
|
66
|
+
"spec/fixtures/mittelschnauzer.rb",
|
67
|
+
"spec/fixtures/motor_launch.rb",
|
68
|
+
"spec/fixtures/page.rb",
|
69
|
+
"spec/fixtures/phone_number.rb",
|
70
|
+
"spec/fixtures/pirogue.rb",
|
71
|
+
"spec/fixtures/programming_language.rb",
|
72
|
+
"spec/fixtures/reservation.rb",
|
73
|
+
"spec/fixtures/scm_operation.rb",
|
74
|
+
"spec/fixtures/sms_message.rb",
|
75
|
+
"spec/fixtures/udp_packet.rb",
|
76
|
+
"spec/integration/absent_field_validator/absent_field_validator_spec.rb",
|
77
|
+
"spec/integration/absent_field_validator/spec_helper.rb",
|
78
|
+
"spec/integration/acceptance_validator/acceptance_validator_spec.rb",
|
79
|
+
"spec/integration/acceptance_validator/spec_helper.rb",
|
80
|
+
"spec/integration/automatic_validation/custom_messages_for_inferred_validation_spec.rb",
|
81
|
+
"spec/integration/automatic_validation/disabling_inferred_validation_spec.rb",
|
82
|
+
"spec/integration/automatic_validation/inferred_boolean_properties_validation_spec.rb",
|
83
|
+
"spec/integration/automatic_validation/inferred_float_property_validation_spec.rb",
|
84
|
+
"spec/integration/automatic_validation/inferred_format_validation_spec.rb",
|
85
|
+
"spec/integration/automatic_validation/inferred_integer_properties_validation_spec.rb",
|
86
|
+
"spec/integration/automatic_validation/inferred_length_validation_spec.rb",
|
87
|
+
"spec/integration/automatic_validation/inferred_presence_validation_spec.rb",
|
88
|
+
"spec/integration/automatic_validation/inferred_primitive_validation_spec.rb",
|
89
|
+
"spec/integration/automatic_validation/inferred_within_validation_spec.rb",
|
90
|
+
"spec/integration/automatic_validation/spec_helper.rb",
|
91
|
+
"spec/integration/block_validator/block_validator_spec.rb",
|
92
|
+
"spec/integration/block_validator/spec_helper.rb",
|
93
|
+
"spec/integration/conditional_validation/if_condition_spec.rb",
|
94
|
+
"spec/integration/conditional_validation/spec_helper.rb",
|
95
|
+
"spec/integration/confirmation_validator/confirmation_validator_spec.rb",
|
96
|
+
"spec/integration/confirmation_validator/spec_helper.rb",
|
97
|
+
"spec/integration/datamapper_models/association_validation_spec.rb",
|
98
|
+
"spec/integration/datamapper_models/inheritance_spec.rb",
|
99
|
+
"spec/integration/duplicated_validations/duplicated_validations_spec.rb",
|
100
|
+
"spec/integration/duplicated_validations/spec_helper.rb",
|
101
|
+
"spec/integration/format_validator/email_format_validator_spec.rb",
|
102
|
+
"spec/integration/format_validator/format_validator_spec.rb",
|
103
|
+
"spec/integration/format_validator/regexp_validator_spec.rb",
|
104
|
+
"spec/integration/format_validator/spec_helper.rb",
|
105
|
+
"spec/integration/format_validator/url_format_validator_spec.rb",
|
106
|
+
"spec/integration/length_validator/default_value_spec.rb",
|
107
|
+
"spec/integration/length_validator/equality_spec.rb",
|
108
|
+
"spec/integration/length_validator/error_message_spec.rb",
|
109
|
+
"spec/integration/length_validator/maximum_spec.rb",
|
110
|
+
"spec/integration/length_validator/minimum_spec.rb",
|
111
|
+
"spec/integration/length_validator/range_spec.rb",
|
112
|
+
"spec/integration/length_validator/spec_helper.rb",
|
113
|
+
"spec/integration/method_validator/method_validator_spec.rb",
|
114
|
+
"spec/integration/method_validator/spec_helper.rb",
|
115
|
+
"spec/integration/numeric_validator/equality_with_float_type_spec.rb",
|
116
|
+
"spec/integration/numeric_validator/equality_with_integer_type_spec.rb",
|
117
|
+
"spec/integration/numeric_validator/float_type_spec.rb",
|
118
|
+
"spec/integration/numeric_validator/gt_with_float_type_spec.rb",
|
119
|
+
"spec/integration/numeric_validator/gte_with_float_type_spec.rb",
|
120
|
+
"spec/integration/numeric_validator/integer_only_true_spec.rb",
|
121
|
+
"spec/integration/numeric_validator/integer_type_spec.rb",
|
122
|
+
"spec/integration/numeric_validator/lt_with_float_type_spec.rb",
|
123
|
+
"spec/integration/numeric_validator/lte_with_float_type_spec.rb",
|
124
|
+
"spec/integration/numeric_validator/spec_helper.rb",
|
125
|
+
"spec/integration/primitive_validator/primitive_validator_spec.rb",
|
126
|
+
"spec/integration/primitive_validator/spec_helper.rb",
|
127
|
+
"spec/integration/pure_ruby_objects/plain_old_ruby_object_validation_spec.rb",
|
128
|
+
"spec/integration/required_field_validator/association_spec.rb",
|
129
|
+
"spec/integration/required_field_validator/boolean_type_value_spec.rb",
|
130
|
+
"spec/integration/required_field_validator/date_type_value_spec.rb",
|
131
|
+
"spec/integration/required_field_validator/datetime_type_value_spec.rb",
|
132
|
+
"spec/integration/required_field_validator/float_type_value_spec.rb",
|
133
|
+
"spec/integration/required_field_validator/integer_type_value_spec.rb",
|
134
|
+
"spec/integration/required_field_validator/plain_old_ruby_object_spec.rb",
|
135
|
+
"spec/integration/required_field_validator/shared_examples.rb",
|
136
|
+
"spec/integration/required_field_validator/spec_helper.rb",
|
137
|
+
"spec/integration/required_field_validator/string_type_value_spec.rb",
|
138
|
+
"spec/integration/required_field_validator/text_type_value_spec.rb",
|
139
|
+
"spec/integration/shared/default_validation_context.rb",
|
140
|
+
"spec/integration/shared/valid_and_invalid_model.rb",
|
141
|
+
"spec/integration/uniqueness_validator/spec_helper.rb",
|
142
|
+
"spec/integration/uniqueness_validator/uniqueness_validator_spec.rb",
|
143
|
+
"spec/integration/within_validator/spec_helper.rb",
|
144
|
+
"spec/integration/within_validator/within_validator_spec.rb",
|
145
|
+
"spec/public/resource_spec.rb",
|
146
|
+
"spec/rcov.opts",
|
147
|
+
"spec/spec.opts",
|
148
|
+
"spec/spec_helper.rb",
|
149
|
+
"spec/unit/contextual_validators/emptiness_spec.rb",
|
150
|
+
"spec/unit/contextual_validators/execution_spec.rb",
|
151
|
+
"spec/unit/contextual_validators/spec_helper.rb",
|
152
|
+
"spec/unit/generic_validator/equality_operator_spec.rb",
|
153
|
+
"spec/unit/validation_errors/adding_spec.rb",
|
154
|
+
"spec/unit/validation_errors/emptiness_spec.rb",
|
155
|
+
"spec/unit/validation_errors/enumerable_spec.rb",
|
156
|
+
"tasks/ci.rake",
|
157
|
+
"tasks/metrics.rake",
|
158
|
+
"tasks/spec.rake",
|
159
|
+
"tasks/yard.rake",
|
160
|
+
"tasks/yardstick.rake"
|
161
|
+
]
|
162
|
+
s.homepage = %q{http://github.com/datamapper/dm-more/tree/master/dm-validations}
|
163
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
164
|
+
s.require_paths = ["lib"]
|
165
|
+
s.rubyforge_project = %q{datamapper}
|
166
|
+
s.rubygems_version = %q{1.3.5}
|
167
|
+
s.summary = %q{Library for performing validations on DM models and pure Ruby object}
|
168
|
+
|
169
|
+
if s.respond_to? :specification_version then
|
170
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
171
|
+
s.specification_version = 3
|
172
|
+
|
173
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
174
|
+
s.add_runtime_dependency(%q<dm-core>, ["~> 0.10.2"])
|
175
|
+
s.add_development_dependency(%q<rspec>, ["~> 1.2.9"])
|
176
|
+
s.add_development_dependency(%q<yard>, ["~> 0.4.0"])
|
177
|
+
else
|
178
|
+
s.add_dependency(%q<dm-core>, ["~> 0.10.2"])
|
179
|
+
s.add_dependency(%q<rspec>, ["~> 1.2.9"])
|
180
|
+
s.add_dependency(%q<yard>, ["~> 0.4.0"])
|
181
|
+
end
|
182
|
+
else
|
183
|
+
s.add_dependency(%q<dm-core>, ["~> 0.10.2"])
|
184
|
+
s.add_dependency(%q<rspec>, ["~> 1.2.9"])
|
185
|
+
s.add_dependency(%q<yard>, ["~> 0.4.0"])
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
data/lib/dm-validations.rb
CHANGED
@@ -50,15 +50,15 @@ module DataMapper
|
|
50
50
|
end
|
51
51
|
|
52
52
|
chainable do
|
53
|
-
def
|
54
|
-
|
55
|
-
super
|
53
|
+
def update(attributes = {}, context = default_validation_context)
|
54
|
+
validation_context(context) { super(attributes) }
|
56
55
|
end
|
57
56
|
end
|
58
57
|
|
59
58
|
chainable do
|
60
|
-
def
|
61
|
-
|
59
|
+
def save_self(*)
|
60
|
+
return false unless validation_context_stack.empty? || valid?(current_validation_context)
|
61
|
+
super
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
@@ -89,33 +89,8 @@ module DataMapper
|
|
89
89
|
klass.validators.execute(context, self)
|
90
90
|
end
|
91
91
|
|
92
|
-
# Begin a recursive walk of the model checking validity
|
93
|
-
#
|
94
|
-
def all_valid?(context = :default)
|
95
|
-
recursive_valid?(self, context, true)
|
96
|
-
end
|
97
|
-
|
98
|
-
# Do recursive validity checking
|
99
|
-
#
|
100
|
-
def recursive_valid?(target, context, state)
|
101
|
-
valid = state
|
102
|
-
target.instance_variables.each do |ivar|
|
103
|
-
ivar_value = target.instance_variable_get(ivar)
|
104
|
-
if ivar_value.validatable?
|
105
|
-
valid = valid && recursive_valid?(ivar_value, context, valid)
|
106
|
-
elsif ivar_value.respond_to?(:each)
|
107
|
-
ivar_value.each do |item|
|
108
|
-
if item.validatable?
|
109
|
-
valid = valid && recursive_valid?(item, context, valid)
|
110
|
-
end
|
111
|
-
end
|
112
|
-
end
|
113
|
-
end
|
114
|
-
return valid && target.valid?
|
115
|
-
end
|
116
|
-
|
117
92
|
def validation_property_value(name)
|
118
|
-
respond_to?(name, true)
|
93
|
+
__send__(name) if respond_to?(name, true)
|
119
94
|
end
|
120
95
|
|
121
96
|
# Get the corresponding Resource property, if it exists.
|
@@ -128,18 +103,6 @@ module DataMapper
|
|
128
103
|
end
|
129
104
|
end
|
130
105
|
|
131
|
-
def validation_association_keys(name)
|
132
|
-
if model.relationships.has_key?(name)
|
133
|
-
result = []
|
134
|
-
relation = model.relationships[name]
|
135
|
-
relation.child_key.each do |key|
|
136
|
-
result << key.name
|
137
|
-
end
|
138
|
-
return result
|
139
|
-
end
|
140
|
-
nil
|
141
|
-
end
|
142
|
-
|
143
106
|
module ClassMethods
|
144
107
|
include DataMapper::Validate::ValidatesPresent
|
145
108
|
include DataMapper::Validate::ValidatesAbsent
|
@@ -158,18 +121,20 @@ module DataMapper
|
|
158
121
|
# Return the set of contextual validators or create a new one
|
159
122
|
#
|
160
123
|
def validators
|
161
|
-
@
|
124
|
+
@validators ||= ContextualValidators.new
|
162
125
|
end
|
163
126
|
|
127
|
+
private
|
128
|
+
|
164
129
|
# Clean up the argument list and return a opts hash, including the
|
165
130
|
# merging of any default opts. Set the context to default if none is
|
166
131
|
# provided. Also allow :context to be aliased to :on, :when & group
|
167
132
|
#
|
168
133
|
def opts_from_validator_args(args, defaults = nil)
|
169
|
-
opts = args.last.kind_of?(Hash) ? args.pop : {}
|
134
|
+
opts = args.last.kind_of?(Hash) ? args.pop.dup : {}
|
170
135
|
context = opts.delete(:group) || opts.delete(:on) || opts.delete(:when) || opts.delete(:context) || :default
|
171
|
-
opts[:context] = context
|
172
|
-
opts.
|
136
|
+
opts[:context] = Array(context)
|
137
|
+
opts.update(defaults) unless defaults.nil?
|
173
138
|
opts
|
174
139
|
end
|
175
140
|
|
@@ -178,21 +143,12 @@ module DataMapper
|
|
178
143
|
# if it does not already exist
|
179
144
|
#
|
180
145
|
def create_context_instance_methods(context)
|
181
|
-
name = "valid_for_#{context.to_s}?"
|
182
|
-
|
146
|
+
name = "valid_for_#{context.to_s}?"
|
147
|
+
unless respond_to?(:resource_method_defined) ? resource_method_defined?(name) : instance_methods.include?(name)
|
183
148
|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
184
|
-
def #{name}
|
185
|
-
valid?(#{context.to_sym.inspect})
|
186
|
-
end
|
187
|
-
RUBY
|
188
|
-
end
|
189
|
-
|
190
|
-
all = "all_valid_for_#{context.to_s}?" # all_valid_for_signup?
|
191
|
-
if !instance_methods.include?(all)
|
192
|
-
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
193
|
-
def #{all} # def all_valid_for_signup?
|
194
|
-
all_valid?(#{context.to_sym.inspect}) # all_valid?(:signup)
|
195
|
-
end # end
|
149
|
+
def #{name} # def valid_for_signup?
|
150
|
+
valid?(#{context.to_sym.inspect}) # valid?(:signup)
|
151
|
+
end # end
|
196
152
|
RUBY
|
197
153
|
end
|
198
154
|
end
|
@@ -209,21 +165,15 @@ module DataMapper
|
|
209
165
|
#
|
210
166
|
# @param [Class] klazz
|
211
167
|
# Validator class, example: DataMapper::Validate::LengthValidator
|
212
|
-
def add_validator_to_context(opts, fields,
|
168
|
+
def add_validator_to_context(opts, fields, validator_class)
|
213
169
|
fields.each do |field|
|
214
|
-
validator =
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
opts[:context].each do |c|
|
222
|
-
unless validators.context(c).include?(validator)
|
223
|
-
validators.context(c) << validator
|
224
|
-
create_context_instance_methods(c)
|
225
|
-
end
|
226
|
-
end
|
170
|
+
validator = validator_class.new(field, opts.dup)
|
171
|
+
|
172
|
+
opts[:context].each do |context|
|
173
|
+
validator_contexts = validators.context(context)
|
174
|
+
next if validator_contexts.include?(validator)
|
175
|
+
validator_contexts << validator
|
176
|
+
create_context_instance_methods(context)
|
227
177
|
end
|
228
178
|
end
|
229
179
|
end
|
@@ -39,9 +39,9 @@ module DataMapper
|
|
39
39
|
# @details [Triggers]
|
40
40
|
# Triggers that generate validator creation
|
41
41
|
#
|
42
|
-
# :
|
43
|
-
# Setting the option :
|
44
|
-
#
|
42
|
+
# :required => true
|
43
|
+
# Setting the option :required to true causes a
|
44
|
+
# validates_present validator to be automatically created on
|
45
45
|
# the property
|
46
46
|
#
|
47
47
|
# :length => 20
|
@@ -111,8 +111,8 @@ module DataMapper
|
|
111
111
|
def disabled_auto_validations?
|
112
112
|
@disable_auto_validations || false
|
113
113
|
end
|
114
|
-
alias auto_validations_disabled? disabled_auto_validations?
|
115
114
|
|
115
|
+
alias auto_validations_disabled? disabled_auto_validations?
|
116
116
|
|
117
117
|
# Checks whether or not property should be auto validated.
|
118
118
|
# It is the case for properties with :auto_validation option
|
@@ -125,7 +125,7 @@ module DataMapper
|
|
125
125
|
end
|
126
126
|
|
127
127
|
def infer_presence_validation_for(property, options)
|
128
|
-
return if
|
128
|
+
return if skip_presence_validation?(property)
|
129
129
|
|
130
130
|
validates_present property.name, options_with_message(options, property, :presence)
|
131
131
|
end
|
@@ -193,8 +193,8 @@ module DataMapper
|
|
193
193
|
|
194
194
|
private
|
195
195
|
|
196
|
-
def
|
197
|
-
property.
|
196
|
+
def skip_presence_validation?(property)
|
197
|
+
property.allow_blank? || property.serial?
|
198
198
|
end
|
199
199
|
end # module AutoValidate
|
200
200
|
end # module Validate
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'forwardable'
|
2
2
|
|
3
3
|
module DataMapper
|
4
4
|
module Validate
|
@@ -27,13 +27,6 @@ module DataMapper
|
|
27
27
|
# API
|
28
28
|
#
|
29
29
|
|
30
|
-
def dump
|
31
|
-
contexts.each_pair do |key, context|
|
32
|
-
puts "Key=#{key} Context: #{context}"
|
33
|
-
end
|
34
|
-
end
|
35
|
-
alias_method :inspect, :dump
|
36
|
-
|
37
30
|
# Return an array of validators for a named context
|
38
31
|
#
|
39
32
|
# @param [String]
|
@@ -59,18 +52,11 @@ module DataMapper
|
|
59
52
|
# @return [Boolean]
|
60
53
|
# true if all are valid, otherwise false
|
61
54
|
def execute(named_context, target)
|
62
|
-
raise(ArgumentError, "validation context #{named_context} doesn't seem to be defined. Known contexts are #{contexts.keys.inspect}") if !named_context || (contexts.length > 0 && !contexts[named_context])
|
63
55
|
target.errors.clear!
|
64
|
-
result = true
|
65
|
-
# note that all? and any? stop iteration on first negative or positive result,
|
66
|
-
# so we really have to use each here to make sure all validators are
|
67
|
-
# executed
|
68
|
-
context(named_context).select { |validator| validator.execute?(target) }.each do |validator|
|
69
|
-
result = false unless validator.call(target)
|
70
|
-
end
|
71
|
-
|
72
56
|
|
73
|
-
|
57
|
+
context(named_context).map do |validator|
|
58
|
+
validator.execute?(target) ? validator.call(target) : true
|
59
|
+
end.all?
|
74
60
|
end
|
75
61
|
|
76
62
|
end # module ContextualValidators
|