interaktor 0.6.0 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fe8bfb895d5be7950b49cde31d318c904d33ac57e4db772d79834061d5f561fd
4
- data.tar.gz: ef77d7af2851d7c3de8db7632b1e2fbc38528981f42e7da500c7f1b21c3836ed
3
+ metadata.gz: d6f6275ae6d81c54b883e5d5f97662cb105cd882243f65a0bcdcdea98e62d141
4
+ data.tar.gz: d13a41c22442f2a3332b0b7ef765749a5350f8a656c4330d4770aae72745716c
5
5
  SHA512:
6
- metadata.gz: 6fe6cee6bdd970cd8572c7b7874a4eb60b17d21911d23a1b9d529dee3398e690d3ac0d3290c85ce9e78b080af3e58f82a6c051bbdf432c54c0a2ecab16b24cfb
7
- data.tar.gz: f1fe97ca9a55ee29c6fec80b5681acf722b7baea437a28a69f7a73aaf9583373aad9ec43086ad46dbb3a894782526d6b81f925d12a91ce5436f3f45b32c02f23
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@v3
16
+ - uses: actions/checkout@v7
17
17
  - name: Set up Ruby
18
18
  uses: ruby/setup-ruby@v1
19
19
  with:
20
- ruby-version: 3.2
20
+ ruby-version: "4.0"
21
21
 
22
22
  - name: Publish to RubyGems
23
23
  run: |
@@ -8,13 +8,13 @@ jobs:
8
8
  fail-fast: false
9
9
  matrix:
10
10
  os: [ubuntu-latest, macos-latest]
11
- ruby: [3.0, 3.1, 3.2, 3.3, 3.4, head, debug]
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@v2
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
- 3.4.9
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
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "interaktor"
3
- spec.version = "0.6.0"
3
+ spec.version = "0.7.0.pre"
4
4
 
5
5
  spec.author = "Taylor Thurlow"
6
6
  spec.email = "thurlow@hey.com"
@@ -22,9 +22,7 @@ module Interaktor::Callable
22
22
 
23
23
  klass.check_for_disallowed_attribute_names!
24
24
 
25
- klass.attribute_names.each do |name|
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
@@ -0,0 +1,8 @@
1
+ class Interaktor::Error::SuccessInHookError < Interaktor::Error::Base
2
+ def message
3
+ <<~MSG.gsub(/\s+/, " ").strip
4
+ #{interaktor} interaktor called `success!` in a hook. Only the `call`
5
+ method of an interaktor can call `success!`.
6
+ MSG
7
+ end
8
+ end
@@ -325,6 +325,16 @@ module Interaktor::Hooks
325
325
  #
326
326
  # Returns nothing.
327
327
  def run_hook(hook, *args)
328
- hook.is_a?(Symbol) ? send(hook, *args) : instance_exec(*args, &hook)
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
@@ -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
- catch(:early_return) do
46
- latest_interaction = interaktor.call!(latest_interaction || @interaction)
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
@@ -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!).once.ordered.and_call_original
77
- expect(interaktor2).to receive(:call!).once.ordered.and_call_original
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!).once.ordered.and_call_original
108
- expect(interaktor2).to receive(:call!).once.ordered.and_call_original
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!).once.ordered.and_call_original
128
- expect(interaktor2).to receive(:call!).once.ordered.and_call_original
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!).once.ordered.and_call_original
157
- expect(interaktor2).to receive(:call!).once.ordered.and_call_original
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
- add_filter "/spec/"
26
- add_filter "/vendor/"
27
- add_filter "/lib/interaktor/error"
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).new
67
-
68
- expect(instance).to receive(:run!).and_raise(Interaktor::Failure)
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.6.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: 2026-06-29 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: activemodel
@@ -81,9 +80,11 @@ 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
@@ -101,7 +102,6 @@ homepage: https://github.com/taylorthurlow/interaktor
101
102
  licenses:
102
103
  - MIT
103
104
  metadata: {}
104
- post_install_message:
105
105
  rdoc_options: []
106
106
  require_paths:
107
107
  - lib
@@ -116,8 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
116
  - !ruby/object:Gem::Version
117
117
  version: '0'
118
118
  requirements: []
119
- rubygems_version: 3.4.19
120
- signing_key:
119
+ rubygems_version: 4.0.20
121
120
  specification_version: 4
122
121
  summary: Simple service object implementation
123
122
  test_files: []