dried_interaction 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2f78458580c4604685a1e5e0115864894059552dce690536518d36f54aabfd08
4
- data.tar.gz: 1b1e0e2ccfb7e71d0bde3f634895e2b01b8d0d4d1761212edd02dafb0073ee20
3
+ metadata.gz: 160ab6be7540d1ed3d7672df8ee5f1362d540462f6cc72b69f515bed212ac7fb
4
+ data.tar.gz: b9429267e0d60062795fd89e87f980817ab159728b6c0e01e3654acde7b180f7
5
5
  SHA512:
6
- metadata.gz: 0c26753cd44cab35cd0b2d2500863ca5febbe0a4637e82419d3e041c7785aaedc6b351c336d96037ecc346d239cdc73739fbc2d3fe15334e48f7f7ed19f2c7cf
7
- data.tar.gz: b03db178d0c805414d9a97faa50c407f9a75ec4d99ba36d9926a6b8fbeee19881972d6796e627db34ab1eab99bbdf881dd7686b49173cd60f4cadf92f9f440e9
6
+ metadata.gz: dfbe0f6c739498cdf5a0b1f39b35889c91c9c4b523a8d408a7c3aed715f79e704f4b4a2272bbd9eb3afa09226713bc0808a505d7b7dafcc91a0635c858fc01a9
7
+ data.tar.gz: f40319e60a0f525229541fa8db558475958e1ee791d40998ed0fce7a5bb0d5e785688e515a70b20e3b4f7b7ba31c05fafb83bab0e91a1cffb2ee43411346e98c
data/README.md CHANGED
@@ -85,7 +85,7 @@ Available values: `strict` (By default) and `soft`
85
85
  ### Interactor calling
86
86
 
87
87
  ```rb
88
- PublishPost.new.call(user, params) do |interactor|
88
+ PublishPost.new.call({ user: user, params: params }) do |interactor|
89
89
  interactor.success do |post|
90
90
  # handle success
91
91
  end
@@ -98,7 +98,7 @@ end
98
98
 
99
99
  When you use `soft` mode you can handle contract failure as divided failure case:
100
100
  ```rb
101
- PublishPost.new.call(user, params) do |interactor|
101
+ PublishPost.new.call({ user: user, params: params }) do |interactor|
102
102
  interactor.success do |post|
103
103
  # handle success
104
104
  end
@@ -4,7 +4,7 @@ require 'dry/validation'
4
4
  require 'dry/schema'
5
5
 
6
6
  module DriedInteraction
7
- module ClassMethods
7
+ module Dsl
8
8
  MODES = %i[strict soft].freeze
9
9
  attr_accessor :contract_validator, :contract_mode
10
10
 
@@ -13,6 +13,15 @@ module DriedInteraction
13
13
  @contract_validator = resolve_contract_validator(kind, &block)
14
14
  end
15
15
 
16
+ # soft_contract, strict_contract
17
+ MODES.each do |mode|
18
+ define_method("#{mode}_contract") do |args = {}, &block|
19
+ contract(args.merge(mode: mode), &block)
20
+ end
21
+ end
22
+
23
+ private
24
+
16
25
  def resolve_contract_mode(mode)
17
26
  MODES.include?(mode.to_sym) ? mode.to_sym : (raise ArgumentError)
18
27
  end
@@ -23,6 +32,10 @@ module DriedInteraction
23
32
  Dry::Schema.Params(&block)
24
33
  when :extended
25
34
  Dry::Validation.Contract(&block)
35
+ when Dry::Schema::Params
36
+ kind.merge(Dry::Schema.Params(&block))
37
+ when Dry::Validation::Contract
38
+ kind.build(&block)
26
39
  else
27
40
  raise ArgumentError
28
41
  end
@@ -8,16 +8,18 @@ module DriedInteraction
8
8
  include Dry::Monads[:result]
9
9
 
10
10
  def call(*args)
11
- interaction_contract_error('Call method allows only one argument') if args.size != 1
12
-
11
+ return interaction_contract_error('Call method allows only one argument') if args.size != 1
13
12
  arg = args.first
13
+
14
14
  contract = self.class.contract_validator
15
+ return interaction_contract_error('Contract must be filled') if arg && !contract
16
+
15
17
  contract_params = fetch_contract_params(contract)
16
18
  check_result = check_params_presence_in_args(contract_params, arg.keys)
17
19
  return check_result if check_result&.failure?
18
20
 
19
21
  contract_result = contract.(arg)
20
- return interaction_contract_error(contract_result.errors.messages) if contract_result.failure?
22
+ return interaction_contract_error(contract_result.errors) if contract_result.failure?
21
23
 
22
24
  super(arg)
23
25
  end
@@ -36,7 +38,7 @@ module DriedInteraction
36
38
  end
37
39
 
38
40
  def interaction_contract_error(msgs)
39
- error = DriedInteractionError.new(class: self.class.to_s, errors: wrap_errors(msgs).join('; '))
41
+ error = DriedInteractionError.new(class: self.class.to_s, errors: msgs)
40
42
 
41
43
  case self.class.contract_mode
42
44
  when :strict
@@ -47,9 +49,5 @@ module DriedInteraction
47
49
  raise ArgumentError
48
50
  end
49
51
  end
50
-
51
- def wrap_errors(msgs)
52
- msgs.is_a?(Array) ? msgs : [msgs]
53
- end
54
52
  end
55
53
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DriedInteraction
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.1'
5
5
  end
@@ -5,12 +5,12 @@ require 'dry/matcher/result_matcher'
5
5
  require 'dry/monads'
6
6
  require 'dry-initializer'
7
7
 
8
- require 'dried_interaction/class_methods'
8
+ require 'dried_interaction/dsl'
9
9
  require 'dried_interaction/instance_methods'
10
10
 
11
11
  module DriedInteraction
12
12
  def self.included(klass)
13
- klass.extend(ClassMethods)
13
+ klass.extend(Dsl)
14
14
  klass.extend(Dry::Initializer)
15
15
 
16
16
  klass.prepend(InstanceMethods)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dried_interaction
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - gl-pv
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-06 00:00:00.000000000 Z
11
+ date: 2023-04-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-initializer
@@ -142,7 +142,7 @@ files:
142
142
  - bin/setup
143
143
  - dried_interaction.gemspec
144
144
  - lib/dried_interaction.rb
145
- - lib/dried_interaction/class_methods.rb
145
+ - lib/dried_interaction/dsl.rb
146
146
  - lib/dried_interaction/error.rb
147
147
  - lib/dried_interaction/instance_methods.rb
148
148
  - lib/dried_interaction/version.rb