sequent 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ecf3a14a1b1acf03a8269e158bad28a533c54095
|
4
|
+
data.tar.gz: af4beb48c4dc6323e2baaf5f0047a41fed3fff6e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8d4478a2872b35922943920af4d572933f6cef35ae7c8b872326237687cfd9b4b2dc0ef44c7731ef3ae5dfc245fd5533b512b7e0be11d0bf1ee235e799afc89d
|
7
|
+
data.tar.gz: 20bc101b0fa43c2e04635e390d7529a2b2c1d86c33d45283410c3066a0a645f24dd1eeff1ca937c663ddb39b277d49ca37a3f83e6ce173f74d267571344c6589
|
@@ -6,28 +6,36 @@ module Sequent
|
|
6
6
|
# Validator for associations. Typically used in Sequent::Core::Command,
|
7
7
|
# Sequent::Core::Event and Sequent::Core::ValueObjects.
|
8
8
|
#
|
9
|
+
# When you define attrs that are also value object or array(..) then this class is
|
10
|
+
# automatically used.
|
11
|
+
#
|
9
12
|
# Example:
|
10
13
|
#
|
11
|
-
# class RegisterForTrainingCommand <
|
14
|
+
# class RegisterForTrainingCommand < Sequent::Core::Command
|
12
15
|
# attrs trainee: Person
|
16
|
+
# end
|
13
17
|
#
|
14
|
-
#
|
15
|
-
#
|
18
|
+
# This will register :trainee with the AssociationValidator and is equivilant to
|
19
|
+
#
|
20
|
+
# validates_with Sequent::Core::AssociationValidator, associations: [:trainee]
|
16
21
|
#
|
17
|
-
# end
|
18
22
|
class AssociationValidator < ActiveModel::Validator
|
19
23
|
|
24
|
+
def initialize(options = {})
|
25
|
+
super
|
26
|
+
raise "Must provide ':associations' to validate" unless options[:associations].present?
|
27
|
+
end
|
28
|
+
|
20
29
|
def validate(record)
|
21
30
|
associations = options[:associations]
|
22
31
|
associations = [associations] unless associations.instance_of?(Array)
|
23
32
|
associations.each do |association|
|
24
|
-
next unless association # since ruby 2.0...?
|
25
33
|
value = record.instance_variable_get("@#{association.to_s}")
|
26
34
|
if value && incorrect_type?(value, record, association)
|
27
35
|
record.errors[association] = "is not of type #{record.class.types[association]}"
|
28
36
|
elsif value && value.kind_of?(Array)
|
29
37
|
item_type = record.class.type_for(association).item_type
|
30
|
-
record.errors[association] = "is invalid"
|
38
|
+
record.errors[association] = "is invalid" unless validate_all(value, item_type).all?
|
31
39
|
else
|
32
40
|
record.errors[association] = "is invalid" if value && value.invalid?
|
33
41
|
end
|
@@ -40,14 +48,14 @@ module Sequent
|
|
40
48
|
!value.kind_of?(Array) && record.class.respond_to?(:types) && !value.kind_of?(record.class.types[association])
|
41
49
|
end
|
42
50
|
|
43
|
-
def
|
44
|
-
|
45
|
-
if
|
46
|
-
|
47
|
-
elsif
|
48
|
-
|
51
|
+
def validate_all(values, item_type)
|
52
|
+
values.map do |value|
|
53
|
+
if value.nil?
|
54
|
+
false
|
55
|
+
elsif value.respond_to?(:valid?)
|
56
|
+
value.valid?
|
49
57
|
else
|
50
|
-
|
58
|
+
Sequent::Core::Helpers::ValueValidators.for(item_type).valid_value?(value)
|
51
59
|
end
|
52
60
|
end
|
53
61
|
end
|
@@ -132,6 +132,14 @@ EOS
|
|
132
132
|
value = self.instance_variable_get("@#{field[0]}")
|
133
133
|
if value.respond_to? :validation_errors
|
134
134
|
value.validation_errors.each { |k, v| result["#{field[0].to_s}_#{k.to_s}".to_sym] = v }
|
135
|
+
elsif field[1].class == ArrayWithType and value.present?
|
136
|
+
value
|
137
|
+
.select { |val| val.respond_to?(:validation_errors) }
|
138
|
+
.each_with_index do |val, index|
|
139
|
+
val.validation_errors.each do |k, v|
|
140
|
+
result["#{field[0].to_s}_#{index}_#{k.to_s}".to_sym] = v
|
141
|
+
end
|
142
|
+
end
|
135
143
|
end
|
136
144
|
end
|
137
145
|
prefix ? HashWithIndifferentAccess[result.map { |k, v| ["#{prefix}_#{k}", v] }] : result
|
data/lib/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sequent
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lars Vonk
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2015-08-
|
13
|
+
date: 2015-08-14 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activerecord
|