interaktor 0.5.0 → 0.6.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.
@@ -1,460 +1,454 @@
1
- # rubocop:disable RSpec/ScatteredSetup
1
+ RSpec.describe Interaktor::Hooks do
2
+ describe "#with_hooks" do
3
+ def build_hooked(&block)
4
+ hooked = Class.new.send(:include, Interaktor::Hooks)
2
5
 
3
- module Interaktor
4
- RSpec.describe Hooks do
5
- describe "#with_hooks" do
6
- def build_hooked(&block)
7
- hooked = Class.new.send(:include, Interaktor::Hooks)
6
+ hooked.class_eval do
7
+ attr_reader :steps
8
8
 
9
- hooked.class_eval do
10
- attr_reader :steps
11
-
12
- def self.process
13
- new.tap(&:process).steps
14
- end
15
-
16
- def initialize
17
- @steps = []
18
- end
9
+ def self.process
10
+ new.tap(&:process).steps
11
+ end
19
12
 
20
- def process
21
- with_hooks { steps << :process }
22
- end
13
+ def initialize
14
+ @steps = []
23
15
  end
24
16
 
25
- hooked.class_eval(&block) if block
26
- hooked
17
+ def process
18
+ with_hooks { steps << :process }
19
+ end
27
20
  end
28
21
 
29
- context "with an around hook method" do
30
- let(:hooked) {
31
- build_hooked do
32
- around :add_around_before_and_around_after
22
+ hooked.class_eval(&block) if block
23
+ hooked
24
+ end
33
25
 
34
- private
26
+ context "with an around hook method" do
27
+ let(:hooked) {
28
+ build_hooked do
29
+ around :add_around_before_and_around_after
35
30
 
36
- def add_around_before_and_around_after(hooked)
37
- steps << :around_before
38
- hooked.call
39
- steps << :around_after
40
- end
41
- end
42
- }
31
+ private
43
32
 
44
- it "runs the around hook method" do
45
- expect(hooked.process).to eq([
46
- :around_before,
47
- :process,
48
- :around_after
49
- ])
33
+ def add_around_before_and_around_after(hooked)
34
+ steps << :around_before
35
+ hooked.call
36
+ steps << :around_after
37
+ end
50
38
  end
39
+ }
40
+
41
+ it "runs the around hook method" do
42
+ expect(hooked.process).to eq([
43
+ :around_before,
44
+ :process,
45
+ :around_after
46
+ ])
51
47
  end
48
+ end
52
49
 
53
- context "with an around hook block" do
54
- let(:hooked) {
55
- build_hooked do
56
- around do |hooked|
57
- steps << :around_before
58
- hooked.call
59
- steps << :around_after
60
- end
61
- end
62
- }
63
-
64
- it "runs the around hook block" do
65
- expect(hooked.process).to eq([
66
- :around_before,
67
- :process,
68
- :around_after
69
- ])
50
+ context "with an around hook block" do
51
+ let(:hooked) {
52
+ build_hooked do
53
+ around do |hooked|
54
+ steps << :around_before
55
+ hooked.call
56
+ steps << :around_after
57
+ end
70
58
  end
59
+ }
60
+
61
+ it "runs the around hook block" do
62
+ expect(hooked.process).to eq([
63
+ :around_before,
64
+ :process,
65
+ :around_after
66
+ ])
71
67
  end
68
+ end
69
+
70
+ context "with an around hook method and block in one call" do
71
+ let(:hooked) {
72
+ build_hooked do
73
+ around :add_around_before1_and_around_after1 do |hooked|
74
+ steps << :around_before2
75
+ hooked.call
76
+ steps << :around_after2
77
+ end
72
78
 
73
- context "with an around hook method and block in one call" do
74
- let(:hooked) {
75
- build_hooked do
76
- around :add_around_before1_and_around_after1 do |hooked|
77
- steps << :around_before2
78
- hooked.call
79
- steps << :around_after2
80
- end
81
-
82
- private
83
-
84
- def add_around_before1_and_around_after1(hooked)
85
- steps << :around_before1
86
- hooked.call
87
- steps << :around_after1
88
- end
89
- end
90
- }
91
-
92
- it "runs the around hook method and block in order" do
93
- expect(hooked.process).to eq([
94
- :around_before1,
95
- :around_before2,
96
- :process,
97
- :around_after2,
98
- :around_after1
99
- ])
79
+ private
80
+
81
+ def add_around_before1_and_around_after1(hooked)
82
+ steps << :around_before1
83
+ hooked.call
84
+ steps << :around_after1
85
+ end
100
86
  end
87
+ }
88
+
89
+ it "runs the around hook method and block in order" do
90
+ expect(hooked.process).to eq([
91
+ :around_before1,
92
+ :around_before2,
93
+ :process,
94
+ :around_after2,
95
+ :around_after1
96
+ ])
101
97
  end
98
+ end
102
99
 
103
- context "with an around hook method and block in multiple calls" do
104
- let(:hooked) {
105
- build_hooked do
106
- around do |hooked|
107
- steps << :around_before1
108
- hooked.call
109
- steps << :around_after1
110
- end
111
-
112
- around :add_around_before2_and_around_after2
113
-
114
- private
115
-
116
- def add_around_before2_and_around_after2(hooked)
117
- steps << :around_before2
118
- hooked.call
119
- steps << :around_after2
120
- end
121
- end
122
- }
123
-
124
- it "runs the around hook block and method in order" do
125
- expect(hooked.process).to eq([
126
- :around_before1,
127
- :around_before2,
128
- :process,
129
- :around_after2,
130
- :around_after1
131
- ])
100
+ context "with an around hook method and block in multiple calls" do
101
+ let(:hooked) {
102
+ build_hooked do
103
+ around do |hooked|
104
+ steps << :around_before1
105
+ hooked.call
106
+ steps << :around_after1
107
+ end
108
+
109
+ around :add_around_before2_and_around_after2
110
+
111
+ private
112
+
113
+ def add_around_before2_and_around_after2(hooked)
114
+ steps << :around_before2
115
+ hooked.call
116
+ steps << :around_after2
117
+ end
132
118
  end
119
+ }
120
+
121
+ it "runs the around hook block and method in order" do
122
+ expect(hooked.process).to eq([
123
+ :around_before1,
124
+ :around_before2,
125
+ :process,
126
+ :around_after2,
127
+ :around_after1
128
+ ])
133
129
  end
130
+ end
134
131
 
135
- context "with a before hook method" do
136
- let(:hooked) {
137
- build_hooked do
138
- before :add_before
132
+ context "with a before hook method" do
133
+ let(:hooked) {
134
+ build_hooked do
135
+ before :add_before
139
136
 
140
- private
137
+ private
141
138
 
142
- def add_before
143
- steps << :before
144
- end
139
+ def add_before
140
+ steps << :before
145
141
  end
146
- }
147
-
148
- it "runs the before hook method" do
149
- expect(hooked.process).to eq([
150
- :before,
151
- :process
152
- ])
153
142
  end
143
+ }
144
+
145
+ it "runs the before hook method" do
146
+ expect(hooked.process).to eq([
147
+ :before,
148
+ :process
149
+ ])
154
150
  end
151
+ end
155
152
 
156
- context "with a before hook block" do
157
- let(:hooked) {
158
- build_hooked do
159
- before do
160
- steps << :before
161
- end
153
+ context "with a before hook block" do
154
+ let(:hooked) {
155
+ build_hooked do
156
+ before do
157
+ steps << :before
162
158
  end
163
- }
164
-
165
- it "runs the before hook block" do
166
- expect(hooked.process).to eq([
167
- :before,
168
- :process
169
- ])
170
159
  end
160
+ }
161
+
162
+ it "runs the before hook block" do
163
+ expect(hooked.process).to eq([
164
+ :before,
165
+ :process
166
+ ])
171
167
  end
168
+ end
172
169
 
173
- context "with a before hook method and block in one call" do
174
- let(:hooked) {
175
- build_hooked do
176
- before :add_before1 do
177
- steps << :before2
178
- end
170
+ context "with a before hook method and block in one call" do
171
+ let(:hooked) {
172
+ build_hooked do
173
+ before :add_before1 do
174
+ steps << :before2
175
+ end
179
176
 
180
- private
177
+ private
181
178
 
182
- def add_before1
183
- steps << :before1
184
- end
179
+ def add_before1
180
+ steps << :before1
185
181
  end
186
- }
187
-
188
- it "runs the before hook method and block in order" do
189
- expect(hooked.process).to eq([
190
- :before1,
191
- :before2,
192
- :process
193
- ])
194
182
  end
183
+ }
184
+
185
+ it "runs the before hook method and block in order" do
186
+ expect(hooked.process).to eq([
187
+ :before1,
188
+ :before2,
189
+ :process
190
+ ])
195
191
  end
192
+ end
196
193
 
197
- context "with a before hook method and block in multiple calls" do
198
- let(:hooked) {
199
- build_hooked do
200
- before do
201
- steps << :before1
202
- end
194
+ context "with a before hook method and block in multiple calls" do
195
+ let(:hooked) {
196
+ build_hooked do
197
+ before do
198
+ steps << :before1
199
+ end
203
200
 
204
- before :add_before2
201
+ before :add_before2
205
202
 
206
- private
203
+ private
207
204
 
208
- def add_before2
209
- steps << :before2
210
- end
205
+ def add_before2
206
+ steps << :before2
211
207
  end
212
- }
213
-
214
- it "runs the before hook block and method in order" do
215
- expect(hooked.process).to eq([
216
- :before1,
217
- :before2,
218
- :process
219
- ])
220
208
  end
209
+ }
210
+
211
+ it "runs the before hook block and method in order" do
212
+ expect(hooked.process).to eq([
213
+ :before1,
214
+ :before2,
215
+ :process
216
+ ])
221
217
  end
218
+ end
222
219
 
223
- context "with an after hook method" do
224
- let(:hooked) {
225
- build_hooked do
226
- after :add_after
220
+ context "with an after hook method" do
221
+ let(:hooked) {
222
+ build_hooked do
223
+ after :add_after
227
224
 
228
- private
225
+ private
229
226
 
230
- def add_after
231
- steps << :after
232
- end
227
+ def add_after
228
+ steps << :after
233
229
  end
234
- }
235
-
236
- it "runs the after hook method" do
237
- expect(hooked.process).to eq([
238
- :process,
239
- :after
240
- ])
241
230
  end
231
+ }
232
+
233
+ it "runs the after hook method" do
234
+ expect(hooked.process).to eq([
235
+ :process,
236
+ :after
237
+ ])
242
238
  end
239
+ end
243
240
 
244
- context "with an after hook block" do
245
- let(:hooked) {
246
- build_hooked do
247
- after do
248
- steps << :after
249
- end
241
+ context "with an after hook block" do
242
+ let(:hooked) {
243
+ build_hooked do
244
+ after do
245
+ steps << :after
250
246
  end
251
- }
252
-
253
- it "runs the after hook block" do
254
- expect(hooked.process).to eq([
255
- :process,
256
- :after
257
- ])
258
247
  end
248
+ }
249
+
250
+ it "runs the after hook block" do
251
+ expect(hooked.process).to eq([
252
+ :process,
253
+ :after
254
+ ])
259
255
  end
256
+ end
260
257
 
261
- context "with an after hook method and block in one call" do
262
- let(:hooked) {
263
- build_hooked do
264
- after :add_after1 do
265
- steps << :after2
266
- end
258
+ context "with an after hook method and block in one call" do
259
+ let(:hooked) {
260
+ build_hooked do
261
+ after :add_after1 do
262
+ steps << :after2
263
+ end
267
264
 
268
- private
265
+ private
269
266
 
270
- def add_after1
271
- steps << :after1
272
- end
267
+ def add_after1
268
+ steps << :after1
273
269
  end
274
- }
275
-
276
- it "runs the after hook method and block in order" do
277
- expect(hooked.process).to eq([
278
- :process,
279
- :after2,
280
- :after1
281
- ])
282
270
  end
271
+ }
272
+
273
+ it "runs the after hook method and block in order" do
274
+ expect(hooked.process).to eq([
275
+ :process,
276
+ :after2,
277
+ :after1
278
+ ])
283
279
  end
280
+ end
284
281
 
285
- context "with an after hook method and block in multiple calls" do
286
- let(:hooked) {
287
- build_hooked do
288
- after do
289
- steps << :after1
290
- end
282
+ context "with an after hook method and block in multiple calls" do
283
+ let(:hooked) {
284
+ build_hooked do
285
+ after do
286
+ steps << :after1
287
+ end
291
288
 
292
- after :add_after2
289
+ after :add_after2
293
290
 
294
- private
291
+ private
295
292
 
296
- def add_after2
297
- steps << :after2
298
- end
293
+ def add_after2
294
+ steps << :after2
299
295
  end
300
- }
301
-
302
- it "runs the after hook block and method in order" do
303
- expect(hooked.process).to eq([
304
- :process,
305
- :after2,
306
- :after1
307
- ])
308
296
  end
297
+ }
298
+
299
+ it "runs the after hook block and method in order" do
300
+ expect(hooked.process).to eq([
301
+ :process,
302
+ :after2,
303
+ :after1
304
+ ])
309
305
  end
306
+ end
310
307
 
311
- context "with an ensure_hook method" do
312
- let(:hooked) {
313
- build_hooked do
314
- ensure_hook :add_ensure
308
+ context "with an ensure_hook method" do
309
+ let(:hooked) {
310
+ build_hooked do
311
+ ensure_hook :add_ensure
315
312
 
316
- private
313
+ private
317
314
 
318
- def add_ensure
319
- steps << :ensure
320
- end
315
+ def add_ensure
316
+ steps << :ensure
321
317
  end
322
- }
318
+ end
319
+ }
320
+
321
+ it "runs the after hook method" do
322
+ expect(hooked.process).to eq([
323
+ :process,
324
+ :ensure
325
+ ])
326
+ end
327
+ end
323
328
 
324
- it "runs the after hook method" do
325
- expect(hooked.process).to eq([
326
- :process,
327
- :ensure
328
- ])
329
+ context "with an ensure_hook block" do
330
+ let(:hooked) {
331
+ build_hooked do
332
+ ensure_hook do
333
+ steps << :ensure
334
+ end
329
335
  end
336
+ }
337
+
338
+ it "runs the after hook block" do
339
+ expect(hooked.process).to eq([
340
+ :process,
341
+ :ensure
342
+ ])
330
343
  end
344
+ end
331
345
 
332
- context "with an ensure_hook block" do
333
- let(:hooked) {
334
- build_hooked do
335
- ensure_hook do
336
- steps << :ensure
337
- end
346
+ context "with an ensure_hook method and block in one call" do
347
+ let(:hooked) {
348
+ build_hooked do
349
+ ensure_hook :add_ensure1 do
350
+ steps << :ensure2
338
351
  end
339
- }
340
352
 
341
- it "runs the after hook block" do
342
- expect(hooked.process).to eq([
343
- :process,
344
- :ensure
345
- ])
353
+ private
354
+
355
+ def add_ensure1
356
+ steps << :ensure1
357
+ end
346
358
  end
359
+ }
360
+
361
+ it "runs the ensure_hook method and block in order" do
362
+ expect(hooked.process).to eq([
363
+ :process,
364
+ :ensure1,
365
+ :ensure2
366
+ ])
347
367
  end
368
+ end
348
369
 
349
- context "with an ensure_hook method and block in one call" do
350
- let(:hooked) {
351
- build_hooked do
352
- ensure_hook :add_ensure1 do
353
- steps << :ensure2
354
- end
370
+ context "with an ensure_hook method and block in multiple calls" do
371
+ let(:hooked) {
372
+ build_hooked do
373
+ ensure_hook do
374
+ steps << :ensure1
375
+ end
355
376
 
356
- private
377
+ ensure_hook :add_ensure2
357
378
 
358
- def add_ensure1
359
- steps << :ensure1
360
- end
361
- end
362
- }
379
+ private
363
380
 
364
- it "runs the ensure_hook method and block in order" do
365
- expect(hooked.process).to eq([
366
- :process,
367
- :ensure1,
368
- :ensure2
369
- ])
381
+ def add_ensure2
382
+ steps << :ensure2
383
+ end
370
384
  end
385
+ }
386
+
387
+ it "runs the ensure_hook block and method in order" do
388
+ expect(hooked.process).to eq([
389
+ :process,
390
+ :ensure1,
391
+ :ensure2
392
+ ])
371
393
  end
394
+ end
395
+
396
+ context "with around, before, after and ensure_hook hooks" do
397
+ let(:hooked) {
398
+ build_hooked do
399
+ around do |hooked|
400
+ steps << :around_before1
401
+ hooked.call
402
+ steps << :around_after1
403
+ end
404
+
405
+ around do |hooked|
406
+ steps << :around_before2
407
+ hooked.call
408
+ steps << :around_after2
409
+ end
372
410
 
373
- context "with an ensure_hook method and block in multiple calls" do
374
- let(:hooked) {
375
- build_hooked do
376
- ensure_hook do
377
- steps << :ensure1
378
- end
411
+ before do
412
+ steps << :before1
413
+ end
379
414
 
380
- ensure_hook :add_ensure2
415
+ before do
416
+ steps << :before2
417
+ end
381
418
 
382
- private
419
+ after do
420
+ steps << :after1
421
+ end
383
422
 
384
- def add_ensure2
385
- steps << :ensure2
386
- end
423
+ after do
424
+ steps << :after2
387
425
  end
388
- }
389
426
 
390
- it "runs the ensure_hook block and method in order" do
391
- expect(hooked.process).to eq([
392
- :process,
393
- :ensure1,
394
- :ensure2
395
- ])
396
- end
397
- end
427
+ ensure_hook do
428
+ steps << :ensure1
429
+ end
398
430
 
399
- context "with around, before, after and ensure_hook hooks" do
400
- let(:hooked) {
401
- build_hooked do
402
- around do |hooked|
403
- steps << :around_before1
404
- hooked.call
405
- steps << :around_after1
406
- end
407
-
408
- around do |hooked|
409
- steps << :around_before2
410
- hooked.call
411
- steps << :around_after2
412
- end
413
-
414
- before do
415
- steps << :before1
416
- end
417
-
418
- before do
419
- steps << :before2
420
- end
421
-
422
- after do
423
- steps << :after1
424
- end
425
-
426
- after do
427
- steps << :after2
428
- end
429
-
430
- ensure_hook do
431
- steps << :ensure1
432
- end
433
-
434
- ensure_hook do
435
- steps << :ensure2
436
- end
437
- end
438
- }
439
-
440
- it "runs hooks in the proper order" do
441
- expect(hooked.process).to eq([
442
- :around_before1,
443
- :around_before2,
444
- :before1,
445
- :before2,
446
- :process,
447
- :after2,
448
- :after1,
449
- :around_after2,
450
- :around_after1,
451
- :ensure1,
452
- :ensure2
453
- ])
431
+ ensure_hook do
432
+ steps << :ensure2
433
+ end
454
434
  end
435
+ }
436
+
437
+ it "runs hooks in the proper order" do
438
+ expect(hooked.process).to eq([
439
+ :around_before1,
440
+ :around_before2,
441
+ :before1,
442
+ :before2,
443
+ :process,
444
+ :after2,
445
+ :after1,
446
+ :around_after2,
447
+ :around_after1,
448
+ :ensure1,
449
+ :ensure2
450
+ ])
455
451
  end
456
452
  end
457
453
  end
458
454
  end
459
-
460
- # rubocop:enable RSpec/ScatteredSetup