dried_interaction 0.1.1 → 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: 4bb3c6571b1286ed2e0f2b327efb07eacb17bb0f6e6edca8355d6081de014bae
4
- data.tar.gz: 44c570ac8b377f963bcde0d78de4bf67e3fbe57bd39ef4216e15c31b686909b5
3
+ metadata.gz: fc03f8cdd296c8e6d4e02569d36c852230788f2e3fcdbfea27190e9dc7b4ceac
4
+ data.tar.gz: 43c97474d74f55612a51bb37b30e6a378b80d1a983d90662efb9fd74f04a67ab
5
5
  SHA512:
6
- metadata.gz: 19f0b433ebed93ee5d903cdf794af2827582642445ea7a21bdb0410915a54807cb9011c42b26ea131c4a3b3fc2e0df5568a63ce93a6e60b0de8fab3a246767bb
7
- data.tar.gz: ddbf74d37d6e4d8cacb5a56d6e6833362122f0b3f3250ea3ddedae6835221a9b016d932b18e70bb3bcdafe6e116b6729f36b329c6912cfdaef9733459e0727cc
6
+ metadata.gz: 174a2ad3439cb18b6e9e929ac2b838daa914d12dbb42c98360bf31bc5b66d79bba0e3fd01d883b73833e8c5ab43a6d431ea59ddf67155ca162ff15489409c76c
7
+ data.tar.gz: 24548abcd09e1b2ee14ca632eb559815fbdb4477982ac2ee890d7c72430bb7155e6cb038bc803976108e2c313a44e07778a29d622f812840a1ab5abd79b162ec
data/.gitignore CHANGED
@@ -6,6 +6,8 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ dried_interaction-*.gem
10
+ Gemfile.lock
9
11
 
10
12
  # rspec failure tracking
11
13
  .rspec_status
data/README.md CHANGED
@@ -18,11 +18,6 @@ Or install it yourself as:
18
18
 
19
19
  $ gem install dried_interaction
20
20
 
21
- ## Why it may be useful to you
22
- 1. Keeps your business logic typed
23
- 2. Validates interactor input data by default
24
- 3. Includes incredible DRY stack gems by default for yours interactors
25
-
26
21
  ## Usage
27
22
  ### Setting up an interactor
28
23
  For setting up an interactor you need to include DriedInteraction into your class.
@@ -63,10 +58,34 @@ class PublishPost
63
58
  end
64
59
  ```
65
60
 
61
+ ### Contract options
62
+ You can pass to contract some options which will customize contract logic.
63
+ 1. `kind`. Explains which type of validator you will use.
64
+ Available values: `simple` (By default) and `extended`
65
+ `simple` kind uses simple `Dry::Schema.Params`
66
+ `extended` kind uses more complex `Dry::Validation.Contract`
67
+
68
+ ```rb
69
+ contract(kind: :simple) do ... end
70
+ # or
71
+ contract(kind: :extended) do ... end
72
+ ```
73
+
74
+ 2. `mode`. Explains how to handle contract validation errors.
75
+ Available values: `strict` (By default) and `soft`
76
+ `strict` mode raises exception when contract check fails.
77
+ `soft` mode returns Failure monad with error info when contract check fails.
78
+
79
+ ```rb
80
+ contract(mode: :strict) do ... end # => raise DriedInteractionError
81
+ # or
82
+ contract(mode: :soft) do ... end # => returns Failure(DriedInteractionError)
83
+ ```
84
+
66
85
  ### Interactor calling
67
86
 
68
87
  ```rb
69
- PublishPost.new.call(user, params) do |interactor|
88
+ PublishPost.new.call({ user: user, params: params }) do |interactor|
70
89
  interactor.success do |post|
71
90
  # handle success
72
91
  end
@@ -77,10 +96,22 @@ PublishPost.new.call(user, params) do |interactor|
77
96
  end
78
97
  ```
79
98
 
80
- ## Additional info
99
+ When you use `soft` mode you can handle contract failure as divided failure case:
100
+ ```rb
101
+ PublishPost.new.call({ user: user, params: params }) do |interactor|
102
+ interactor.success do |post|
103
+ # handle success
104
+ end
105
+
106
+ interactor.failure(DriedInteractionError) do |error|
107
+ # handle contract validation error
108
+ end
81
109
 
82
- 1. You have to return a monad from interactor as result.
83
- 2. Contract failure will be handled as exception.
110
+ interactor.failure do |error|
111
+ # handle failure
112
+ end
113
+ end
114
+ ```
84
115
 
85
116
  ## Contributing
86
117
 
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry/validation'
4
+ require 'dry/schema'
5
+ require 'dried_interaction/instance_methods'
6
+
7
+ module DriedInteraction
8
+ module Dsl
9
+ MODES = %i[strict soft].freeze
10
+ attr_accessor :contract_validator, :contract_mode, :for_method
11
+
12
+ def contract(kind: :simple, mode: MODES.first, &block)
13
+ @contract_mode = resolve_contract_mode(mode)
14
+ @contract_validator = resolve_contract_validator(kind, &block)
15
+ end
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
+
27
+ def resolve_contract_mode(mode)
28
+ MODES.include?(mode.to_sym) ? mode.to_sym : (raise ArgumentError)
29
+ end
30
+
31
+ def resolve_contract_validator(kind, &block)
32
+ case kind
33
+ when :simple
34
+ Dry::Schema.Params(&block)
35
+ when :extended
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)
41
+ else
42
+ raise ArgumentError
43
+ end
44
+ end
45
+ end
46
+ end
@@ -1,19 +1,25 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'dry/monads'
3
4
  require 'dried_interaction/error'
4
5
 
5
6
  module DriedInteraction
6
7
  module InstanceMethods
8
+ include Dry::Monads[:result]
9
+
7
10
  def call(*args)
8
11
  interaction_contract_error('Call method allows only one argument') if args.size != 1
9
-
10
12
  arg = args.first
13
+
11
14
  contract = self.class.contract_validator
15
+ interaction_contract_error('Contract must be filled') if arg && !contract
16
+
12
17
  contract_params = fetch_contract_params(contract)
13
- check_params_presence_in_args(contract_params, arg.keys)
18
+ check_result = check_params_presence_in_args(contract_params, arg.keys)
19
+ return check_result if check_result&.failure?
14
20
 
15
21
  contract_result = contract.(arg)
16
- return interaction_contract_error(contract_result.errors.messages) if contract_result.failure?
22
+ return interaction_contract_error(contract_result.errors) if contract_result.failure?
17
23
 
18
24
  super(arg)
19
25
  end
@@ -32,7 +38,16 @@ module DriedInteraction
32
38
  end
33
39
 
34
40
  def interaction_contract_error(msgs)
35
- raise DriedInteractionError, class: self.class.to_s, errors: Array.wrap(msgs).join('; ')
41
+ error = DriedInteractionError.new(class: self.class.to_s, errors: msgs)
42
+
43
+ case self.class.contract_mode
44
+ when :strict
45
+ raise error
46
+ when :soft
47
+ Failure(error)
48
+ else
49
+ raise ArgumentError
50
+ end
36
51
  end
37
52
  end
38
53
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DriedInteraction
4
- VERSION = '0.1.1'
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.1.1
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-05 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
@@ -140,10 +140,9 @@ files:
140
140
  - Rakefile
141
141
  - bin/console
142
142
  - bin/setup
143
- - dried_interaction-0.1.0.gem
144
143
  - dried_interaction.gemspec
145
144
  - lib/dried_interaction.rb
146
- - lib/dried_interaction/class_methods.rb
145
+ - lib/dried_interaction/dsl.rb
147
146
  - lib/dried_interaction/error.rb
148
147
  - lib/dried_interaction/instance_methods.rb
149
148
  - lib/dried_interaction/version.rb
Binary file
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'dry/validation'
4
- require 'dry/schema'
5
-
6
- module DriedInteraction
7
- module ClassMethods
8
- attr_accessor :contract_validator
9
-
10
- def contract(kind = :simple, &block)
11
- case kind
12
- when :simple
13
- @contract_validator = Dry::Schema.Params(&block)
14
- when :extended
15
- @contract_validator = Dry::Validation.Contract(&block)
16
- else
17
- raise ArgumentError
18
- end
19
- end
20
- end
21
- end