dry-mutations 1.1.1 → 1.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -1
- data/Gemfile.lock +1 -1
- data/lib/dry/mutations/errors/error_atom.rb +10 -0
- data/lib/dry/mutations/extensions/command.rb +15 -3
- data/lib/dry/mutations/extensions/outcome.rb +9 -9
- data/lib/dry/mutations/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9c45806cc2f3f877d39548794e64438c7cf7f2c3
|
4
|
+
data.tar.gz: 464ad2f0135a2ee05ab451be15966cda17542231
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 62264df60af3efed445dd1e336f59f0d39457c39a38949eaa4072d7ec41ac42ec2782b33fbe0a0c8a6a23639df347deb15acd7d6f8b96b58d1f9d0e62a7e1231
|
7
|
+
data.tar.gz: 020d8c019d8b0e5df5f681701b749e655533b28e637529b532de3a6111291d4239872e31862230d91e9dadb9f8d41b90effc021e4d3851c15782c3fbc3c640f1
|
data/.rubocop.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -19,8 +19,18 @@ module Dry
|
|
19
19
|
return nil if set.empty?
|
20
20
|
|
21
21
|
fail TypeError, "Expected: ::Dry::Validation::MessageSet; got: #{set.class}" unless set.is_a?(::Dry::Validation::MessageSet)
|
22
|
+
|
22
23
|
set.map.with_index.with_object(::Mutations::ErrorHash.new) do |(msg, idx), memo|
|
23
24
|
key = msg.path.join('.')
|
25
|
+
last = msg.path.pop
|
26
|
+
tail = msg.path.inject(memo) { |acc, curr| acc[curr.to_s] ||= ::Mutations::ErrorHash.new }
|
27
|
+
tail[last.to_s] = case tail[last.to_s]
|
28
|
+
when ErrorAtom then ::Mutations::ErrorArray.new << tail[last.to_s]
|
29
|
+
when NilClass then ::Mutations::ErrorArray.new
|
30
|
+
when ::Mutations::ErrorArray then tail[last.to_s]
|
31
|
+
end
|
32
|
+
|
33
|
+
tail[last.to_s] << new(last, msg.predicate, msg, message: msg.text, index: idx)
|
24
34
|
memo[key] = new(key, msg.predicate, msg, message: msg.text, index: idx)
|
25
35
|
end
|
26
36
|
end
|
@@ -117,17 +117,29 @@ module Dry
|
|
117
117
|
|
118
118
|
(@errors ||= ::Mutations::ErrorHash.new).tap do |errs|
|
119
119
|
path.inject(errs) do |cur_errors, part|
|
120
|
-
cur_errors[part.
|
120
|
+
cur_errors[part.to_s] ||= ::Mutations::ErrorHash.new
|
121
121
|
end[last] = atom
|
122
|
-
end
|
122
|
+
end[key] = Errors::ErrorAtom.new(key, kind, dry_message, message: message)
|
123
123
|
end
|
124
124
|
|
125
125
|
def messages
|
126
|
-
@messages ||=
|
126
|
+
@messages ||= yield_messages.uniq
|
127
127
|
end
|
128
128
|
|
129
129
|
private
|
130
130
|
|
131
|
+
def yield_messages(flat = [], errors = @errors)
|
132
|
+
return flat unless errors
|
133
|
+
errors = errors.values if errors.is_a?(Hash)
|
134
|
+
errors.each_with_object(flat) do |error, acc|
|
135
|
+
case error
|
136
|
+
when ::Mutations::ErrorHash, ::Mutations::ErrorArray then yield_messages(acc, error)
|
137
|
+
when ::Dry::Mutations::Errors::ErrorAtom then acc << error.dry_message
|
138
|
+
when ::Mutations::ErrorAtom then acc << error.message
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
131
143
|
def schema
|
132
144
|
@schema ||= self.class.schema
|
133
145
|
end
|
@@ -9,12 +9,14 @@ module Dry
|
|
9
9
|
|
10
10
|
attr_reader :outcome, :either
|
11
11
|
|
12
|
+
# rubocop:disable Style/VariableNumber
|
12
13
|
def initialize(outcome)
|
13
14
|
@∨ = outcome.class.instance_variable_get(:@∨)
|
14
15
|
@either = Right(@outcome = outcome).bind do |value|
|
15
16
|
value.public_send(@∨[:success]) ? Right(value.public_send(@∨[:right])) : Left(value.public_send(@∨[:left]))
|
16
17
|
end
|
17
18
|
end
|
19
|
+
# rubocop:enable Style/VariableNumber
|
18
20
|
end
|
19
21
|
|
20
22
|
class Matcher # :nodoc:
|
@@ -23,14 +25,12 @@ module Dry
|
|
23
25
|
resolve: ->(value) { value.either.value }
|
24
26
|
)
|
25
27
|
|
26
|
-
# rubocop:disable Style/BlockDelimiters
|
27
28
|
FAILURE = Dry::Matcher::Case.new(
|
28
|
-
match: ->
|
29
|
+
match: ->(value, *patterns) {
|
29
30
|
value.left? && (patterns.none? || (patterns & value.either.value.keys).any?)
|
30
31
|
},
|
31
|
-
resolve: ->
|
32
|
+
resolve: ->(value) { value.either.value }
|
32
33
|
)
|
33
|
-
# rubocop:enable Style/BlockDelimiters
|
34
34
|
|
35
35
|
# Build the matcher
|
36
36
|
def self.!
|
@@ -41,15 +41,17 @@ module Dry
|
|
41
41
|
private_constant :FAILURE
|
42
42
|
end
|
43
43
|
|
44
|
+
# rubocop:disable Style/VariableNumber
|
44
45
|
def self.prepended base
|
45
46
|
λ = base.instance_methods.method(:include?)
|
46
47
|
base.instance_variable_set(:@∨, {
|
47
|
-
left:
|
48
|
-
right:
|
49
|
-
success:
|
48
|
+
left: %i|errors left|.detect(&λ),
|
49
|
+
right: %i|result output right|.detect(&λ),
|
50
|
+
success: %i|success?|.detect(&λ)
|
50
51
|
}.reject { |_, v| v.nil? }.merge(base.instance_variable_get(:@∨) || {}))
|
51
52
|
fail ArgumentError, "Can not have #{self} #{__callee__} to #{base}: base class must look like an either." unless base.instance_variable_get(:@∨).size == 3
|
52
53
|
end
|
54
|
+
# rubocop:enable Style/VariableNumber
|
53
55
|
singleton_class.send :alias_method, :included, :prepended
|
54
56
|
|
55
57
|
def either
|
@@ -74,7 +76,6 @@ module Dry
|
|
74
76
|
end
|
75
77
|
end
|
76
78
|
|
77
|
-
# rubocop:disable Style/MethodName
|
78
79
|
def self.Either input
|
79
80
|
case input
|
80
81
|
when Class then input.prepend Either unless input.ancestors.include?(Either)
|
@@ -104,7 +105,6 @@ module Dry
|
|
104
105
|
fail ::Mutations::ValidationException.new(outcome.errors) unless outcome.success?
|
105
106
|
end.value
|
106
107
|
end
|
107
|
-
# rubocop:enable Style/MethodName
|
108
108
|
end
|
109
109
|
end
|
110
110
|
end
|