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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3a75d0a0c9aee462be75310c21f148745b72ffdb
4
- data.tar.gz: a342fb6f615e09c1a1be1209a0e3b8e535c656b2
3
+ metadata.gz: 9c45806cc2f3f877d39548794e64438c7cf7f2c3
4
+ data.tar.gz: 464ad2f0135a2ee05ab451be15966cda17542231
5
5
  SHA512:
6
- metadata.gz: 73fd6cacb9b2c24adcb03133745260a4db87962b57f5b2def7faa1af6e22b2c76232d5459649da4f962deee5f244375568e31839336000768b8241973fdaf64a
7
- data.tar.gz: 3eb8c0ff3ba03121948eac99d82a17d7da17f2d96664066618c428afea789eb2f98e709f6010b630fa428adc028a420b51f368c4ce73cec5048928923babd0c6
6
+ metadata.gz: 62264df60af3efed445dd1e336f59f0d39457c39a38949eaa4072d7ec41ac42ec2782b33fbe0a0c8a6a23639df347deb15acd7d6f8b96b58d1f9d0e62a7e1231
7
+ data.tar.gz: 020d8c019d8b0e5df5f681701b749e655533b28e637529b532de3a6111291d4239872e31862230d91e9dadb9f8d41b90effc021e4d3851c15782c3fbc3c640f1
data/.rubocop.yml CHANGED
@@ -23,7 +23,7 @@ Metrics/ModuleLength:
23
23
  Max: 256
24
24
 
25
25
  Metrics/BlockLength:
26
- Max: 64
26
+ Max: 128
27
27
 
28
28
  ################################################################################
29
29
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dry-mutations (1.1.1)
4
+ dry-mutations (1.1.3)
5
5
  activesupport (>= 3.2, < 5)
6
6
  dry-transaction (~> 0.9, < 0.10)
7
7
  dry-validation (~> 0.10)
@@ -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.to_sym] ||= ::Mutations::ErrorHash.new
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 ||= @errors && @errors.values.map(&:dry_message)
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: -> (value, *patterns) {
29
+ match: ->(value, *patterns) {
29
30
  value.left? && (patterns.none? || (patterns & value.either.value.keys).any?)
30
31
  },
31
- resolve: -> (value) { value.either.value }
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: [:errors, :left].detect(&λ),
48
- right: [:result, :output, :right].detect(&λ),
49
- success: [:success?].detect(&λ)
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
@@ -1,5 +1,5 @@
1
1
  module Dry
2
2
  module Mutations
3
- VERSION = '1.1.1'.freeze
3
+ VERSION = '1.1.3'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry-mutations
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aleksei Matiushkin