dried_interaction 0.1.1 → 0.2.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: 2f78458580c4604685a1e5e0115864894059552dce690536518d36f54aabfd08
4
+ data.tar.gz: 1b1e0e2ccfb7e71d0bde3f634895e2b01b8d0d4d1761212edd02dafb0073ee20
5
5
  SHA512:
6
- metadata.gz: 19f0b433ebed93ee5d903cdf794af2827582642445ea7a21bdb0410915a54807cb9011c42b26ea131c4a3b3fc2e0df5568a63ce93a6e60b0de8fab3a246767bb
7
- data.tar.gz: ddbf74d37d6e4d8cacb5a56d6e6833362122f0b3f3250ea3ddedae6835221a9b016d932b18e70bb3bcdafe6e116b6729f36b329c6912cfdaef9733459e0727cc
6
+ metadata.gz: 0c26753cd44cab35cd0b2d2500863ca5febbe0a4637e82419d3e041c7785aaedc6b351c336d96037ecc346d239cdc73739fbc2d3fe15334e48f7f7ed19f2c7cf
7
+ data.tar.gz: b03db178d0c805414d9a97faa50c407f9a75ec4d99ba36d9926a6b8fbeee19881972d6796e627db34ab1eab99bbdf881dd7686b49173cd60f4cadf92f9f440e9
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,6 +58,30 @@ 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
@@ -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, 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
 
@@ -5,14 +5,24 @@ require 'dry/schema'
5
5
 
6
6
  module DriedInteraction
7
7
  module ClassMethods
8
- attr_accessor :contract_validator
8
+ MODES = %i[strict soft].freeze
9
+ attr_accessor :contract_validator, :contract_mode
9
10
 
10
- def contract(kind = :simple, &block)
11
+ def contract(kind: :simple, mode: MODES.first, &block)
12
+ @contract_mode = resolve_contract_mode(mode)
13
+ @contract_validator = resolve_contract_validator(kind, &block)
14
+ end
15
+
16
+ def resolve_contract_mode(mode)
17
+ MODES.include?(mode.to_sym) ? mode.to_sym : (raise ArgumentError)
18
+ end
19
+
20
+ def resolve_contract_validator(kind, &block)
11
21
  case kind
12
22
  when :simple
13
- @contract_validator = Dry::Schema.Params(&block)
23
+ Dry::Schema.Params(&block)
14
24
  when :extended
15
- @contract_validator = Dry::Validation.Contract(&block)
25
+ Dry::Validation.Contract(&block)
16
26
  else
17
27
  raise ArgumentError
18
28
  end
@@ -1,16 +1,20 @@
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
12
 
10
13
  arg = args.first
11
14
  contract = self.class.contract_validator
12
15
  contract_params = fetch_contract_params(contract)
13
- check_params_presence_in_args(contract_params, arg.keys)
16
+ check_result = check_params_presence_in_args(contract_params, arg.keys)
17
+ return check_result if check_result&.failure?
14
18
 
15
19
  contract_result = contract.(arg)
16
20
  return interaction_contract_error(contract_result.errors.messages) if contract_result.failure?
@@ -32,7 +36,20 @@ module DriedInteraction
32
36
  end
33
37
 
34
38
  def interaction_contract_error(msgs)
35
- raise DriedInteractionError, class: self.class.to_s, errors: Array.wrap(msgs).join('; ')
39
+ error = DriedInteractionError.new(class: self.class.to_s, errors: wrap_errors(msgs).join('; '))
40
+
41
+ case self.class.contract_mode
42
+ when :strict
43
+ raise error
44
+ when :soft
45
+ Failure(error)
46
+ else
47
+ raise ArgumentError
48
+ end
49
+ end
50
+
51
+ def wrap_errors(msgs)
52
+ msgs.is_a?(Array) ? msgs : [msgs]
36
53
  end
37
54
  end
38
55
  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.2.0'
5
5
  end
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.2.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-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-initializer
@@ -140,7 +140,6 @@ 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
145
  - lib/dried_interaction/class_methods.rb
Binary file