interaktor 0.6.0.pre2 → 0.7.0.pre
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 +4 -4
- data/.github/workflows/publish.yml +2 -2
- data/.github/workflows/tests.yml +2 -2
- data/.ruby-version +1 -1
- data/README.md +40 -0
- data/interaktor.gemspec +1 -1
- data/lib/interaktor/callable.rb +29 -3
- data/lib/interaktor/error/invalid_organizer_failure_definition_error.rb +16 -0
- data/lib/interaktor/error/success_in_hook_error.rb +8 -0
- data/lib/interaktor/failure.rb +17 -1
- data/lib/interaktor/hooks.rb +11 -1
- data/lib/interaktor/organizer.rb +48 -2
- data/lib/interaktor.rb +10 -1
- data/spec/interaktor/failure_spec.rb +36 -0
- data/spec/interaktor/organizer_spec.rb +412 -8
- data/spec/spec_helper.rb +5 -3
- data/spec/support/lint.rb +172 -3
- metadata +8 -8
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d6f6275ae6d81c54b883e5d5f97662cb105cd882243f65a0bcdcdea98e62d141
|
|
4
|
+
data.tar.gz: d13a41c22442f2a3332b0b7ef765749a5350f8a656c4330d4770aae72745716c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0277f71f02285b1bf95dee03b4718f400ede0d63d291493f470770edcbd0bad78691612832a673acf8e983468d13ab72510d486eade2e377871ca67aabdcdb77
|
|
7
|
+
data.tar.gz: 85eb23320cf8c8a3bdd7ff84830a2066b421aa26183292127857cf1a960fc49e22e356d7cf254a4b8afc267680d2dfaac02daed19affacfd24defec86646894a
|
|
@@ -13,11 +13,11 @@ jobs:
|
|
|
13
13
|
runs-on: ubuntu-latest
|
|
14
14
|
|
|
15
15
|
steps:
|
|
16
|
-
- uses: actions/checkout@
|
|
16
|
+
- uses: actions/checkout@v7
|
|
17
17
|
- name: Set up Ruby
|
|
18
18
|
uses: ruby/setup-ruby@v1
|
|
19
19
|
with:
|
|
20
|
-
ruby-version:
|
|
20
|
+
ruby-version: "4.0"
|
|
21
21
|
|
|
22
22
|
- name: Publish to RubyGems
|
|
23
23
|
run: |
|
data/.github/workflows/tests.yml
CHANGED
|
@@ -8,13 +8,13 @@ jobs:
|
|
|
8
8
|
fail-fast: false
|
|
9
9
|
matrix:
|
|
10
10
|
os: [ubuntu-latest, macos-latest]
|
|
11
|
-
ruby: [3.
|
|
11
|
+
ruby: ["3.3", "3.4", "4.0", head, debug]
|
|
12
12
|
runs-on: ${{ matrix.os }}
|
|
13
13
|
continue-on-error: ${{ endsWith(matrix.ruby, 'head') || matrix.ruby == 'debug' }}
|
|
14
14
|
env:
|
|
15
15
|
BUNDLE_GEMFILE: "Gemfile.ci"
|
|
16
16
|
steps:
|
|
17
|
-
- uses: actions/checkout@
|
|
17
|
+
- uses: actions/checkout@v7
|
|
18
18
|
- name: Set up Ruby
|
|
19
19
|
uses: ruby/setup-ruby@v1
|
|
20
20
|
with:
|
data/.ruby-version
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
4.0.6
|
data/README.md
CHANGED
|
@@ -208,6 +208,8 @@ end
|
|
|
208
208
|
|
|
209
209
|
If `#fail!` is called, any code defined in the hook after the call to the interaktor will not be run.
|
|
210
210
|
|
|
211
|
+
A hook can call `#fail!`, but not `#success!`. Only the `call` method of an interaktor can call `#success!`.
|
|
212
|
+
|
|
211
213
|
#### Hook sequence
|
|
212
214
|
|
|
213
215
|
Before hooks are invoked in the order in which they were defined while after hooks are invoked in the opposite order. Around hooks are invoked outside of any defined before and after hooks. For example:
|
|
@@ -343,6 +345,42 @@ Any arguments which are _not_ accepted by the next interaktor (listed as an inpu
|
|
|
343
345
|
If the organizer specifies any success attributes, the final interaktor in the
|
|
344
346
|
organized list must also specify those success attributes.
|
|
345
347
|
|
|
348
|
+
#### Failure
|
|
349
|
+
|
|
350
|
+
If any one of the organized interaktors fails, the organizer fails too.
|
|
351
|
+
|
|
352
|
+
An organizer declares its own failure attributes, like any other interaktor. The contract is between the caller and the organizer, not with whatever interaktors happen to be called by the organizer. As a result, the organizer's failure attributes don't need to match the failure attributes of the organized interaktors in any way. An organizer that declares failure attributes must define a `#failure_attributes_from` method. The method takes the interaction of the organized interaktor that failed, and returns a hash of failure attributes for the organizer.
|
|
353
|
+
|
|
354
|
+
```ruby
|
|
355
|
+
class PlaceOrder
|
|
356
|
+
include Interaktor::Organizer
|
|
357
|
+
|
|
358
|
+
failure do
|
|
359
|
+
attribute :reason
|
|
360
|
+
validates :reason, inclusion: [:invalid_order, :card_declined]
|
|
361
|
+
|
|
362
|
+
attribute :decline_code
|
|
363
|
+
end
|
|
364
|
+
|
|
365
|
+
organize CreateOrder, ChargeCard, SendThankYou
|
|
366
|
+
|
|
367
|
+
def failure_attributes_from(failed_interaction)
|
|
368
|
+
case failed_interaction
|
|
369
|
+
when CreateOrder::Interaction
|
|
370
|
+
{reason: :invalid_order}
|
|
371
|
+
when ChargeCard::Interaction
|
|
372
|
+
{reason: :card_declined, decline_code: failed_interaction.decline_code}
|
|
373
|
+
end
|
|
374
|
+
end
|
|
375
|
+
end
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
Every interaktor class has an automatically-defined `Interaction` class. The failure attributes of that interaktor are available on the interaction.
|
|
379
|
+
|
|
380
|
+
The returned attributes are validated like the attributes passed to `fail!`. If the method returns `nil`, the organizer fails with every failure attribute set to `nil`. In the example above, that would raise `Interaktor::Error::AttributeValidationError`, because `reason` would be missing. To treat a failure as an error instead, raise an exception from the method.
|
|
381
|
+
|
|
382
|
+
An organizer without failure attributes fails with no attributes, and must not define `failure_attributes_from`.
|
|
383
|
+
|
|
346
384
|
#### Rollback
|
|
347
385
|
|
|
348
386
|
If any one of the organized interaktors fails, the organizer stops. If the `ChargeCard` interaktor fails, `SendThankYou` is never called.
|
|
@@ -383,6 +421,8 @@ end
|
|
|
383
421
|
|
|
384
422
|
**NOTE:** The interaktor that fails is _not_ rolled back. Because every interaktor should have a single purpose, there should be no need to clean up after any failed interaktor. This is why the rollback method above can access the `order` success attribute - rollback is only called on successful interaktors.
|
|
385
423
|
|
|
424
|
+
The interaktors that had already run are also rolled back when a later interaktor raises an exception instead of failing. This only happens for exceptions that inherit from `StandardError`. Other exceptions, such as `Interrupt`, the exception that `Timeout.timeout` raises, or `Sidekiq::Shutdown`, usually mean the process or thread is being stopped, and running more code at that point is risky. When one of those is raised, nothing is rolled back.
|
|
425
|
+
|
|
386
426
|
## Testing interaktors
|
|
387
427
|
|
|
388
428
|
When written correctly, an interaktor is easy to test because it only _does_ one thing. Take the following interaktor:
|
data/interaktor.gemspec
CHANGED
data/lib/interaktor/callable.rb
CHANGED
|
@@ -22,9 +22,7 @@ module Interaktor::Callable
|
|
|
22
22
|
|
|
23
23
|
klass.check_for_disallowed_attribute_names!
|
|
24
24
|
|
|
25
|
-
klass.attribute_names.each
|
|
26
|
-
define_method(name) { @interaction.send(name) }
|
|
27
|
-
end
|
|
25
|
+
klass.attribute_names.each { |name| define_attribute_reader(name) }
|
|
28
26
|
end
|
|
29
27
|
end
|
|
30
28
|
|
|
@@ -53,6 +51,8 @@ module Interaktor::Callable
|
|
|
53
51
|
const_set(:SuccessAttributesModel, klass)
|
|
54
52
|
|
|
55
53
|
klass.check_for_disallowed_attribute_names!
|
|
54
|
+
|
|
55
|
+
klass.attribute_names.each { |name| define_attribute_reader(name) }
|
|
56
56
|
end
|
|
57
57
|
end
|
|
58
58
|
|
|
@@ -80,6 +80,32 @@ module Interaktor::Callable
|
|
|
80
80
|
|
|
81
81
|
private
|
|
82
82
|
|
|
83
|
+
# Define a method on the interaktor that returns the value of an input or
|
|
84
|
+
# success attribute.
|
|
85
|
+
#
|
|
86
|
+
# @param name [String]
|
|
87
|
+
#
|
|
88
|
+
# @return [void]
|
|
89
|
+
def define_attribute_reader(name)
|
|
90
|
+
# Rejects the name of a method that the gem defines on every interaktor,
|
|
91
|
+
# because the attribute method would replace it.
|
|
92
|
+
gem_method = [Interaktor, Interaktor::Hooks].any? do |mod|
|
|
93
|
+
mod.method_defined?(name) || mod.private_method_defined?(name)
|
|
94
|
+
end
|
|
95
|
+
raise ArgumentError, "Disallowed attribute name(s): #{name}" if gem_method
|
|
96
|
+
|
|
97
|
+
self::AttributeMethods.define_method(name) do
|
|
98
|
+
# Reads the attribute objects directly, because the interaction stops
|
|
99
|
+
# returning input attributes once it has finished. A success attribute
|
|
100
|
+
# replaces an input attribute of the same name once the interaktor has
|
|
101
|
+
# succeeded.
|
|
102
|
+
[@interaction.success_object, @interaction.input_object]
|
|
103
|
+
.compact
|
|
104
|
+
.find { |attributes| attributes.attribute_names.include?(name) }
|
|
105
|
+
&.send(name)
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
83
109
|
# The main execution method triggered by the public `#call` or `#call!`
|
|
84
110
|
# methods.
|
|
85
111
|
#
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
class Interaktor::Error::InvalidOrganizerFailureDefinitionError < Interaktor::Error::Base
|
|
2
|
+
def message
|
|
3
|
+
if defined?(interaktor::FailureAttributesModel)
|
|
4
|
+
<<~MSG.gsub(/\s+/, " ").strip
|
|
5
|
+
#{interaktor} organizer declares failure attributes, so it must define
|
|
6
|
+
a `failure_attributes_from` method that builds them from the failed
|
|
7
|
+
interaction of an organized interaktor.
|
|
8
|
+
MSG
|
|
9
|
+
else
|
|
10
|
+
<<~MSG.gsub(/\s+/, " ").strip
|
|
11
|
+
#{interaktor} organizer defines a `failure_attributes_from` method, but
|
|
12
|
+
declares no failure attributes for it to build.
|
|
13
|
+
MSG
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
data/lib/interaktor/failure.rb
CHANGED
|
@@ -8,6 +8,22 @@ class Interaktor::Failure < StandardError
|
|
|
8
8
|
# error was raised
|
|
9
9
|
def initialize(interaction = nil)
|
|
10
10
|
@interaction = interaction
|
|
11
|
-
super
|
|
11
|
+
super(build_message)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
private
|
|
15
|
+
|
|
16
|
+
# Build a human-readable message from the failure attributes, if any were
|
|
17
|
+
# provided to `fail!`. When there are none, returns nil so that
|
|
18
|
+
# StandardError falls back to its default (the exception class name).
|
|
19
|
+
#
|
|
20
|
+
# @return [String, nil]
|
|
21
|
+
def build_message
|
|
22
|
+
attributes = interaction&.failure_object&.attributes
|
|
23
|
+
return if attributes.nil? || attributes.empty?
|
|
24
|
+
|
|
25
|
+
attributes
|
|
26
|
+
.map { |name, value| "#{name}: #{value.inspect}" }
|
|
27
|
+
.join(", ")
|
|
12
28
|
end
|
|
13
29
|
end
|
data/lib/interaktor/hooks.rb
CHANGED
|
@@ -325,6 +325,16 @@ module Interaktor::Hooks
|
|
|
325
325
|
#
|
|
326
326
|
# Returns nothing.
|
|
327
327
|
def run_hook(hook, *args)
|
|
328
|
-
|
|
328
|
+
hook_finished = false
|
|
329
|
+
|
|
330
|
+
# `success!` ends the interaktor by throwing `:early_return`. A throw from
|
|
331
|
+
# inside `call` is caught before it gets here, so a throw caught here came
|
|
332
|
+
# from the hook itself.
|
|
333
|
+
catch(:early_return) do
|
|
334
|
+
hook.is_a?(Symbol) ? send(hook, *args) : instance_exec(*args, &hook)
|
|
335
|
+
hook_finished = true
|
|
336
|
+
end
|
|
337
|
+
|
|
338
|
+
raise Interaktor::Error::SuccessInHookError.new(self.class) if !hook_finished
|
|
329
339
|
end
|
|
330
340
|
end
|
data/lib/interaktor/organizer.rb
CHANGED
|
@@ -36,15 +36,39 @@ module Interaktor::Organizer
|
|
|
36
36
|
module InstanceMethods
|
|
37
37
|
# Invoke the organized Interaktors. An Interaktor::Organizer is expected
|
|
38
38
|
# NOT to define its own `#call` in favor of this default implementation.
|
|
39
|
+
# Fails if an organized interaktor fails.
|
|
40
|
+
#
|
|
41
|
+
# @raises [Interaktor::Failure] if an organized interaktor fails
|
|
39
42
|
#
|
|
40
43
|
# @return [void]
|
|
41
44
|
def call
|
|
45
|
+
@finished_interactions = []
|
|
46
|
+
|
|
47
|
+
# An organizer that declares failure attributes must build them from the
|
|
48
|
+
# failed interaction of an organized interaktor, so it must define
|
|
49
|
+
# `failure_attributes_from`. One that declares none has nothing to build.
|
|
50
|
+
declares_failure_attributes = !!defined?(self.class::FailureAttributesModel)
|
|
51
|
+
if declares_failure_attributes != respond_to?(:failure_attributes_from, true)
|
|
52
|
+
raise Interaktor::Error::InvalidOrganizerFailureDefinitionError.new(self.class)
|
|
53
|
+
end
|
|
54
|
+
|
|
42
55
|
latest_interaction = nil
|
|
43
56
|
|
|
44
57
|
self.class.organized.each do |interaktor|
|
|
45
|
-
|
|
46
|
-
|
|
58
|
+
# `.call` raises the failure of any other interaktor that the organized
|
|
59
|
+
# interaktor calls, so a failed interaction here is always its own.
|
|
60
|
+
interaction = interaktor.call(latest_interaction || @interaction)
|
|
61
|
+
|
|
62
|
+
if interaction.failure?
|
|
63
|
+
if declares_failure_attributes
|
|
64
|
+
fail!(failure_attributes_from(interaction) || {})
|
|
65
|
+
else
|
|
66
|
+
fail!
|
|
67
|
+
end
|
|
47
68
|
end
|
|
69
|
+
|
|
70
|
+
@finished_interactions << interaction
|
|
71
|
+
latest_interaction = interaction
|
|
48
72
|
end
|
|
49
73
|
|
|
50
74
|
if defined?(self.class::SuccessAttributesModel)
|
|
@@ -55,6 +79,28 @@ module Interaktor::Organizer
|
|
|
55
79
|
&.slice(*self.class::SuccessAttributesModel.attribute_names) || {}
|
|
56
80
|
)
|
|
57
81
|
end
|
|
82
|
+
rescue
|
|
83
|
+
# Rolls back when an organized interaktor raises, and also when one fails,
|
|
84
|
+
# because the organizer then raises its own failure.
|
|
85
|
+
roll_back_finished_interactions
|
|
86
|
+
raise
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# Roll back the organized interaktors, in reverse order. An organizer that
|
|
90
|
+
# defines its own `#rollback` must call `super`.
|
|
91
|
+
#
|
|
92
|
+
# @return [void]
|
|
93
|
+
def rollback
|
|
94
|
+
roll_back_finished_interactions
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
private
|
|
98
|
+
|
|
99
|
+
# Roll back the organized interaktors that finished, in reverse order.
|
|
100
|
+
#
|
|
101
|
+
# @return [void]
|
|
102
|
+
def roll_back_finished_interactions
|
|
103
|
+
@finished_interactions&.reverse_each(&:rollback!)
|
|
58
104
|
end
|
|
59
105
|
end
|
|
60
106
|
end
|
data/lib/interaktor.rb
CHANGED
|
@@ -17,6 +17,12 @@ module Interaktor
|
|
|
17
17
|
|
|
18
18
|
interaction_class = Class.new(Interaktor::Interaction)
|
|
19
19
|
base.const_set(:Interaction, interaction_class)
|
|
20
|
+
|
|
21
|
+
# Holds the methods that return input and success attributes. Methods
|
|
22
|
+
# defined in the interaktor class, or in modules that it includes later,
|
|
23
|
+
# take priority over them.
|
|
24
|
+
base.const_set(:AttributeMethods, Module.new)
|
|
25
|
+
include base::AttributeMethods
|
|
20
26
|
end
|
|
21
27
|
end
|
|
22
28
|
|
|
@@ -57,7 +63,10 @@ module Interaktor
|
|
|
57
63
|
# context is failed or any error is raised, the context is rolled back.
|
|
58
64
|
def run
|
|
59
65
|
run!
|
|
60
|
-
rescue Interaktor::Failure
|
|
66
|
+
rescue Interaktor::Failure => e
|
|
67
|
+
# A failure of another interaktor, which this one called with `.call!`, is
|
|
68
|
+
# an error for this interaktor rather than a failure of it.
|
|
69
|
+
raise if !e.interaction.equal?(@interaction)
|
|
61
70
|
end
|
|
62
71
|
|
|
63
72
|
# Invoke an Interaktor instance along with all defined hooks, typically used
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
RSpec.describe Interaktor::Failure do
|
|
2
|
+
describe "#message" do
|
|
3
|
+
it "builds a message from the failure attributes" do
|
|
4
|
+
interaktor = FakeInteraktor.build_interaktor do
|
|
5
|
+
failure do
|
|
6
|
+
attribute :error
|
|
7
|
+
attribute :code
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def call
|
|
11
|
+
fail!(error: "not found", code: 404)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
expect {
|
|
16
|
+
interaktor.call!
|
|
17
|
+
}.to raise_error(described_class, 'error: "not found", code: 404')
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "falls back to the default message when there are no failure attributes" do
|
|
21
|
+
interaktor = FakeInteraktor.build_interaktor do
|
|
22
|
+
def call
|
|
23
|
+
fail!
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
expect {
|
|
28
|
+
interaktor.call!
|
|
29
|
+
}.to raise_error(described_class, described_class.name)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
it "falls back to the default message when the interaction is absent" do
|
|
33
|
+
expect(described_class.new.message).to eq described_class.name
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -73,8 +73,8 @@ RSpec.describe Interaktor::Organizer do
|
|
|
73
73
|
|
|
74
74
|
allow(organizer).to receive(:organized).and_return([interaktor1, interaktor2])
|
|
75
75
|
|
|
76
|
-
expect(interaktor1).to receive(:call
|
|
77
|
-
expect(interaktor2).to receive(:call
|
|
76
|
+
expect(interaktor1).to receive(:call).once.ordered.and_call_original
|
|
77
|
+
expect(interaktor2).to receive(:call).once.ordered.and_call_original
|
|
78
78
|
|
|
79
79
|
result = organizer.call(foo: "asdf")
|
|
80
80
|
|
|
@@ -104,8 +104,8 @@ RSpec.describe Interaktor::Organizer do
|
|
|
104
104
|
|
|
105
105
|
allow(organizer).to receive(:organized).and_return([interaktor1, interaktor2])
|
|
106
106
|
|
|
107
|
-
expect(interaktor1).to receive(:call
|
|
108
|
-
expect(interaktor2).to receive(:call
|
|
107
|
+
expect(interaktor1).to receive(:call).once.ordered.and_call_original
|
|
108
|
+
expect(interaktor2).to receive(:call).once.ordered.and_call_original
|
|
109
109
|
|
|
110
110
|
organizer.call(foo: "whatever")
|
|
111
111
|
end
|
|
@@ -124,8 +124,8 @@ RSpec.describe Interaktor::Organizer do
|
|
|
124
124
|
|
|
125
125
|
allow(organizer).to receive(:organized).and_return([interaktor1, interaktor2])
|
|
126
126
|
|
|
127
|
-
expect(interaktor1).to receive(:call
|
|
128
|
-
expect(interaktor2).to receive(:call
|
|
127
|
+
expect(interaktor1).to receive(:call).once.ordered.and_call_original
|
|
128
|
+
expect(interaktor2).to receive(:call).once.ordered.and_call_original
|
|
129
129
|
|
|
130
130
|
organizer.call(foo: "whatever")
|
|
131
131
|
end
|
|
@@ -153,8 +153,8 @@ RSpec.describe Interaktor::Organizer do
|
|
|
153
153
|
|
|
154
154
|
allow(organizer).to receive(:organized).and_return([interaktor1, interaktor2])
|
|
155
155
|
|
|
156
|
-
expect(interaktor1).to receive(:call
|
|
157
|
-
expect(interaktor2).to receive(:call
|
|
156
|
+
expect(interaktor1).to receive(:call).once.ordered.and_call_original
|
|
157
|
+
expect(interaktor2).to receive(:call).once.ordered.and_call_original
|
|
158
158
|
|
|
159
159
|
organizer.call(foo: "whatever")
|
|
160
160
|
end
|
|
@@ -202,6 +202,410 @@ RSpec.describe Interaktor::Organizer do
|
|
|
202
202
|
end
|
|
203
203
|
end
|
|
204
204
|
|
|
205
|
+
it "raises an exception when an organized interaktor calls #success! in a hook" do
|
|
206
|
+
organizer = FakeInteraktor.build_interaktor(type: described_class)
|
|
207
|
+
interaktor1 = FakeInteraktor.build_interaktor("Interaktor1") do
|
|
208
|
+
before { success! }
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
allow(organizer).to receive(:organized).and_return([interaktor1])
|
|
212
|
+
|
|
213
|
+
expect {
|
|
214
|
+
organizer.call
|
|
215
|
+
}.to raise_error(Interaktor::Error::SuccessInHookError) { |error|
|
|
216
|
+
expect(error.interaktor).to be interaktor1
|
|
217
|
+
}
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
context "when an organized interaktor fails" do
|
|
221
|
+
it "fails without attributes and doesn't call the later interaktors when the organizer declares no failure attributes" do
|
|
222
|
+
organizer = FakeInteraktor.build_interaktor(type: described_class)
|
|
223
|
+
interaktor1 = FakeInteraktor.build_interaktor("Interaktor1") do
|
|
224
|
+
failure { attribute :reason }
|
|
225
|
+
|
|
226
|
+
def call
|
|
227
|
+
fail!(reason: :nope)
|
|
228
|
+
end
|
|
229
|
+
end
|
|
230
|
+
interaktor2 = FakeInteraktor.build_interaktor("Interaktor2")
|
|
231
|
+
|
|
232
|
+
allow(organizer).to receive(:organized).and_return([interaktor1, interaktor2])
|
|
233
|
+
|
|
234
|
+
expect(interaktor2).not_to receive(:call)
|
|
235
|
+
|
|
236
|
+
result = organizer.call
|
|
237
|
+
|
|
238
|
+
expect(result.failure?).to be true
|
|
239
|
+
expect(result.failure_object).to be nil
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
it "fails with the attributes the organizer builds from the failed interaction" do
|
|
243
|
+
organizer = FakeInteraktor.build_interaktor(type: described_class) do
|
|
244
|
+
failure do
|
|
245
|
+
attribute :reason
|
|
246
|
+
validates :reason, inclusion: [:interaktor2_failed]
|
|
247
|
+
|
|
248
|
+
attribute :detail
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
def failure_attributes_from(interaction)
|
|
252
|
+
case interaction
|
|
253
|
+
when Interaktor2::Interaction
|
|
254
|
+
{reason: :interaktor2_failed, detail: interaction.message}
|
|
255
|
+
end
|
|
256
|
+
end
|
|
257
|
+
end
|
|
258
|
+
interaktor1 = FakeInteraktor.build_interaktor("Interaktor1")
|
|
259
|
+
interaktor2 = FakeInteraktor.build_interaktor("Interaktor2") do
|
|
260
|
+
failure { attribute :message }
|
|
261
|
+
|
|
262
|
+
def call
|
|
263
|
+
fail!(message: "it broke")
|
|
264
|
+
end
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
allow(organizer).to receive(:organized).and_return([interaktor1, interaktor2])
|
|
268
|
+
|
|
269
|
+
result = organizer.call
|
|
270
|
+
|
|
271
|
+
expect(result.failure?).to be true
|
|
272
|
+
expect(result.reason).to eq :interaktor2_failed
|
|
273
|
+
expect(result.detail).to eq "it broke"
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
it "raises Interaktor::Failure for the organizer interaction from .call!" do
|
|
277
|
+
organizer = FakeInteraktor.build_interaktor(type: described_class) do
|
|
278
|
+
failure { attribute :reason }
|
|
279
|
+
|
|
280
|
+
def failure_attributes_from(interaction)
|
|
281
|
+
{reason: :interaktor1_failed}
|
|
282
|
+
end
|
|
283
|
+
end
|
|
284
|
+
interaktor1 = FakeInteraktor.build_interaktor("Interaktor1") do
|
|
285
|
+
def call
|
|
286
|
+
fail!
|
|
287
|
+
end
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
allow(organizer).to receive(:organized).and_return([interaktor1])
|
|
291
|
+
|
|
292
|
+
expect {
|
|
293
|
+
organizer.call!
|
|
294
|
+
}.to raise_error(Interaktor::Failure) { |error|
|
|
295
|
+
expect(error.interaction).to be_an organizer::Interaction
|
|
296
|
+
expect(error.interaction.reason).to eq :interaktor1_failed
|
|
297
|
+
}
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
it "raises when the failure attributes that the organizer builds aren't valid" do
|
|
301
|
+
organizer = FakeInteraktor.build_interaktor(type: described_class) do
|
|
302
|
+
failure do
|
|
303
|
+
attribute :reason
|
|
304
|
+
validates :reason, presence: true
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
def failure_attributes_from(interaction)
|
|
308
|
+
nil
|
|
309
|
+
end
|
|
310
|
+
end
|
|
311
|
+
interaktor1 = FakeInteraktor.build_interaktor("Interaktor1") do
|
|
312
|
+
def call
|
|
313
|
+
fail!
|
|
314
|
+
end
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
allow(organizer).to receive(:organized).and_return([interaktor1])
|
|
318
|
+
|
|
319
|
+
expect {
|
|
320
|
+
organizer.call
|
|
321
|
+
}.to raise_error(Interaktor::Error::AttributeValidationError) { |error|
|
|
322
|
+
expect(error.interaktor).to be_an organizer
|
|
323
|
+
expect(error.validation_errors).to eq(reason: ["can't be blank"])
|
|
324
|
+
}
|
|
325
|
+
end
|
|
326
|
+
|
|
327
|
+
it "passes the failure of a nested organizer to the outer organizer" do
|
|
328
|
+
inner_organizer = FakeInteraktor.build_interaktor("InnerOrganizer", type: described_class) do
|
|
329
|
+
failure { attribute :inner_reason }
|
|
330
|
+
|
|
331
|
+
def failure_attributes_from(interaction)
|
|
332
|
+
{inner_reason: :interaktor1_failed}
|
|
333
|
+
end
|
|
334
|
+
end
|
|
335
|
+
outer_organizer = FakeInteraktor.build_interaktor("OuterOrganizer", type: described_class) do
|
|
336
|
+
failure { attribute :outer_reason }
|
|
337
|
+
|
|
338
|
+
def failure_attributes_from(interaction)
|
|
339
|
+
case interaction
|
|
340
|
+
when InnerOrganizer::Interaction
|
|
341
|
+
{outer_reason: :"inner_#{interaction.inner_reason}"}
|
|
342
|
+
end
|
|
343
|
+
end
|
|
344
|
+
end
|
|
345
|
+
interaktor1 = FakeInteraktor.build_interaktor("Interaktor1") do
|
|
346
|
+
def call
|
|
347
|
+
fail!
|
|
348
|
+
end
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
allow(inner_organizer).to receive(:organized).and_return([interaktor1])
|
|
352
|
+
allow(outer_organizer).to receive(:organized).and_return([inner_organizer])
|
|
353
|
+
|
|
354
|
+
result = outer_organizer.call
|
|
355
|
+
|
|
356
|
+
expect(result.outer_reason).to eq :inner_interaktor1_failed
|
|
357
|
+
end
|
|
358
|
+
|
|
359
|
+
it "raises the failure of an interaktor that an organized interaktor calls with .call!" do
|
|
360
|
+
organizer = FakeInteraktor.build_interaktor(type: described_class) do
|
|
361
|
+
failure { attribute :reason }
|
|
362
|
+
|
|
363
|
+
def failure_attributes_from(interaction)
|
|
364
|
+
{reason: :interaktor1_failed}
|
|
365
|
+
end
|
|
366
|
+
end
|
|
367
|
+
inner_interaktor = FakeInteraktor.build_interaktor("InnerInteraktor") do
|
|
368
|
+
define_method(:call) { fail! }
|
|
369
|
+
end
|
|
370
|
+
interaktor1 = FakeInteraktor.build_interaktor("Interaktor1") do
|
|
371
|
+
define_method(:call) { inner_interaktor.call! }
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
allow(organizer).to receive(:organized).and_return([interaktor1])
|
|
375
|
+
|
|
376
|
+
expect {
|
|
377
|
+
organizer.call
|
|
378
|
+
}.to raise_error(Interaktor::Failure) { |error|
|
|
379
|
+
expect(error.interaction).to be_an inner_interaktor::Interaction
|
|
380
|
+
}
|
|
381
|
+
end
|
|
382
|
+
|
|
383
|
+
it "raises the failure of a second instance of an organized interaktor that it calls with .call!" do
|
|
384
|
+
organizer = FakeInteraktor.build_interaktor(type: described_class) do
|
|
385
|
+
input { attribute :depth }
|
|
386
|
+
|
|
387
|
+
failure { attribute :reason }
|
|
388
|
+
|
|
389
|
+
def failure_attributes_from(interaction)
|
|
390
|
+
{reason: :interaktor1_failed}
|
|
391
|
+
end
|
|
392
|
+
end
|
|
393
|
+
interaktor1 = FakeInteraktor.build_interaktor("Interaktor1") do
|
|
394
|
+
input { attribute :depth }
|
|
395
|
+
|
|
396
|
+
def call
|
|
397
|
+
if depth == 0
|
|
398
|
+
self.class.call!(depth: 1)
|
|
399
|
+
else
|
|
400
|
+
fail!
|
|
401
|
+
end
|
|
402
|
+
end
|
|
403
|
+
end
|
|
404
|
+
|
|
405
|
+
allow(organizer).to receive(:organized).and_return([interaktor1])
|
|
406
|
+
|
|
407
|
+
expect {
|
|
408
|
+
organizer.call(depth: 0)
|
|
409
|
+
}.to raise_error(Interaktor::Failure) { |error|
|
|
410
|
+
expect(error.interaction).to be_an interaktor1::Interaction
|
|
411
|
+
}
|
|
412
|
+
end
|
|
413
|
+
end
|
|
414
|
+
|
|
415
|
+
context "when an organized interaktor fails or raises" do
|
|
416
|
+
it "rolls back the finished interaktors in reverse order, but not the failed one" do
|
|
417
|
+
steps = []
|
|
418
|
+
organizer = FakeInteraktor.build_interaktor(type: described_class)
|
|
419
|
+
interaktor1 = FakeInteraktor.build_interaktor("Interaktor1") do
|
|
420
|
+
define_method(:rollback) { steps << :rollback1 }
|
|
421
|
+
end
|
|
422
|
+
interaktor2 = FakeInteraktor.build_interaktor("Interaktor2") do
|
|
423
|
+
define_method(:rollback) { steps << :rollback2 }
|
|
424
|
+
end
|
|
425
|
+
interaktor3 = FakeInteraktor.build_interaktor("Interaktor3") do
|
|
426
|
+
define_method(:call) { fail! }
|
|
427
|
+
define_method(:rollback) { steps << :rollback3 }
|
|
428
|
+
end
|
|
429
|
+
|
|
430
|
+
allow(organizer).to receive(:organized).and_return([interaktor1, interaktor2, interaktor3])
|
|
431
|
+
|
|
432
|
+
organizer.call
|
|
433
|
+
|
|
434
|
+
expect(steps).to eq [:rollback2, :rollback1]
|
|
435
|
+
end
|
|
436
|
+
|
|
437
|
+
it "rolls back the finished interaktors when a later interaktor raises" do
|
|
438
|
+
steps = []
|
|
439
|
+
organizer = FakeInteraktor.build_interaktor(type: described_class)
|
|
440
|
+
interaktor1 = FakeInteraktor.build_interaktor("Interaktor1") do
|
|
441
|
+
define_method(:rollback) { steps << :rollback1 }
|
|
442
|
+
end
|
|
443
|
+
interaktor2 = FakeInteraktor.build_interaktor("Interaktor2") do
|
|
444
|
+
define_method(:call) { raise "boom" }
|
|
445
|
+
end
|
|
446
|
+
|
|
447
|
+
allow(organizer).to receive(:organized).and_return([interaktor1, interaktor2])
|
|
448
|
+
|
|
449
|
+
expect { organizer.call }.to raise_error("boom")
|
|
450
|
+
expect(steps).to eq [:rollback1]
|
|
451
|
+
end
|
|
452
|
+
|
|
453
|
+
it "rolls back the finished interaktors when a later interaktor lets the failure of another interaktor through" do
|
|
454
|
+
steps = []
|
|
455
|
+
organizer = FakeInteraktor.build_interaktor(type: described_class)
|
|
456
|
+
inner_interaktor = FakeInteraktor.build_interaktor("InnerInteraktor") do
|
|
457
|
+
define_method(:call) { fail! }
|
|
458
|
+
end
|
|
459
|
+
interaktor1 = FakeInteraktor.build_interaktor("Interaktor1") do
|
|
460
|
+
define_method(:rollback) { steps << :rollback1 }
|
|
461
|
+
end
|
|
462
|
+
interaktor2 = FakeInteraktor.build_interaktor("Interaktor2") do
|
|
463
|
+
define_method(:call) { inner_interaktor.call! }
|
|
464
|
+
end
|
|
465
|
+
|
|
466
|
+
allow(organizer).to receive(:organized).and_return([interaktor1, interaktor2])
|
|
467
|
+
|
|
468
|
+
expect { organizer.call }.to raise_error(Interaktor::Failure)
|
|
469
|
+
expect(steps).to eq [:rollback1]
|
|
470
|
+
end
|
|
471
|
+
|
|
472
|
+
it "rolls back the interaktors of a finished nested organizer" do
|
|
473
|
+
steps = []
|
|
474
|
+
inner_organizer = FakeInteraktor.build_interaktor("InnerOrganizer", type: described_class)
|
|
475
|
+
outer_organizer = FakeInteraktor.build_interaktor("OuterOrganizer", type: described_class)
|
|
476
|
+
interaktor1 = FakeInteraktor.build_interaktor("Interaktor1") do
|
|
477
|
+
define_method(:rollback) { steps << :rollback1 }
|
|
478
|
+
end
|
|
479
|
+
interaktor2 = FakeInteraktor.build_interaktor("Interaktor2") do
|
|
480
|
+
define_method(:rollback) { steps << :rollback2 }
|
|
481
|
+
end
|
|
482
|
+
interaktor3 = FakeInteraktor.build_interaktor("Interaktor3") do
|
|
483
|
+
define_method(:call) { fail! }
|
|
484
|
+
end
|
|
485
|
+
|
|
486
|
+
allow(inner_organizer).to receive(:organized).and_return([interaktor1, interaktor2])
|
|
487
|
+
allow(outer_organizer).to receive(:organized).and_return([inner_organizer, interaktor3])
|
|
488
|
+
|
|
489
|
+
outer_organizer.call
|
|
490
|
+
|
|
491
|
+
expect(steps).to eq [:rollback2, :rollback1]
|
|
492
|
+
end
|
|
493
|
+
|
|
494
|
+
it "rolls back each finished interaktor once when an interaktor in a nested organizer fails" do
|
|
495
|
+
steps = []
|
|
496
|
+
inner_organizer = FakeInteraktor.build_interaktor("InnerOrganizer", type: described_class)
|
|
497
|
+
outer_organizer = FakeInteraktor.build_interaktor("OuterOrganizer", type: described_class)
|
|
498
|
+
interaktor1 = FakeInteraktor.build_interaktor("Interaktor1") do
|
|
499
|
+
define_method(:rollback) { steps << :rollback1 }
|
|
500
|
+
end
|
|
501
|
+
interaktor2 = FakeInteraktor.build_interaktor("Interaktor2") do
|
|
502
|
+
define_method(:rollback) { steps << :rollback2 }
|
|
503
|
+
end
|
|
504
|
+
interaktor3 = FakeInteraktor.build_interaktor("Interaktor3") do
|
|
505
|
+
define_method(:call) { fail! }
|
|
506
|
+
end
|
|
507
|
+
|
|
508
|
+
allow(inner_organizer).to receive(:organized).and_return([interaktor2, interaktor3])
|
|
509
|
+
allow(outer_organizer).to receive(:organized).and_return([interaktor1, inner_organizer])
|
|
510
|
+
|
|
511
|
+
outer_organizer.call
|
|
512
|
+
|
|
513
|
+
expect(steps).to eq [:rollback2, :rollback1]
|
|
514
|
+
end
|
|
515
|
+
|
|
516
|
+
it "doesn't roll back anything when the organizer succeeds" do
|
|
517
|
+
steps = []
|
|
518
|
+
organizer = FakeInteraktor.build_interaktor(type: described_class)
|
|
519
|
+
interaktor1 = FakeInteraktor.build_interaktor("Interaktor1") do
|
|
520
|
+
define_method(:rollback) { steps << :rollback1 }
|
|
521
|
+
end
|
|
522
|
+
|
|
523
|
+
allow(organizer).to receive(:organized).and_return([interaktor1])
|
|
524
|
+
|
|
525
|
+
organizer.call
|
|
526
|
+
|
|
527
|
+
expect(steps).to eq []
|
|
528
|
+
end
|
|
529
|
+
|
|
530
|
+
it "doesn't run the custom rollback of the organizer that failed" do
|
|
531
|
+
steps = []
|
|
532
|
+
organizer = FakeInteraktor.build_interaktor(type: described_class) do
|
|
533
|
+
define_method(:rollback) do
|
|
534
|
+
super()
|
|
535
|
+
steps << :organizer_rollback
|
|
536
|
+
end
|
|
537
|
+
end
|
|
538
|
+
interaktor1 = FakeInteraktor.build_interaktor("Interaktor1") do
|
|
539
|
+
define_method(:rollback) { steps << :rollback1 }
|
|
540
|
+
end
|
|
541
|
+
interaktor2 = FakeInteraktor.build_interaktor("Interaktor2") do
|
|
542
|
+
define_method(:call) { fail! }
|
|
543
|
+
end
|
|
544
|
+
|
|
545
|
+
allow(organizer).to receive(:organized).and_return([interaktor1, interaktor2])
|
|
546
|
+
|
|
547
|
+
organizer.call
|
|
548
|
+
|
|
549
|
+
expect(steps).to eq [:rollback1]
|
|
550
|
+
end
|
|
551
|
+
|
|
552
|
+
it "runs the custom rollback of a finished nested organizer" do
|
|
553
|
+
steps = []
|
|
554
|
+
inner_organizer = FakeInteraktor.build_interaktor("InnerOrganizer", type: described_class) do
|
|
555
|
+
define_method(:rollback) do
|
|
556
|
+
super()
|
|
557
|
+
steps << :inner_organizer_rollback
|
|
558
|
+
end
|
|
559
|
+
end
|
|
560
|
+
outer_organizer = FakeInteraktor.build_interaktor("OuterOrganizer", type: described_class)
|
|
561
|
+
interaktor1 = FakeInteraktor.build_interaktor("Interaktor1") do
|
|
562
|
+
define_method(:rollback) { steps << :rollback1 }
|
|
563
|
+
end
|
|
564
|
+
interaktor2 = FakeInteraktor.build_interaktor("Interaktor2") do
|
|
565
|
+
define_method(:call) { fail! }
|
|
566
|
+
end
|
|
567
|
+
|
|
568
|
+
allow(inner_organizer).to receive(:organized).and_return([interaktor1])
|
|
569
|
+
allow(outer_organizer).to receive(:organized).and_return([inner_organizer, interaktor2])
|
|
570
|
+
|
|
571
|
+
outer_organizer.call
|
|
572
|
+
|
|
573
|
+
expect(steps).to eq [:rollback1, :inner_organizer_rollback]
|
|
574
|
+
end
|
|
575
|
+
end
|
|
576
|
+
|
|
577
|
+
it "raises an exception if the organizer declares failure attributes but doesn't build them" do
|
|
578
|
+
organizer = FakeInteraktor.build_interaktor(type: described_class) do
|
|
579
|
+
failure { attribute :reason }
|
|
580
|
+
end
|
|
581
|
+
interaktor1 = FakeInteraktor.build_interaktor("Interaktor1")
|
|
582
|
+
|
|
583
|
+
allow(organizer).to receive(:organized).and_return([interaktor1])
|
|
584
|
+
|
|
585
|
+
expect {
|
|
586
|
+
organizer.call
|
|
587
|
+
}.to raise_error(Interaktor::Error::InvalidOrganizerFailureDefinitionError) { |error|
|
|
588
|
+
expect(error.interaktor).to be organizer
|
|
589
|
+
}
|
|
590
|
+
end
|
|
591
|
+
|
|
592
|
+
it "raises an exception if the organizer builds failure attributes but doesn't declare them" do
|
|
593
|
+
organizer = FakeInteraktor.build_interaktor(type: described_class) do
|
|
594
|
+
def failure_attributes_from(interaction)
|
|
595
|
+
{}
|
|
596
|
+
end
|
|
597
|
+
end
|
|
598
|
+
interaktor1 = FakeInteraktor.build_interaktor("Interaktor1")
|
|
599
|
+
|
|
600
|
+
allow(organizer).to receive(:organized).and_return([interaktor1])
|
|
601
|
+
|
|
602
|
+
expect {
|
|
603
|
+
organizer.call
|
|
604
|
+
}.to raise_error(Interaktor::Error::InvalidOrganizerFailureDefinitionError) { |error|
|
|
605
|
+
expect(error.interaktor).to be organizer
|
|
606
|
+
}
|
|
607
|
+
end
|
|
608
|
+
|
|
205
609
|
it "raises an exception if the organizer's last interaktor does not include the organizer's success attributes" do
|
|
206
610
|
organizer = FakeInteraktor.build_interaktor(type: described_class) do
|
|
207
611
|
success do
|
data/spec/spec_helper.rb
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
Warning[:deprecated] = true
|
|
2
|
+
|
|
1
3
|
RSpec.configure do |config|
|
|
2
4
|
config.expect_with :rspec do |expectations|
|
|
3
5
|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
|
@@ -22,9 +24,9 @@ require "simplecov"
|
|
|
22
24
|
SimpleCov.start do
|
|
23
25
|
enable_coverage :branch
|
|
24
26
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
skip "/spec/"
|
|
28
|
+
skip "/vendor/"
|
|
29
|
+
skip "/lib/interaktor/error"
|
|
28
30
|
end
|
|
29
31
|
|
|
30
32
|
Bundler.require(:default, :test)
|
data/spec/support/lint.rb
CHANGED
|
@@ -17,6 +17,21 @@ RSpec.shared_examples "lint" do |interaktor_class|
|
|
|
17
17
|
}.to raise_error(Interaktor::Error::UnknownAttributeError)
|
|
18
18
|
end
|
|
19
19
|
|
|
20
|
+
it "raises the failure of an interaktor that it calls with .call!" do
|
|
21
|
+
inner_interaktor = FakeInteraktor.build_interaktor("InnerInteraktor") do
|
|
22
|
+
define_method(:call) { fail! }
|
|
23
|
+
end
|
|
24
|
+
interaktor = FakeInteraktor.build_interaktor(type: interaktor_class) do
|
|
25
|
+
define_method(:call) { inner_interaktor.call! }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
expect {
|
|
29
|
+
interaktor.call
|
|
30
|
+
}.to raise_error(Interaktor::Failure) { |error|
|
|
31
|
+
expect(error.interaction).to be_an inner_interaktor::Interaction
|
|
32
|
+
}
|
|
33
|
+
end
|
|
34
|
+
|
|
20
35
|
it "fails when a non-hash or non-context argument is passed" do
|
|
21
36
|
interaktor = FakeInteraktor.build_interaktor(type: interaktor_class)
|
|
22
37
|
|
|
@@ -63,9 +78,9 @@ RSpec.shared_examples "lint" do |interaktor_class|
|
|
|
63
78
|
end
|
|
64
79
|
|
|
65
80
|
it "rescues failure" do
|
|
66
|
-
instance = FakeInteraktor.build_interaktor(type: interaktor_class)
|
|
67
|
-
|
|
68
|
-
|
|
81
|
+
instance = FakeInteraktor.build_interaktor(type: interaktor_class) do
|
|
82
|
+
define_method(:call) { fail! }
|
|
83
|
+
end.new
|
|
69
84
|
|
|
70
85
|
expect {
|
|
71
86
|
instance.run
|
|
@@ -214,6 +229,23 @@ RSpec.shared_examples "lint" do |interaktor_class|
|
|
|
214
229
|
expect { result.bar }.to raise_error(NoMethodError)
|
|
215
230
|
end
|
|
216
231
|
|
|
232
|
+
it "lets the interaktor read its attributes when it is rolled back" do
|
|
233
|
+
foo_in_rollback = nil
|
|
234
|
+
interaktor = FakeInteraktor.build_interaktor(type: interaktor_class) do
|
|
235
|
+
input { attribute :foo }
|
|
236
|
+
|
|
237
|
+
after { raise "after hook failed" }
|
|
238
|
+
|
|
239
|
+
define_method(:rollback) { foo_in_rollback = foo }
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
expect {
|
|
243
|
+
interaktor.call(foo: "bar")
|
|
244
|
+
}.to raise_error("after hook failed")
|
|
245
|
+
|
|
246
|
+
expect(foo_in_rollback).to eq "bar"
|
|
247
|
+
end
|
|
248
|
+
|
|
217
249
|
it "disallows specifically disallowed attribute names" do
|
|
218
250
|
expect {
|
|
219
251
|
FakeInteraktor.build_interaktor(type: interaktor_class) do
|
|
@@ -223,6 +255,14 @@ RSpec.shared_examples "lint" do |interaktor_class|
|
|
|
223
255
|
}.to raise_error(ArgumentError, /disallowed attribute name/i)
|
|
224
256
|
end
|
|
225
257
|
|
|
258
|
+
it "disallows the names of methods of the interaktor" do
|
|
259
|
+
expect {
|
|
260
|
+
FakeInteraktor.build_interaktor(type: interaktor_class) do
|
|
261
|
+
input { attribute :run }
|
|
262
|
+
end
|
|
263
|
+
}.to raise_error(ArgumentError, /disallowed attribute name\(s\): run/i)
|
|
264
|
+
end
|
|
265
|
+
|
|
226
266
|
it "raises an exception when input block is provided more than once" do
|
|
227
267
|
expect {
|
|
228
268
|
FakeInteraktor.build_interaktor(type: interaktor_class) do
|
|
@@ -249,6 +289,87 @@ RSpec.shared_examples "lint" do |interaktor_class|
|
|
|
249
289
|
expect(result.foo).to eq "bar"
|
|
250
290
|
end
|
|
251
291
|
|
|
292
|
+
it "lets the interaktor read its attributes when it is rolled back" do
|
|
293
|
+
foo_in_rollback = nil
|
|
294
|
+
interaktor = FakeInteraktor.build_interaktor(type: interaktor_class) do
|
|
295
|
+
success { attribute :foo }
|
|
296
|
+
|
|
297
|
+
after { raise "after hook failed" }
|
|
298
|
+
|
|
299
|
+
def call
|
|
300
|
+
success!(foo: "bar")
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
define_method(:rollback) { foo_in_rollback = foo }
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
expect {
|
|
307
|
+
interaktor.call
|
|
308
|
+
}.to raise_error("after hook failed")
|
|
309
|
+
|
|
310
|
+
expect(foo_in_rollback).to eq "bar"
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
it "lets the interaktor read an input attribute of the same name until it calls #success!" do
|
|
314
|
+
foo_in_call = nil
|
|
315
|
+
foo_in_after_hook = nil
|
|
316
|
+
interaktor = FakeInteraktor.build_interaktor(type: interaktor_class) do
|
|
317
|
+
success { attribute :foo }
|
|
318
|
+
input { attribute :foo }
|
|
319
|
+
|
|
320
|
+
after { foo_in_after_hook = foo }
|
|
321
|
+
|
|
322
|
+
define_method(:call) do
|
|
323
|
+
foo_in_call = foo
|
|
324
|
+
success!(foo: "new")
|
|
325
|
+
end
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
interaktor.call(foo: "old")
|
|
329
|
+
|
|
330
|
+
expect(foo_in_call).to eq "old"
|
|
331
|
+
expect(foo_in_after_hook).to eq "new"
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
it "doesn't hide a method of the same name from a module that the interaktor includes" do
|
|
335
|
+
helpers = Module.new do
|
|
336
|
+
def foo
|
|
337
|
+
"from the module"
|
|
338
|
+
end
|
|
339
|
+
end
|
|
340
|
+
interaktor = FakeInteraktor.build_interaktor(type: interaktor_class) do
|
|
341
|
+
include helpers
|
|
342
|
+
|
|
343
|
+
success { attribute :foo }
|
|
344
|
+
|
|
345
|
+
def call
|
|
346
|
+
success!(foo: foo)
|
|
347
|
+
end
|
|
348
|
+
end
|
|
349
|
+
|
|
350
|
+
result = interaktor.call
|
|
351
|
+
|
|
352
|
+
expect(result.foo).to eq "from the module"
|
|
353
|
+
end
|
|
354
|
+
|
|
355
|
+
it "doesn't replace a method of the same name defined before the success block" do
|
|
356
|
+
interaktor = FakeInteraktor.build_interaktor(type: interaktor_class) do
|
|
357
|
+
def foo
|
|
358
|
+
"from the method"
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
success { attribute :foo }
|
|
362
|
+
|
|
363
|
+
def call
|
|
364
|
+
success!(foo: foo)
|
|
365
|
+
end
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
result = interaktor.call
|
|
369
|
+
|
|
370
|
+
expect(result.foo).to eq "from the method"
|
|
371
|
+
end
|
|
372
|
+
|
|
252
373
|
it "raises an exception when the attribute is required and not provided" do
|
|
253
374
|
interaktor = FakeInteraktor.build_interaktor(type: interaktor_class) do
|
|
254
375
|
success do
|
|
@@ -384,6 +505,14 @@ RSpec.shared_examples "lint" do |interaktor_class|
|
|
|
384
505
|
}.to raise_error(ArgumentError, /disallowed attribute name/i)
|
|
385
506
|
end
|
|
386
507
|
|
|
508
|
+
it "disallows the names of methods of the interaktor" do
|
|
509
|
+
expect {
|
|
510
|
+
FakeInteraktor.build_interaktor(type: interaktor_class) do
|
|
511
|
+
success { attribute :run }
|
|
512
|
+
end
|
|
513
|
+
}.to raise_error(ArgumentError, /disallowed attribute name\(s\): run/i)
|
|
514
|
+
end
|
|
515
|
+
|
|
387
516
|
it "raises an exception when success block is provided more than once" do
|
|
388
517
|
expect {
|
|
389
518
|
FakeInteraktor.build_interaktor(type: interaktor_class) do
|
|
@@ -541,4 +670,44 @@ RSpec.shared_examples "lint" do |interaktor_class|
|
|
|
541
670
|
}.to raise_error "Failure block already defined"
|
|
542
671
|
end
|
|
543
672
|
end
|
|
673
|
+
|
|
674
|
+
describe "hooks" do
|
|
675
|
+
it "raises an exception when a before hook calls #success!" do
|
|
676
|
+
interaktor = FakeInteraktor.build_interaktor(type: interaktor_class) do
|
|
677
|
+
before { success! }
|
|
678
|
+
end
|
|
679
|
+
|
|
680
|
+
expect {
|
|
681
|
+
interaktor.call
|
|
682
|
+
}.to raise_error(Interaktor::Error::SuccessInHookError) { |error|
|
|
683
|
+
expect(error.interaktor).to be interaktor
|
|
684
|
+
}
|
|
685
|
+
end
|
|
686
|
+
|
|
687
|
+
it "raises an exception when an around hook calls #success!" do
|
|
688
|
+
interaktor = FakeInteraktor.build_interaktor(type: interaktor_class) do
|
|
689
|
+
around { |_run_interaktor| success! }
|
|
690
|
+
end
|
|
691
|
+
|
|
692
|
+
expect {
|
|
693
|
+
interaktor.call
|
|
694
|
+
}.to raise_error(Interaktor::Error::SuccessInHookError)
|
|
695
|
+
end
|
|
696
|
+
|
|
697
|
+
it "lets an around hook run an interaktor that calls #success!" do
|
|
698
|
+
interaktor = FakeInteraktor.build_interaktor(type: interaktor_class) do
|
|
699
|
+
success { attribute :foo }
|
|
700
|
+
|
|
701
|
+
around { |run_interaktor| run_interaktor.call }
|
|
702
|
+
|
|
703
|
+
def call
|
|
704
|
+
success!(foo: "bar")
|
|
705
|
+
end
|
|
706
|
+
end
|
|
707
|
+
|
|
708
|
+
result = interaktor.call
|
|
709
|
+
|
|
710
|
+
expect(result.foo).to eq "bar"
|
|
711
|
+
end
|
|
712
|
+
end
|
|
544
713
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: interaktor
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.7.0.pre
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Taylor Thurlow
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: activemodel
|
|
@@ -81,15 +80,18 @@ files:
|
|
|
81
80
|
- lib/interaktor/error/attribute_validation_error.rb
|
|
82
81
|
- lib/interaktor/error/base.rb
|
|
83
82
|
- lib/interaktor/error/invalid_method_for_state_error.rb
|
|
83
|
+
- lib/interaktor/error/invalid_organizer_failure_definition_error.rb
|
|
84
84
|
- lib/interaktor/error/missing_explicit_success_error.rb
|
|
85
85
|
- lib/interaktor/error/organizer_missing_passed_attribute_error.rb
|
|
86
86
|
- lib/interaktor/error/organizer_success_attribute_missing_error.rb
|
|
87
|
+
- lib/interaktor/error/success_in_hook_error.rb
|
|
87
88
|
- lib/interaktor/error/unknown_attribute_error.rb
|
|
88
89
|
- lib/interaktor/failure.rb
|
|
89
90
|
- lib/interaktor/hooks.rb
|
|
90
91
|
- lib/interaktor/interaction.rb
|
|
91
92
|
- lib/interaktor/organizer.rb
|
|
92
93
|
- spec/integration_spec.rb
|
|
94
|
+
- spec/interaktor/failure_spec.rb
|
|
93
95
|
- spec/interaktor/hooks_spec.rb
|
|
94
96
|
- spec/interaktor/organizer_spec.rb
|
|
95
97
|
- spec/interaktor_spec.rb
|
|
@@ -100,7 +102,6 @@ homepage: https://github.com/taylorthurlow/interaktor
|
|
|
100
102
|
licenses:
|
|
101
103
|
- MIT
|
|
102
104
|
metadata: {}
|
|
103
|
-
post_install_message:
|
|
104
105
|
rdoc_options: []
|
|
105
106
|
require_paths:
|
|
106
107
|
- lib
|
|
@@ -111,12 +112,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
111
112
|
version: '3.0'
|
|
112
113
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
113
114
|
requirements:
|
|
114
|
-
- - "
|
|
115
|
+
- - ">="
|
|
115
116
|
- !ruby/object:Gem::Version
|
|
116
|
-
version:
|
|
117
|
+
version: '0'
|
|
117
118
|
requirements: []
|
|
118
|
-
rubygems_version:
|
|
119
|
-
signing_key:
|
|
119
|
+
rubygems_version: 4.0.20
|
|
120
120
|
specification_version: 4
|
|
121
121
|
summary: Simple service object implementation
|
|
122
122
|
test_files: []
|