dm-validations 0.10.0 → 0.10.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.rdoc CHANGED
@@ -1,4 +1,12 @@
1
- === 0.10.0 / 2009-10-15
1
+ === 0.10.1 / 2009-09-30
2
+
3
+ * 2 minor enhancements:
4
+
5
+ * Always pass the base options into the validation methods so that
6
+ :allow_nil => true is always in effect in auto-validations
7
+ * Improved active_model compatibility
8
+
9
+ === 0.10.0 / 2009-09-15
2
10
 
3
11
  * 3 major enhancements:
4
12
 
@@ -89,7 +89,7 @@ module DataMapper
89
89
  # validation when the value is nil
90
90
  opts = { :allow_nil => true }
91
91
 
92
- if property.options.has_key?(:validates)
92
+ if property.options.key?(:validates)
93
93
  opts[:context] = property.options[:validates]
94
94
  end
95
95
 
@@ -98,7 +98,7 @@ module DataMapper
98
98
  infer_format_validation_for(property, opts.dup)
99
99
  infer_uniqueness_validation_for(property, opts.dup)
100
100
  infer_within_validation_for(property, opts.dup)
101
- infer_numeric_validation_for(property, opts.dup)
101
+ infer_type_validation_for(property, opts.dup)
102
102
  end # auto_generate_validations
103
103
 
104
104
  # Checks whether auto validations are currently
@@ -121,50 +121,56 @@ module DataMapper
121
121
  # @return [TrueClass, FalseClass]
122
122
  # true for properties with :auto_validation option that has positive value
123
123
  def skip_auto_validation_for?(property)
124
- property.options.has_key?(:auto_validation) && !property.options[:auto_validation]
124
+ property.options.key?(:auto_validation) && !property.options[:auto_validation]
125
125
  end
126
126
 
127
127
  def infer_presence_validation_for(property, options)
128
- unless allow_nil?(property)
129
- # validates_present property.name, opts
130
- validates_present property.name, options_with_message(options, property, :presence)
131
- end
128
+ return if allow_nil?(property)
129
+
130
+ validates_present property.name, options_with_message(options, property, :presence)
132
131
  end
133
132
 
134
133
  def infer_length_validation_for(property, options)
135
- if [ String, DataMapper::Types::Text ].include?(property.type)
136
- case length = property.options.fetch(:length, DataMapper::Property::DEFAULT_LENGTH)
137
- when Range then options[:within] = length
138
- else options[:maximum] = length
139
- end
134
+ return unless [ String, DataMapper::Types::Text ].include?(property.type)
140
135
 
141
- validates_length property.name, options_with_message(options, property, :length)
136
+ case length = property.options.fetch(:length, DataMapper::Property::DEFAULT_LENGTH)
137
+ when Range then options[:within] = length
138
+ else options[:maximum] = length
142
139
  end
140
+
141
+ validates_length property.name, options_with_message(options, property, :length)
143
142
  end
144
143
 
145
144
  def infer_format_validation_for(property, options)
146
- if property.options.has_key?(:format)
147
- options[:with] = property.options[:format]
148
- validates_format property.name, options_with_message(options, property, :format)
149
- end
145
+ return unless property.options.key?(:format)
146
+
147
+ options[:with] = property.options[:format]
148
+
149
+ validates_format property.name, options_with_message(options, property, :format)
150
150
  end
151
151
 
152
152
  def infer_uniqueness_validation_for(property, options)
153
- if property.options.has_key?(:unique)
154
- value = property.options[:unique]
155
- if value.is_a?(Array) || value.is_a?(Symbol)
156
- validates_is_unique property.name, options_with_message({:scope => Array(value)}, property, :is_unique)
157
- elsif value.is_a?(TrueClass)
158
- validates_is_unique property.name, options_with_message({}, property, :is_unique)
159
- end
153
+ return unless property.options.key?(:unique)
154
+
155
+ case value = property.options[:unique]
156
+ when Array, Symbol
157
+ options[:scope] = Array(value)
158
+
159
+ validates_is_unique property.name, options_with_message(options, property, :is_unique)
160
+ when TrueClass
161
+ validates_is_unique property.name, options_with_message(options, property, :is_unique)
160
162
  end
161
163
  end
162
164
 
163
165
  def infer_within_validation_for(property, options)
164
- validates_within property.name, options_with_message({:set => property.options[:set]}, property, :within) if property.options.has_key?(:set)
166
+ return unless property.options.key?(:set)
167
+
168
+ options[:set] = property.options[:set]
169
+
170
+ validates_within property.name, options_with_message(options, property, :within)
165
171
  end
166
172
 
167
- def infer_numeric_validation_for(property, options)
173
+ def infer_type_validation_for(property, options)
168
174
  options[:gte] = property.min if property.min
169
175
  options[:lte] = property.max if property.max
170
176
 
@@ -177,11 +183,11 @@ module DataMapper
177
183
  options[:scale] = property.scale
178
184
 
179
185
  validates_is_number property.name, options_with_message(options, property, :is_number)
180
- else
186
+ elsif !property.custom?
181
187
  # We only need this in the case we don't already
182
188
  # have a numeric validator, because otherwise
183
189
  # it will cause duplicate validation errors
184
- validates_is_primitive property.name, options_with_message(options, property, :is_primitive) unless property.custom?
190
+ validates_is_primitive property.name, options_with_message(options, property, :is_primitive)
185
191
  end
186
192
  end
187
193
 
@@ -84,6 +84,17 @@ module DataMapper
84
84
  end
85
85
  end
86
86
 
87
+ # Return validation errors for a particular field name or an empty array
88
+ #
89
+ # This method is a necessary requirement for active_model compatibility.
90
+ #
91
+ # @param [Symbol] field_name the name of the field you want an error for
92
+ # @return [Array<Array<String>>]
93
+ # array of validation errors or empty array, if there are no errors on given field
94
+ def [](field_name)
95
+ errors[field_name] ||= []
96
+ end
97
+
87
98
  # Return validation errors for a particular field_name.
88
99
  #
89
100
  # @param [Symbol] field_name the name of the field you want an error for
@@ -1,5 +1,5 @@
1
1
  module DataMapper
2
2
  module Validations
3
- VERSION = '0.10.0'.freeze
3
+ VERSION = '0.10.1'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dm-validations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.10.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Guy van den Berg, DataMapper development team
@@ -9,10 +9,19 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-16 00:00:00 -07:00
12
+ date: 2009-09-30 00:00:00 -07:00
13
13
  default_executable:
14
- dependencies: []
15
-
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: dm-core
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - "="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.10.1
24
+ version:
16
25
  description: Library for performing validations on DM models and pure Ruby object
17
26
  email:
18
27
  - vandenberg.guy [a] gmail [d] com