2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
67
|
# File 'lib/hashy_validator/hashy_array_validator.rb', line 2
def validate_each(record, attribute, value)
instance_value = HashyValueValidator.new(value)
unless instance_value.is_valid
record.errors.add(attribute, instance_value.reason)
return false
end
value = instance_value.value
unique_attrs = {}
boolean_attrs = []
validations =
options.stringify_keys.map do |val_attr,val|
if (val.is_a?(HashValidator::Validations::Multiple) && val.validations.include?('boolean')) || (val.is_a?(String) && val == 'boolean')
boolean_attrs << val_attr
[val_attr, val]
elsif val.is_a?(HashValidator::Validations::Multiple) && val.validations.include?('unique')
unique_attrs[val_attr] ||= []
new_val = HashValidator::Validations::Multiple.new(val.validations.reject{|v| v == 'unique'})
val.validations.blank? ? nil : [val_attr, new_val]
elsif val.is_a?(String) && val == 'unique'
unique_attrs[val_attr] ||= []
nil
else
[val_attr, val]
end
end.compact.to_h
value = value.map{ |e| e.stringify_keys.slice(*validations.keys) }
value.each do |t|
boolean_attrs.each do |boolean_attr|
t[boolean_attr] = get_boolean_value(t[boolean_attr]) if t.key?(boolean_attr)
end
unique_attrs.keys.each do |unique_attr|
if unique_attrs[unique_attr].include?(t[unique_attr])
record.errors.add(attribute, "'#{unique_attr}' not unique")
else
unique_attrs[unique_attr] << t[unique_attr]
end
end
validator = HashValidator.validate(t, validations)
unless validator.valid?
validator.errors.each { |k,v| record.errors.add(attribute, "'#{k.to_s}' #{v}") }
end
end
record.send("#{attribute}=", value)
end
|