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 +4 -4
- data/.gitignore +2 -0
- data/README.md +40 -9
- data/lib/dried_interaction/dsl.rb +46 -0
- data/lib/dried_interaction/instance_methods.rb +19 -4
- data/lib/dried_interaction/version.rb +1 -1
- data/lib/dried_interaction.rb +2 -2
- metadata +3 -4
- data/dried_interaction-0.1.0.gem +0 -0
- data/lib/dried_interaction/class_methods.rb +0 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fc03f8cdd296c8e6d4e02569d36c852230788f2e3fcdbfea27190e9dc7b4ceac
|
4
|
+
data.tar.gz: 43c97474d74f55612a51bb37b30e6a378b80d1a983d90662efb9fd74f04a67ab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 174a2ad3439cb18b6e9e929ac2b838daa914d12dbb42c98360bf31bc5b66d79bba0e3fd01d883b73833e8c5ab43a6d431ea59ddf67155ca162ff15489409c76c
|
7
|
+
data.tar.gz: 24548abcd09e1b2ee14ca632eb559815fbdb4477982ac2ee890d7c72430bb7155e6cb038bc803976108e2c313a44e07778a29d622f812840a1ab5abd79b162ec
|
data/.gitignore
CHANGED
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
|
-
|
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
|
-
|
83
|
-
|
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
|
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
|
-
|
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
|
data/lib/dried_interaction.rb
CHANGED
@@ -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/
|
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(
|
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.
|
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-
|
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/
|
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
|
data/dried_interaction-0.1.0.gem
DELETED
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
|