dm-validations 1.1.0.rc2 → 1.1.0.rc3
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/VERSION +1 -1
- data/dm-validations.gemspec +10 -9
- data/lib/dm-validations.rb +1 -25
- data/lib/dm-validations/contextual_validators.rb +14 -6
- data/lib/dm-validations/support/ordered_hash.rb +434 -0
- data/lib/dm-validations/validation_errors.rb +3 -3
- data/lib/dm-validations/validators/absent_field_validator.rb +2 -1
- data/lib/dm-validations/validators/generic_validator.rb +2 -2
- data/lib/dm-validations/validators/required_field_validator.rb +1 -1
- data/spec/fixtures/mittelschnauzer.rb +5 -2
- data/spec/integration/absent_field_validator/absent_field_validator_spec.rb +3 -3
- data/spec/integration/length_validator/minimum_spec.rb +10 -0
- data/spec/integration/length_validator/range_spec.rb +1 -1
- data/spec/integration/method_validator/method_validator_spec.rb +1 -1
- data/spec/integration/pure_ruby_objects/plain_old_ruby_object_validation_spec.rb +1 -1
- data/spec/integration/required_field_validator/boolean_type_value_spec.rb +1 -1
- data/spec/integration/shared/valid_and_invalid_model.rb +2 -2
- data/spec/spec_helper.rb +8 -0
- metadata +6 -5
data/Gemfile
CHANGED
@@ -5,7 +5,7 @@ source 'http://rubygems.org'
|
|
5
5
|
SOURCE = ENV.fetch('SOURCE', :git).to_sym
|
6
6
|
REPO_POSTFIX = SOURCE == :path ? '' : '.git'
|
7
7
|
DATAMAPPER = SOURCE == :path ? Pathname(__FILE__).dirname.parent : 'http://github.com/datamapper'
|
8
|
-
DM_VERSION = '~> 1.1.0.
|
8
|
+
DM_VERSION = '~> 1.1.0.rc3'
|
9
9
|
DO_VERSION = '~> 0.10.2'
|
10
10
|
DM_DO_ADAPTERS = %w[ sqlite postgres mysql oracle sqlserver ]
|
11
11
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.1.0.
|
1
|
+
1.1.0.rc3
|
data/dm-validations.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{dm-validations}
|
8
|
-
s.version = "1.1.0.
|
8
|
+
s.version = "1.1.0.rc3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Guy van den Berg"]
|
12
|
-
s.date = %q{2011-03-
|
12
|
+
s.date = %q{2011-03-10}
|
13
13
|
s.description = %q{Library for performing validations on DM models and pure Ruby object}
|
14
14
|
s.email = %q{vandenberg.guy [a] gmail [d] com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -31,6 +31,7 @@ Gem::Specification.new do |s|
|
|
31
31
|
"lib/dm-validations/formats/url.rb",
|
32
32
|
"lib/dm-validations/support/context.rb",
|
33
33
|
"lib/dm-validations/support/object.rb",
|
34
|
+
"lib/dm-validations/support/ordered_hash.rb",
|
34
35
|
"lib/dm-validations/validation_errors.rb",
|
35
36
|
"lib/dm-validations/validators/absent_field_validator.rb",
|
36
37
|
"lib/dm-validations/validators/acceptance_validator.rb",
|
@@ -165,7 +166,7 @@ Gem::Specification.new do |s|
|
|
165
166
|
s.homepage = %q{http://github.com/datamapper/dm-validations}
|
166
167
|
s.require_paths = ["lib"]
|
167
168
|
s.rubyforge_project = %q{datamapper}
|
168
|
-
s.rubygems_version = %q{1.
|
169
|
+
s.rubygems_version = %q{1.6.2}
|
169
170
|
s.summary = %q{Library for performing validations on DM models and pure Ruby object}
|
170
171
|
s.test_files = [
|
171
172
|
"spec/fixtures/barcode.rb",
|
@@ -285,21 +286,21 @@ Gem::Specification.new do |s|
|
|
285
286
|
s.specification_version = 3
|
286
287
|
|
287
288
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
288
|
-
s.add_runtime_dependency(%q<dm-core>, ["~> 1.1.0.
|
289
|
-
s.add_development_dependency(%q<dm-types>, ["~> 1.1.0.
|
289
|
+
s.add_runtime_dependency(%q<dm-core>, ["~> 1.1.0.rc3"])
|
290
|
+
s.add_development_dependency(%q<dm-types>, ["~> 1.1.0.rc3"])
|
290
291
|
s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
|
291
292
|
s.add_development_dependency(%q<rake>, ["~> 0.8.7"])
|
292
293
|
s.add_development_dependency(%q<rspec>, ["~> 1.3.1"])
|
293
294
|
else
|
294
|
-
s.add_dependency(%q<dm-core>, ["~> 1.1.0.
|
295
|
-
s.add_dependency(%q<dm-types>, ["~> 1.1.0.
|
295
|
+
s.add_dependency(%q<dm-core>, ["~> 1.1.0.rc3"])
|
296
|
+
s.add_dependency(%q<dm-types>, ["~> 1.1.0.rc3"])
|
296
297
|
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
297
298
|
s.add_dependency(%q<rake>, ["~> 0.8.7"])
|
298
299
|
s.add_dependency(%q<rspec>, ["~> 1.3.1"])
|
299
300
|
end
|
300
301
|
else
|
301
|
-
s.add_dependency(%q<dm-core>, ["~> 1.1.0.
|
302
|
-
s.add_dependency(%q<dm-types>, ["~> 1.1.0.
|
302
|
+
s.add_dependency(%q<dm-core>, ["~> 1.1.0.rc3"])
|
303
|
+
s.add_dependency(%q<dm-types>, ["~> 1.1.0.rc3"])
|
303
304
|
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
304
305
|
s.add_dependency(%q<rake>, ["~> 0.8.7"])
|
305
306
|
s.add_dependency(%q<rspec>, ["~> 1.3.1"])
|
data/lib/dm-validations.rb
CHANGED
@@ -1,29 +1,5 @@
|
|
1
1
|
require 'dm-core'
|
2
|
-
|
3
|
-
begin
|
4
|
-
# We need array for extract_options! which attr_accessors uses, at least in AS
|
5
|
-
# 2.3.3.
|
6
|
-
require 'active_support/core_ext/array'
|
7
|
-
require 'active_support/core_ext/class/attribute_accessors'
|
8
|
-
rescue LoadError
|
9
|
-
require 'extlib/class'
|
10
|
-
end
|
11
|
-
|
12
|
-
begin
|
13
|
-
require 'active_support/core_ext/object/blank'
|
14
|
-
rescue LoadError
|
15
|
-
require 'extlib/blank'
|
16
|
-
end
|
17
|
-
|
18
|
-
begin
|
19
|
-
require 'active_support/ordered_hash'
|
20
|
-
rescue LoadError
|
21
|
-
require 'extlib/dictionary'
|
22
|
-
|
23
|
-
module ::ActiveSupport
|
24
|
-
OrderedHash = ::Dictionary
|
25
|
-
end
|
26
|
-
end
|
2
|
+
require 'dm-validations/support/ordered_hash'
|
27
3
|
|
28
4
|
class Object
|
29
5
|
# If receiver is callable, calls it and
|
@@ -74,14 +74,22 @@ module DataMapper
|
|
74
74
|
# - those applied to dirty attributes only,
|
75
75
|
# - those that should always run (presence/absence)
|
76
76
|
# - those that don't reference any real properties (field-less
|
77
|
-
# block validators)
|
78
|
-
|
79
|
-
|
77
|
+
# block validators, validations in virtual attributes)
|
78
|
+
if target.kind_of?(DataMapper::Resource) && !target.new?
|
79
|
+
attrs = target.attributes.keys
|
80
80
|
dirty_attrs = target.dirty_attributes.keys.map{ |p| p.name }
|
81
|
-
validators = runnable_validators.select{
|
81
|
+
validators = runnable_validators.select{|v|
|
82
|
+
!attrs.include?(v.field_name) || dirty_attrs.include?(v.field_name)
|
83
|
+
}
|
84
|
+
|
85
|
+
# Load all lazy, not-yet-loaded properties that need validation,
|
86
|
+
# all at once.
|
87
|
+
fields_to_load = validators.map{|v|
|
88
|
+
target.class.properties[v.field_name]
|
89
|
+
}.compact.select {|p|
|
90
|
+
p.lazy? && !p.loaded?(target)
|
91
|
+
}
|
82
92
|
|
83
|
-
# Load all lazy, not-yet-loaded properties that need validation, all at once.
|
84
|
-
fields_to_load = validators.map{ |v| target.class.properties[v.field_name] }.select{ |p| p.lazy? && !p.loaded?(target) }
|
85
93
|
target.__send__(:eager_load, fields_to_load)
|
86
94
|
|
87
95
|
# Finally include any validators that should always run or don't
|
@@ -0,0 +1,434 @@
|
|
1
|
+
module DataMapper; module Validations
|
2
|
+
# TITLE:
|
3
|
+
#
|
4
|
+
# OrderedHash (originally Dictionary)
|
5
|
+
#
|
6
|
+
# AUTHORS:
|
7
|
+
#
|
8
|
+
# - Jan Molic
|
9
|
+
# - Thomas Sawyer
|
10
|
+
#
|
11
|
+
# CREDIT:
|
12
|
+
#
|
13
|
+
# - Andrew Johnson (merge, to_a, inspect, shift and Hash[])
|
14
|
+
# - Jeff Sharpe (reverse and reverse!)
|
15
|
+
# - Thomas Leitner (has_key? and key?)
|
16
|
+
#
|
17
|
+
# LICENSE:
|
18
|
+
#
|
19
|
+
# Copyright (c) 2005 Jan Molic, Thomas Sawyer
|
20
|
+
#
|
21
|
+
# Ruby License
|
22
|
+
#
|
23
|
+
# This module is free software. You may use, modify, and/or redistribute this
|
24
|
+
# software under the same terms as Ruby.
|
25
|
+
#
|
26
|
+
# This program is distributed in the hope that it will be useful, but WITHOUT
|
27
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
28
|
+
# FOR A PARTICULAR PURPOSE.
|
29
|
+
#
|
30
|
+
# Originally ported from OrderHash 2.0, Copyright (c) 2005 jan molic
|
31
|
+
#
|
32
|
+
# LOG:
|
33
|
+
#
|
34
|
+
# - 2007.10.31 trans
|
35
|
+
# Fixed initialize so the constructor blocks correctly effected dictionary
|
36
|
+
# rather then just the internal hash.
|
37
|
+
|
38
|
+
# = Dictionary
|
39
|
+
#
|
40
|
+
# The Dictionary class is a Hash that preserves order.
|
41
|
+
# So it has some array-like extensions also. By defualt
|
42
|
+
# a Dictionary object preserves insertion order, but any
|
43
|
+
# order can be specified including alphabetical key order.
|
44
|
+
#
|
45
|
+
# == Usage
|
46
|
+
#
|
47
|
+
# Just require this file and use Dictionary instead of Hash.
|
48
|
+
#
|
49
|
+
# # You can do simply
|
50
|
+
# hsh = Dictionary.new
|
51
|
+
# hsh['z'] = 1
|
52
|
+
# hsh['a'] = 2
|
53
|
+
# hsh['c'] = 3
|
54
|
+
# p hsh.keys #=> ['z','a','c']
|
55
|
+
#
|
56
|
+
# # or using Dictionary[] method
|
57
|
+
# hsh = Dictionary['z', 1, 'a', 2, 'c', 3]
|
58
|
+
# p hsh.keys #=> ['z','a','c']
|
59
|
+
#
|
60
|
+
# # but this doesn't preserve order
|
61
|
+
# hsh = Dictionary['z'=>1, 'a'=>2, 'c'=>3]
|
62
|
+
# p hsh.keys #=> ['a','c','z']
|
63
|
+
#
|
64
|
+
# # Dictionary has useful extensions: push, pop and unshift
|
65
|
+
# p hsh.push('to_end', 15) #=> true, key added
|
66
|
+
# p hsh.push('to_end', 30) #=> false, already - nothing happen
|
67
|
+
# p hsh.unshift('to_begin', 50) #=> true, key added
|
68
|
+
# p hsh.unshift('to_begin', 60) #=> false, already - nothing happen
|
69
|
+
# p hsh.keys #=> ["to_begin", "a", "c", "z", "to_end"]
|
70
|
+
# p hsh.pop #=> ["to_end", 15], if nothing remains, return nil
|
71
|
+
# p hsh.keys #=> ["to_begin", "a", "c", "z"]
|
72
|
+
# p hsh.shift #=> ["to_begin", 30], if nothing remains, return nil
|
73
|
+
#
|
74
|
+
# == Usage Notes
|
75
|
+
#
|
76
|
+
# * You can use #order_by to set internal sort order.
|
77
|
+
# * #<< takes a two element [k,v] array and inserts.
|
78
|
+
# * Use ::auto which creates Dictionay sub-entries as needed.
|
79
|
+
# * And ::alpha which creates a new Dictionary sorted by key.
|
80
|
+
class OrderedHash
|
81
|
+
|
82
|
+
include Enumerable
|
83
|
+
|
84
|
+
class << self
|
85
|
+
#--
|
86
|
+
# TODO is this needed? Doesn't the super class do this?
|
87
|
+
#++
|
88
|
+
def [](*args)
|
89
|
+
hsh = new
|
90
|
+
if Hash === args[0]
|
91
|
+
hsh.replace(args[0])
|
92
|
+
elsif (args.size % 2) != 0
|
93
|
+
raise ArgumentError, "odd number of elements for Hash"
|
94
|
+
else
|
95
|
+
while !args.empty?
|
96
|
+
hsh[args.shift] = args.shift
|
97
|
+
end
|
98
|
+
end
|
99
|
+
hsh
|
100
|
+
end
|
101
|
+
|
102
|
+
# Like #new but the block sets the order.
|
103
|
+
#
|
104
|
+
def new_by(*args, &blk)
|
105
|
+
new(*args).order_by(&blk)
|
106
|
+
end
|
107
|
+
|
108
|
+
# Alternate to #new which creates a dictionary sorted by key.
|
109
|
+
#
|
110
|
+
# d = Dictionary.alpha
|
111
|
+
# d["z"] = 1
|
112
|
+
# d["y"] = 2
|
113
|
+
# d["x"] = 3
|
114
|
+
# d #=> {"x"=>3,"y"=>2,"z"=>2}
|
115
|
+
#
|
116
|
+
# This is equivalent to:
|
117
|
+
#
|
118
|
+
# Dictionary.new.order_by { |key,value| key }
|
119
|
+
def alpha(*args, &block)
|
120
|
+
new(*args, &block).order_by_key
|
121
|
+
end
|
122
|
+
|
123
|
+
# Alternate to #new which auto-creates sub-dictionaries as needed.
|
124
|
+
#
|
125
|
+
# d = Dictionary.auto
|
126
|
+
# d["a"]["b"]["c"] = "abc" #=> { "a"=>{"b"=>{"c"=>"abc"}}}
|
127
|
+
#
|
128
|
+
def auto(*args)
|
129
|
+
#AutoDictionary.new(*args)
|
130
|
+
leet = lambda { |hsh, key| hsh[key] = new(&leet) }
|
131
|
+
new(*args, &leet)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
# New Dictiionary.
|
136
|
+
def initialize(*args, &blk)
|
137
|
+
@order = []
|
138
|
+
@order_by = nil
|
139
|
+
if blk
|
140
|
+
dict = self # This ensure autmatic key entry effect the
|
141
|
+
oblk = lambda{ |hsh, key| blk[dict,key] } # dictionary rather then just the interal hash.
|
142
|
+
@hash = Hash.new(*args, &oblk)
|
143
|
+
else
|
144
|
+
@hash = Hash.new(*args)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
def order
|
149
|
+
reorder if @order_by
|
150
|
+
@order
|
151
|
+
end
|
152
|
+
|
153
|
+
# Keep dictionary sorted by a specific sort order.
|
154
|
+
def order_by( &block )
|
155
|
+
@order_by = block
|
156
|
+
order
|
157
|
+
self
|
158
|
+
end
|
159
|
+
|
160
|
+
# Keep dictionary sorted by key.
|
161
|
+
#
|
162
|
+
# d = Dictionary.new.order_by_key
|
163
|
+
# d["z"] = 1
|
164
|
+
# d["y"] = 2
|
165
|
+
# d["x"] = 3
|
166
|
+
# d #=> {"x"=>3,"y"=>2,"z"=>2}
|
167
|
+
#
|
168
|
+
# This is equivalent to:
|
169
|
+
#
|
170
|
+
# Dictionary.new.order_by { |key,value| key }
|
171
|
+
#
|
172
|
+
# The initializer Dictionary#alpha also provides this.
|
173
|
+
def order_by_key
|
174
|
+
@order_by = lambda { |k,v| k }
|
175
|
+
order
|
176
|
+
self
|
177
|
+
end
|
178
|
+
|
179
|
+
# Keep dictionary sorted by value.
|
180
|
+
#
|
181
|
+
# d = Dictionary.new.order_by_value
|
182
|
+
# d["z"] = 1
|
183
|
+
# d["y"] = 2
|
184
|
+
# d["x"] = 3
|
185
|
+
# d #=> {"x"=>3,"y"=>2,"z"=>2}
|
186
|
+
#
|
187
|
+
# This is equivalent to:
|
188
|
+
#
|
189
|
+
# Dictionary.new.order_by { |key,value| value }
|
190
|
+
def order_by_value
|
191
|
+
@order_by = lambda { |k,v| v }
|
192
|
+
order
|
193
|
+
self
|
194
|
+
end
|
195
|
+
|
196
|
+
#
|
197
|
+
def reorder
|
198
|
+
if @order_by
|
199
|
+
assoc = @order.collect{ |k| [k,@hash[k]] }.sort_by(&@order_by)
|
200
|
+
@order = assoc.collect{ |k,v| k }
|
201
|
+
end
|
202
|
+
@order
|
203
|
+
end
|
204
|
+
|
205
|
+
def ==(hsh2)
|
206
|
+
if hsh2.is_a?( Dictionary )
|
207
|
+
@order == hsh2.order &&
|
208
|
+
@hash == hsh2.instance_variable_get("@hash")
|
209
|
+
else
|
210
|
+
false
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
def [] k
|
215
|
+
@hash[ k ]
|
216
|
+
end
|
217
|
+
|
218
|
+
def fetch(k, *a, &b)
|
219
|
+
@hash.fetch(k, *a, &b)
|
220
|
+
end
|
221
|
+
|
222
|
+
# Store operator.
|
223
|
+
#
|
224
|
+
# h[key] = value
|
225
|
+
#
|
226
|
+
# Or with additional index.
|
227
|
+
#
|
228
|
+
# h[key,index] = value
|
229
|
+
def []=(k, i=nil, v=nil)
|
230
|
+
if v
|
231
|
+
insert(i,k,v)
|
232
|
+
else
|
233
|
+
store(k,i)
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
237
|
+
def insert( i,k,v )
|
238
|
+
@order.insert( i,k )
|
239
|
+
@hash.store( k,v )
|
240
|
+
end
|
241
|
+
|
242
|
+
def store( a,b )
|
243
|
+
@order.push( a ) unless @hash.has_key?( a )
|
244
|
+
@hash.store( a,b )
|
245
|
+
end
|
246
|
+
|
247
|
+
def clear
|
248
|
+
@order = []
|
249
|
+
@hash.clear
|
250
|
+
end
|
251
|
+
|
252
|
+
def delete( key )
|
253
|
+
@order.delete( key )
|
254
|
+
@hash.delete( key )
|
255
|
+
end
|
256
|
+
|
257
|
+
def each_key
|
258
|
+
order.each { |k| yield( k ) }
|
259
|
+
self
|
260
|
+
end
|
261
|
+
|
262
|
+
def each_value
|
263
|
+
order.each { |k| yield( @hash[k] ) }
|
264
|
+
self
|
265
|
+
end
|
266
|
+
|
267
|
+
def each
|
268
|
+
order.each { |k| yield( k,@hash[k] ) }
|
269
|
+
self
|
270
|
+
end
|
271
|
+
alias each_pair each
|
272
|
+
|
273
|
+
def delete_if
|
274
|
+
order.clone.each { |k| delete k if yield(k,@hash[k]) }
|
275
|
+
self
|
276
|
+
end
|
277
|
+
|
278
|
+
def values
|
279
|
+
ary = []
|
280
|
+
order.each { |k| ary.push @hash[k] }
|
281
|
+
ary
|
282
|
+
end
|
283
|
+
|
284
|
+
def keys
|
285
|
+
order
|
286
|
+
end
|
287
|
+
|
288
|
+
def invert
|
289
|
+
hsh2 = self.class.new
|
290
|
+
order.each { |k| hsh2[@hash[k]] = k }
|
291
|
+
hsh2
|
292
|
+
end
|
293
|
+
|
294
|
+
def reject( &block )
|
295
|
+
self.dup.delete_if(&block)
|
296
|
+
end
|
297
|
+
|
298
|
+
def reject!( &block )
|
299
|
+
hsh2 = reject(&block)
|
300
|
+
self == hsh2 ? nil : hsh2
|
301
|
+
end
|
302
|
+
|
303
|
+
def replace( hsh2 )
|
304
|
+
@order = hsh2.order
|
305
|
+
@hash = hsh2.hash
|
306
|
+
end
|
307
|
+
|
308
|
+
def shift
|
309
|
+
key = order.first
|
310
|
+
key ? [key,delete(key)] : super
|
311
|
+
end
|
312
|
+
|
313
|
+
def unshift( k,v )
|
314
|
+
unless @hash.include?( k )
|
315
|
+
@order.unshift( k )
|
316
|
+
@hash.store( k,v )
|
317
|
+
true
|
318
|
+
else
|
319
|
+
false
|
320
|
+
end
|
321
|
+
end
|
322
|
+
|
323
|
+
def <<(kv)
|
324
|
+
push( *kv )
|
325
|
+
end
|
326
|
+
|
327
|
+
def push( k,v )
|
328
|
+
unless @hash.include?( k )
|
329
|
+
@order.push( k )
|
330
|
+
@hash.store( k,v )
|
331
|
+
true
|
332
|
+
else
|
333
|
+
false
|
334
|
+
end
|
335
|
+
end
|
336
|
+
|
337
|
+
def pop
|
338
|
+
key = order.last
|
339
|
+
key ? [key,delete(key)] : nil
|
340
|
+
end
|
341
|
+
|
342
|
+
def inspect
|
343
|
+
ary = []
|
344
|
+
each {|k,v| ary << k.inspect + "=>" + v.inspect}
|
345
|
+
'{' + ary.join(", ") + '}'
|
346
|
+
end
|
347
|
+
|
348
|
+
def dup
|
349
|
+
a = []
|
350
|
+
each{ |k,v| a << k; a << v }
|
351
|
+
self.class[*a]
|
352
|
+
end
|
353
|
+
|
354
|
+
def update( hsh2 )
|
355
|
+
hsh2.each { |k,v| self[k] = v }
|
356
|
+
reorder
|
357
|
+
self
|
358
|
+
end
|
359
|
+
alias :merge! update
|
360
|
+
|
361
|
+
def merge( hsh2 )
|
362
|
+
self.dup.update(hsh2)
|
363
|
+
end
|
364
|
+
|
365
|
+
def select
|
366
|
+
ary = []
|
367
|
+
each { |k,v| ary << [k,v] if yield k,v }
|
368
|
+
ary
|
369
|
+
end
|
370
|
+
|
371
|
+
def reverse!
|
372
|
+
@order.reverse!
|
373
|
+
self
|
374
|
+
end
|
375
|
+
|
376
|
+
def reverse
|
377
|
+
dup.reverse!
|
378
|
+
end
|
379
|
+
|
380
|
+
def first
|
381
|
+
@hash[order.first]
|
382
|
+
end
|
383
|
+
|
384
|
+
def last
|
385
|
+
@hash[order.last]
|
386
|
+
end
|
387
|
+
|
388
|
+
def length
|
389
|
+
@order.length
|
390
|
+
end
|
391
|
+
alias :size :length
|
392
|
+
|
393
|
+
def empty?
|
394
|
+
@hash.empty?
|
395
|
+
end
|
396
|
+
|
397
|
+
def has_key?(key)
|
398
|
+
@hash.has_key?(key)
|
399
|
+
end
|
400
|
+
|
401
|
+
def key?(key)
|
402
|
+
@hash.key?(key)
|
403
|
+
end
|
404
|
+
|
405
|
+
def to_a
|
406
|
+
ary = []
|
407
|
+
each { |k,v| ary << [k,v] }
|
408
|
+
ary
|
409
|
+
end
|
410
|
+
|
411
|
+
def to_json
|
412
|
+
buf = "["
|
413
|
+
map do |k,v|
|
414
|
+
buf << k.to_json
|
415
|
+
buf << ", "
|
416
|
+
buf << v.to_json
|
417
|
+
end.join(", ")
|
418
|
+
buf << "]"
|
419
|
+
buf
|
420
|
+
end
|
421
|
+
|
422
|
+
def to_s
|
423
|
+
self.to_a.to_s
|
424
|
+
end
|
425
|
+
|
426
|
+
def to_hash
|
427
|
+
@hash.dup
|
428
|
+
end
|
429
|
+
|
430
|
+
def to_h
|
431
|
+
@hash.dup
|
432
|
+
end
|
433
|
+
end
|
434
|
+
end; end
|
@@ -48,7 +48,7 @@ module DataMapper
|
|
48
48
|
|
49
49
|
def initialize(resource)
|
50
50
|
@resource = resource
|
51
|
-
@errors =
|
51
|
+
@errors = DataMapper::Validations::OrderedHash.new { |h,k| h[k] = [] }
|
52
52
|
end
|
53
53
|
|
54
54
|
# Clear existing validation errors.
|
@@ -91,12 +91,12 @@ module DataMapper
|
|
91
91
|
# array of validation errors or empty array, if there are no errors on given field
|
92
92
|
def on(field_name)
|
93
93
|
errors_for_field = errors[field_name]
|
94
|
-
|
94
|
+
DataMapper::Ext.blank?(errors_for_field) ? nil : errors_for_field.uniq
|
95
95
|
end
|
96
96
|
|
97
97
|
def each
|
98
98
|
errors.map.each do |k, v|
|
99
|
-
next if
|
99
|
+
next if DataMapper::Ext.blank?(v)
|
100
100
|
yield(v)
|
101
101
|
end
|
102
102
|
end
|
@@ -7,7 +7,8 @@ module DataMapper
|
|
7
7
|
# @since 0.9
|
8
8
|
class AbsenceValidator < GenericValidator
|
9
9
|
def call(target)
|
10
|
-
|
10
|
+
value = target.validation_property_value(field_name)
|
11
|
+
return true if DataMapper::Ext.blank?(value)
|
11
12
|
|
12
13
|
error_message = self.options[:message] || ValidationErrors.default_error_message(:absent, field_name)
|
13
14
|
add_error(target, error_message, field_name)
|
@@ -30,7 +30,7 @@ module DataMapper
|
|
30
30
|
#
|
31
31
|
def initialize(field_name, options = {})
|
32
32
|
@field_name = field_name
|
33
|
-
@options =
|
33
|
+
@options = DataMapper::Ext::Hash.except(options, :if, :unless)
|
34
34
|
@if_clause = options[:if]
|
35
35
|
@unless_clause = options[:unless]
|
36
36
|
@humanized_field_name = DataMapper::Inflector.humanize(@field_name)
|
@@ -99,7 +99,7 @@ module DataMapper
|
|
99
99
|
def optional?(value)
|
100
100
|
if value.nil?
|
101
101
|
@options[:allow_nil] || (@options[:allow_blank] && !@options.has_key?(:allow_nil))
|
102
|
-
elsif
|
102
|
+
elsif DataMapper::Ext.blank?(value)
|
103
103
|
@options[:allow_blank]
|
104
104
|
end
|
105
105
|
end
|
@@ -23,7 +23,7 @@ module DataMapper
|
|
23
23
|
# Other property types are considered present if non-blank.
|
24
24
|
# Non-properties are considered present if non-blank.
|
25
25
|
def present?(value, property)
|
26
|
-
boolean_type?(property) ? !value.nil? : !
|
26
|
+
boolean_type?(property) ? !value.nil? : !DataMapper::Ext.blank?(value)
|
27
27
|
end
|
28
28
|
|
29
29
|
def default_error(property)
|
@@ -19,16 +19,19 @@ module DataMapper
|
|
19
19
|
property :height, Float
|
20
20
|
end
|
21
21
|
|
22
|
+
attr_accessor :owner
|
23
|
+
|
22
24
|
#
|
23
25
|
# Validations
|
24
26
|
#
|
25
27
|
|
26
|
-
validates_length_of :name,
|
28
|
+
validates_length_of :name, :min => 2, :allow_nil => false
|
29
|
+
validates_length_of :owner, :min => 2
|
27
30
|
|
28
31
|
validates_numericality_of :height, :lt => 55.2
|
29
32
|
|
30
33
|
def self.valid_instance
|
31
|
-
new(:name => "Roudolf Wilde", :height => 50.4)
|
34
|
+
new(:name => "Roudolf Wilde", :height => 50.4, :owner => 'don')
|
32
35
|
end
|
33
36
|
end # Mittelschnauzer
|
34
37
|
end
|
@@ -34,7 +34,7 @@ describe 'DataMapper::Validations::Fixtures::Kayak' do
|
|
34
34
|
end
|
35
35
|
|
36
36
|
it "has no error messages" do
|
37
|
-
@kayak.errors.on(:salesman).should
|
37
|
+
@kayak.errors.on(:salesman).should be_nil
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
@@ -49,7 +49,7 @@ describe 'DataMapper::Validations::Fixtures::Kayak' do
|
|
49
49
|
end
|
50
50
|
|
51
51
|
it "has no error messages" do
|
52
|
-
@kayak.errors.on(:salesman).should
|
52
|
+
@kayak.errors.on(:salesman).should be_nil
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
@@ -64,7 +64,7 @@ describe 'DataMapper::Validations::Fixtures::Kayak' do
|
|
64
64
|
end
|
65
65
|
|
66
66
|
it "has no error messages" do
|
67
|
-
@kayak.errors.on(:salesman).should
|
67
|
+
@kayak.errors.on(:salesman).should be_nil
|
68
68
|
end
|
69
69
|
end
|
70
70
|
end
|
@@ -41,4 +41,14 @@ describe 'DataMapper::Validations::Fixtures::Mittelschnauzer' do
|
|
41
41
|
|
42
42
|
it_should_behave_like "entity with a name shorter than 2 characters"
|
43
43
|
end
|
44
|
+
|
45
|
+
describe "persisted, with a single character owner" do
|
46
|
+
before :all do
|
47
|
+
@model.save
|
48
|
+
@model.owner = 'a'
|
49
|
+
@model.valid?
|
50
|
+
end
|
51
|
+
|
52
|
+
it_should_behave_like "invalid model"
|
53
|
+
end
|
44
54
|
end
|
@@ -118,7 +118,7 @@ describe "C++" do
|
|
118
118
|
|
119
119
|
it "is not approved by Linus" do
|
120
120
|
@model.valid?(:hacking_on_the_kernel)
|
121
|
-
@model.errors.on(:ensure_approved_by_linus_himself).should_not
|
121
|
+
@model.errors.on(:ensure_approved_by_linus_himself).should_not be_empty
|
122
122
|
end
|
123
123
|
end
|
124
124
|
|
@@ -107,7 +107,7 @@ describe 'PureRubyObjects::Country' do
|
|
107
107
|
it_should_behave_like "object invalid in default context"
|
108
108
|
|
109
109
|
it "has errors on name" do
|
110
|
-
@model.errors.on(:name).should_not
|
110
|
+
@model.errors.on(:name).should_not be_empty
|
111
111
|
end
|
112
112
|
|
113
113
|
it "is valid in encyclopedia context" do
|
@@ -35,7 +35,7 @@ describe 'required_field_validator/boolean_type_value_spec' do
|
|
35
35
|
|
36
36
|
it "is valid for committing" do
|
37
37
|
@operation.should be_valid_for_committing
|
38
|
-
@operation.errors.on(:network_connection).should
|
38
|
+
@operation.errors.on(:network_connection).should be_nil
|
39
39
|
end
|
40
40
|
|
41
41
|
it "is not valid for pushing" do
|
@@ -26,10 +26,10 @@ describe "invalid model", :shared => true do
|
|
26
26
|
end
|
27
27
|
|
28
28
|
it "has error messages" do
|
29
|
-
@model.errors.should_not
|
29
|
+
@model.errors.should_not be_empty
|
30
30
|
end
|
31
31
|
|
32
32
|
it "has list of full error messages" do
|
33
|
-
@model.errors.full_messages.should_not
|
33
|
+
@model.errors.full_messages.should_not be_empty
|
34
34
|
end
|
35
35
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -5,6 +5,14 @@ require 'dm-validations'
|
|
5
5
|
require 'dm-types'
|
6
6
|
require 'dm-migrations'
|
7
7
|
|
8
|
+
class Hash
|
9
|
+
def except(*keys)
|
10
|
+
hash = dup
|
11
|
+
keys.each { |key| hash.delete(key) }
|
12
|
+
hash
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
8
16
|
SPEC_ROOT = Pathname(__FILE__).dirname
|
9
17
|
Pathname.glob((SPEC_ROOT + 'fixtures/**/*.rb').to_s).each { |file| require file }
|
10
18
|
Pathname.glob((SPEC_ROOT + 'integration/shared/**/*.rb').to_s).each { |file| require file }
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: dm-validations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease: 6
|
5
|
-
version: 1.1.0.
|
5
|
+
version: 1.1.0.rc3
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Guy van den Berg
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-03-
|
13
|
+
date: 2011-03-10 00:00:00 -08:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -20,7 +20,7 @@ dependencies:
|
|
20
20
|
requirements:
|
21
21
|
- - ~>
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version: 1.1.0.
|
23
|
+
version: 1.1.0.rc3
|
24
24
|
type: :runtime
|
25
25
|
prerelease: false
|
26
26
|
version_requirements: *id001
|
@@ -31,7 +31,7 @@ dependencies:
|
|
31
31
|
requirements:
|
32
32
|
- - ~>
|
33
33
|
- !ruby/object:Gem::Version
|
34
|
-
version: 1.1.0.
|
34
|
+
version: 1.1.0.rc3
|
35
35
|
type: :development
|
36
36
|
prerelease: false
|
37
37
|
version_requirements: *id002
|
@@ -92,6 +92,7 @@ files:
|
|
92
92
|
- lib/dm-validations/formats/url.rb
|
93
93
|
- lib/dm-validations/support/context.rb
|
94
94
|
- lib/dm-validations/support/object.rb
|
95
|
+
- lib/dm-validations/support/ordered_hash.rb
|
95
96
|
- lib/dm-validations/validation_errors.rb
|
96
97
|
- lib/dm-validations/validators/absent_field_validator.rb
|
97
98
|
- lib/dm-validations/validators/acceptance_validator.rb
|
@@ -246,7 +247,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
246
247
|
requirements: []
|
247
248
|
|
248
249
|
rubyforge_project: datamapper
|
249
|
-
rubygems_version: 1.
|
250
|
+
rubygems_version: 1.6.2
|
250
251
|
signing_key:
|
251
252
|
specification_version: 3
|
252
253
|
summary: Library for performing validations on DM models and pure Ruby object
|