Class: Dao::Validations::Validator
- Inherits:
-
Object
- Object
- Dao::Validations::Validator
show all
- Includes:
- Common
- Defined in:
- lib/dao/validations/validator.rb
Constant Summary
collapse
- NotBlank =
proc{|value| !value.to_s.strip.empty?}
- Cleared =
'Cleared'.freeze
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Common
#validates_all_of, #validates_any_of, #validates_as_email, #validates_as_phone, #validates_as_url, #validates_case_of, #validates_confirmation_of, #validates_inclusion_of, #validates_length_of, #validates_presence_of, #validates_type_of, #validates_value_of, #validates_word_count_of
Constructor Details
#initialize(*args, &block) ⇒ Validator
Returns a new instance of Validator.
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/dao/validations/validator.rb', line 37
def initialize(*args, &block)
@object = args.shift
@options = Map.options_for(args)
if args.size == 1 and @object and @object.is_a?(Hash)
object_keys = @object.keys.map{|key| key.to_s}
option_keys = %w( object validations errors status )
object_is_options = !object_keys.empty? && (object_keys - option_keys).empty?
if object_is_options
@options = Map.for(@object)
@object = nil
end
end
@object ||= (@options[:object] || Map.new)
@validations ||= (@options[:validations] || Map.new)
@errors ||= (@options[:errors] || Errors.new)
@status ||= (@options[:status] || Status.new)
unless @object.respond_to?(:validator)
@object.send(:extend, Dao::Validations)
@object.validator = self
end
@errors.object = @object
end
|
Instance Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
29
30
31
|
# File 'lib/dao/validations/validator.rb', line 29
def errors
@errors
end
|
#object ⇒ Object
Returns the value of attribute object.
26
27
28
|
# File 'lib/dao/validations/validator.rb', line 26
def object
@object
end
|
#options ⇒ Object
Returns the value of attribute options.
27
28
29
|
# File 'lib/dao/validations/validator.rb', line 27
def options
@options
end
|
#status ⇒ Object
Returns the value of attribute status.
30
31
32
|
# File 'lib/dao/validations/validator.rb', line 30
def status
@status
end
|
#validations ⇒ Object
Returns the value of attribute validations.
28
29
30
|
# File 'lib/dao/validations/validator.rb', line 28
def validations
@validations
end
|
Class Method Details
.mixin(*args, &block) ⇒ Object
19
20
21
22
23
|
# File 'lib/dao/validations/validator.rb', line 19
def mixin(*args, &block)
new(*args, &block).tap do |validator|
validator.mixin = true
end
end
|
Instance Method Details
#_run_validations(errors, list) ⇒ Object
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
# File 'lib/dao/validations/validator.rb', line 224
def _run_validations(errors, list)
Array(list).each do |validations|
validations.each do |keys, chain|
chain.each do |callback|
next unless callback and callback.respond_to?(:to_proc)
number_of_errors = errors.size
value = attributes.get(keys)
returned =
catch(:validation) do
args = Dao.args_for_arity([value, attributes], callback.arity)
prefixing(keys) do
object.instance_exec(*args, &callback)
end
end
errors_added = errors.size > number_of_errors
case returned
when Hash
map = Map.for(returned)
valid = map[:valid]
message = map[:message]
when TrueClass, FalseClass
valid = returned
message = nil
else
valid = !errors_added
message = nil
end
valid = false if errors_added
message ||= callback.options[:message]
message ||= (value.to_s.strip.empty? ? 'is blank' : 'is invalid')
if not valid
errors.add_from_source(keys, callback, message)
else
errors.delete_from_source(keys, callback)
end
end
end
end
end
|
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/dao/validations/validator.rb', line 68
def (object = @object)
attributes =
case
when object.respond_to?(:attributes)
object.attributes
when object.instance_variable_defined?('@attributes')
object.instance_variable_get('@attributes')
when object.is_a?(Map)
object
when object.respond_to?(:to_map)
Map.new(object.to_map)
when object.is_a?(Hash)
Map.new(object)
when object.respond_to?(:to_hash)
Map.new(object.to_hash)
else
raise ArgumentError.new("found no attributes on #{ object.inspect }(#{ object.class.name })")
end
@attributes =
case attributes
when Map
attributes
when Hash
Map.new(attributes)
else
raise(ArgumentError.new("#{ attributes.inspect } (#{ attributes.class })"))
end
@attributes
end
|
#forcing_validity!(boolean = true) ⇒ Object
294
295
296
|
# File 'lib/dao/validations/validator.rb', line 294
def forcing_validity!(boolean = true)
@forcing_validity = !!boolean
end
|
#forcing_validity? ⇒ Boolean
290
291
292
|
# File 'lib/dao/validations/validator.rb', line 290
def forcing_validity?
defined?(@forcing_validity) and @forcing_validity
end
|
#get(key) ⇒ Object
160
161
162
|
# File 'lib/dao/validations/validator.rb', line 160
def get(key)
attributes.get(key_for(key))
end
|
#has(key) ⇒ Object
Also known as:
has?
168
169
170
|
# File 'lib/dao/validations/validator.rb', line 168
def has(key)
attributes.has(key_for(key))
end
|
#key_for(*key) ⇒ Object
156
157
158
|
# File 'lib/dao/validations/validator.rb', line 156
def key_for(*key)
prefix + Array(key).flatten.compact
end
|
#pop_prefix ⇒ Object
148
149
150
|
# File 'lib/dao/validations/validator.rb', line 148
def pop_prefix
stack.prefixes.pop
end
|
#prefix ⇒ Object
152
153
154
|
# File 'lib/dao/validations/validator.rb', line 152
def prefix
stack.prefixes.flatten.compact
end
|
#prefixing(*prefix, &block) ⇒ Object
Also known as:
validating
132
133
134
135
136
137
138
139
140
|
# File 'lib/dao/validations/validator.rb', line 132
def prefixing(*prefix, &block)
prefix = Array(prefix).flatten.compact
push_prefix(prefix)
begin
block.call(*[prefix].slice(0, block.arity))
ensure
pop_prefix
end
end
|
#push_prefix(prefix) ⇒ Object
143
144
145
146
|
# File 'lib/dao/validations/validator.rb', line 143
def push_prefix(prefix)
prefix = Array(prefix).flatten.compact
stack.prefixes.push(prefix)
end
|
#reset ⇒ Object
308
309
310
311
312
313
314
|
# File 'lib/dao/validations/validator.rb', line 308
def reset
errors.clear!
status.update(:ok)
forcing_validity!(false)
validated!(false)
self
end
|
#run_validations(list = validations_list) ⇒ Object
Also known as:
run_validations!, validate
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
# File 'lib/dao/validations/validator.rb', line 196
def run_validations(list = validations_list)
loop do
stack.validations.push(Map.new)
_run_validations(errors, list)
added = stack.validations.pop
break if added.empty?
list = [added]
end
if status.ok? and !errors.empty?
status.source = errors
status.update(412)
end
if status == 412 and status.source == errors and errors.empty?
status.update(200)
end
errors
ensure
validated!(true)
end
|
#set(key, val) ⇒ Object
164
165
166
|
# File 'lib/dao/validations/validator.rb', line 164
def set(key, val)
attributes.set(key_for(key), val)
end
|
#stack ⇒ Object
128
129
130
|
# File 'lib/dao/validations/validator.rb', line 128
def stack
@stack ||= Map[:validations, [], :prefixes, []]
end
|
#valid! ⇒ Object
286
287
288
|
# File 'lib/dao/validations/validator.rb', line 286
def valid!
@forcing_validity = true
end
|
#valid?(*args) ⇒ Boolean
298
299
300
301
302
303
304
305
306
|
# File 'lib/dao/validations/validator.rb', line 298
def valid?(*args)
if forcing_validity?
true
else
options = Map.options_for!(args)
run_validations
errors.empty? and status.ok?
end
end
|
#validate! ⇒ Object
281
282
283
284
|
# File 'lib/dao/validations/validator.rb', line 281
def validate!
raise Error.new("#{ object.class.name } is invalid!") unless valid?
object
end
|
#validated!(boolean = true) ⇒ Object
277
278
279
|
# File 'lib/dao/validations/validator.rb', line 277
def validated!(boolean = true)
@validated = !!boolean
end
|
#validated? ⇒ Boolean
272
273
274
275
|
# File 'lib/dao/validations/validator.rb', line 272
def validated?
@validated = false unless defined?(@validated)
@validated
end
|
#validates(*args, &block) ⇒ Object
Also known as:
add
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/dao/validations/validator.rb', line 100
def validates(*args, &block)
block = args.pop if args.last.respond_to?(:call)
block ||= NotBlank
callback = Callback.new(options, &block)
options = Map.options_for!(args)
key = key_for(args)
validations = stack.validations.last || self.validations
validations[key] ||= Callback::Chain.new
validations[key].add(callback)
callback
end
|
#validates_each(*args, &block) ⇒ Object
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/dao/validations/validator.rb', line 113
def validates_each(*args, &block)
options = Map.options_for!(args)
key = key_for(args)
args.push(options)
validates(*args) do |list|
Array(list).each_with_index do |item, index|
args = Dao.args_for_arity([item], block.arity)
validates(index, &block)
end
true
end
end
|
#validations_list ⇒ Object
192
193
194
|
# File 'lib/dao/validations/validator.rb', line 192
def validations_list
validations_search_path.map{|object| object.validator.validations}.uniq
end
|
#validations_search_path ⇒ Object
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
# File 'lib/dao/validations/validator.rb', line 174
def validations_search_path
@validations_search_path ||= (
if mixin?
list = [
object.respond_to?(:validator) ? object : nil,
object.class.ancestors.map{|ancestor| ancestor.respond_to?(:validator) ? ancestor : nil}
]
list.flatten!
list.compact!
list.reverse!
list.uniq!
list
else
[self]
end
)
end
|
#validator ⇒ Object
10
11
12
|
# File 'lib/dao/validations/validator.rb', line 10
def validator
self
end
|
#validator=(validator) ⇒ Object
14
15
16
|
# File 'lib/dao/validations/validator.rb', line 14
def validator=(validator)
raise NotImplementedError
end
|