dried_interaction 0.2.0 → 0.3.0

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
  SHA256:
3
- metadata.gz: 2f78458580c4604685a1e5e0115864894059552dce690536518d36f54aabfd08
4
- data.tar.gz: 1b1e0e2ccfb7e71d0bde3f634895e2b01b8d0d4d1761212edd02dafb0073ee20
3
+ metadata.gz: fc03f8cdd296c8e6d4e02569d36c852230788f2e3fcdbfea27190e9dc7b4ceac
4
+ data.tar.gz: 43c97474d74f55612a51bb37b30e6a378b80d1a983d90662efb9fd74f04a67ab
5
5
  SHA512:
6
- metadata.gz: 0c26753cd44cab35cd0b2d2500863ca5febbe0a4637e82419d3e041c7785aaedc6b351c336d96037ecc346d239cdc73739fbc2d3fe15334e48f7f7ed19f2c7cf
7
- data.tar.gz: b03db178d0c805414d9a97faa50c407f9a75ec4d99ba36d9926a6b8fbeee19881972d6796e627db34ab1eab99bbdf881dd7686b49173cd60f4cadf92f9f440e9
6
+ metadata.gz: 174a2ad3439cb18b6e9e929ac2b838daa914d12dbb42c98360bf31bc5b66d79bba0e3fd01d883b73833e8c5ab43a6d431ea59ddf67155ca162ff15489409c76c
7
+ data.tar.gz: 24548abcd09e1b2ee14ca632eb559815fbdb4477982ac2ee890d7c72430bb7155e6cb038bc803976108e2c313a44e07778a29d622f812840a1ab5abd79b162ec
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
@@ -2,17 +2,28 @@
2
2
 
3
3
  require 'dry/validation'
4
4
  require 'dry/schema'
5
+ require 'dried_interaction/instance_methods'
5
6
 
6
7
  module DriedInteraction
7
- module ClassMethods
8
+ module Dsl
8
9
  MODES = %i[strict soft].freeze
9
- attr_accessor :contract_validator, :contract_mode
10
+ attr_accessor :contract_validator, :contract_mode, :for_method
10
11
 
11
12
  def contract(kind: :simple, mode: MODES.first, &block)
12
13
  @contract_mode = resolve_contract_mode(mode)
13
14
  @contract_validator = resolve_contract_validator(kind, &block)
14
15
  end
15
16
 
17
+ def soft_contract(args = {}, &block)
18
+ contract(args.merge(mode: :soft), &block)
19
+ end
20
+
21
+ def strict_contract(args = {}, &block)
22
+ contract(args.merge(mode: :strict))
23
+ end
24
+
25
+ private
26
+
16
27
  def resolve_contract_mode(mode)
17
28
  MODES.include?(mode.to_sym) ? mode.to_sym : (raise ArgumentError)
18
29
  end
@@ -23,6 +34,10 @@ module DriedInteraction
23
34
  Dry::Schema.Params(&block)
24
35
  when :extended
25
36
  Dry::Validation.Contract(&block)
37
+ when Dry::Schema::Params
38
+ kind.merge(Dry::Schema.Params(&block))
39
+ when Dry::Validation::Contract
40
+ kind.build(&block)
26
41
  else
27
42
  raise ArgumentError
28
43
  end
@@ -9,15 +9,17 @@ module DriedInteraction
9
9
 
10
10
  def call(*args)
11
11
  interaction_contract_error('Call method allows only one argument') if args.size != 1
12
-
13
12
  arg = args.first
13
+
14
14
  contract = self.class.contract_validator
15
+ 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.0'
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.0
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-11 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