trailblazer-macro 2.1.0.rc12 → 2.1.2

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.
@@ -36,11 +36,11 @@ class DocsPunditProcTest < Minitest::Spec
36
36
  it do
37
37
  #:pundit-result
38
38
  result = Create.(params: {}, current_user: Module)
39
- result["result.policy.default"].success? #=> true
40
- result["result.policy.default"]["policy"] #=> #<MyPolicy ...>
39
+ result[:"result.policy.default"].success? #=> true
40
+ result[:"result.policy.default"][:policy] #=> #<MyPolicy ...>
41
41
  #:pundit-result end
42
- result["result.policy.default"].success?.must_equal true
43
- result["result.policy.default"]["policy"].is_a?(MyPolicy).must_equal true
42
+ result[:"result.policy.default"].success?.must_equal true
43
+ result[:"result.policy.default"][:policy].is_a?(MyPolicy).must_equal true
44
44
  end
45
45
 
46
46
  #---
@@ -82,7 +82,7 @@ class DocsPunditProcTest < Minitest::Spec
82
82
  #:di-call
83
83
  Create.(params: {},
84
84
  current_user: Module,
85
- "policy.default.eval" => Trailblazer::Operation::Policy::Pundit.build(AnotherPolicy, :create?)
85
+ :"policy.default.eval" => Trailblazer::Operation::Policy::Pundit.build(AnotherPolicy, :create?)
86
86
  )
87
87
  #:di-call end
88
88
  result.inspect("").must_equal %{<Result:true [nil] >} }
@@ -104,9 +104,9 @@ class PunditWithNameTest < Minitest::Spec
104
104
  it {
105
105
  #:name-call
106
106
  result = Create.(params: {}, current_user: Module)
107
- result["result.policy.after_model"].success? #=> true
107
+ result[:"result.policy.after_model"].success? #=> true
108
108
  #:name-call end
109
- result["result.policy.after_model"].success?.must_equal true }
109
+ result[:"result.policy.after_model"].success?.must_equal true }
110
110
  end
111
111
 
112
112
  #---
@@ -23,7 +23,7 @@ class NestedRescueTest < Minitest::Spec
23
23
  fail ->(options, **) { options["outer-err"] = true }, id: "nested/failure"
24
24
  end
25
25
 
26
- it { Trailblazer::Developer.railway(NestedInsanity).must_match /\[>Rescue\(\d+\),>nested/ } # FIXME: better introspect tests for all id-generating macros.
26
+ it { Trailblazer::Developer.railway(NestedInsanity).must_match /\[>Rescue\(.{8}\),>nested/ } # FIXME: better introspect tests for all id-generating macros.
27
27
  it { NestedInsanity.().inspect("a", "y", "z", "b", "c", "e", "inner-err", "outer-err").must_equal %{<Result:true [true, true, true, true, true, true, nil, nil] >} }
28
28
  it { NestedInsanity.( "raise-y" => true).inspect("a", "y", "z", "b", "c", "e", "inner-err", "outer-err").must_equal %{<Result:false [true, true, nil, nil, nil, nil, true, true] >} }
29
29
  it { NestedInsanity.( "raise-a" => true).inspect("a", "y", "z", "b", "c", "e", "inner-err", "outer-err").must_equal %{<Result:false [true, true, true, true, nil, nil, nil, true] >} }
@@ -1,9 +1,6 @@
1
1
  require "test_helper"
2
2
 
3
3
  class DocsWrapTest < Minitest::Spec
4
- module Memo
5
- end
6
-
7
4
  =begin
8
5
  When success: return the block's returns
9
6
  When raise: return {Railway.fail!}
@@ -11,11 +8,10 @@ When raise: return {Railway.fail!}
11
8
  #:wrap-handler
12
9
  class HandleUnsafeProcess
13
10
  def self.call((ctx, flow_options), *, &block)
14
- begin
15
- yield # calls the wrapped steps
16
- rescue
17
- [ Trailblazer::Operation::Railway.fail!, [ctx, flow_options] ]
18
- end
11
+ yield # calls the wrapped steps
12
+ rescue
13
+ ctx[:exception] = $!.message
14
+ [ Trailblazer::Operation::Railway.fail!, [ctx, flow_options] ]
19
15
  end
20
16
  end
21
17
  #:wrap-handler end
@@ -64,6 +60,11 @@ result.wtf? #=>
64
60
  =end
65
61
  end
66
62
 
63
+ =begin
64
+ Writing into ctx in a Wrap()
65
+ =end
66
+ it { Memo::Create.( { seq: [], rehash_raise: true } )[:exception].must_equal("nope!") }
67
+
67
68
  =begin
68
69
  When success: return the block's returns
69
70
  When raise: return {Railway.fail!}, but wire Wrap() to {fail_fast: true}
@@ -74,13 +75,9 @@ When raise: return {Railway.fail!}, but wire Wrap() to {fail_fast: true}
74
75
  class Memo::Create < Trailblazer::Operation
75
76
  class HandleUnsafeProcess
76
77
  def self.call((ctx), *, &block)
77
- begin
78
- yield # calls the wrapped steps
79
- rescue
80
- puts $!
81
- ctx[:exception] = $!.message
82
- [ Trailblazer::Operation::Railway.fail!, [ctx, {}] ]
83
- end
78
+ yield # calls the wrapped steps
79
+ rescue
80
+ [ Trailblazer::Operation::Railway.fail!, [ctx, {}] ]
84
81
  end
85
82
  end
86
83
 
@@ -112,11 +109,9 @@ When raise: return {Railway.fail_fast!} and configure Wrap() to {fast_track: t
112
109
  #:fail-fast-handler
113
110
  class HandleUnsafeProcess
114
111
  def self.call((ctx), *, &block)
115
- begin
116
- yield # calls the wrapped steps
117
- rescue
118
- [ Trailblazer::Operation::Railway.fail_fast!, [ctx, {}] ]
119
- end
112
+ yield # calls the wrapped steps
113
+ rescue
114
+ [ Trailblazer::Operation::Railway.fail_fast!, [ctx, {}] ]
120
115
  end
121
116
  end
122
117
  #:fail-fast-handler end
@@ -145,120 +140,176 @@ When raise: return {Railway.fail_fast!} and configure Wrap() to {fast_track: t
145
140
  When success: return the block's returns
146
141
  When raise: return {Railway.fail!} or {Railway.pass!}
147
142
  =end
148
- class WrapWithTransactionTest < Minitest::Spec
149
- Memo = Module.new
143
+ class WrapWithCustomEndsTest < Minitest::Spec
144
+ Memo = Module.new
150
145
 
151
- module Sequel
152
- def self.transaction
153
- begin
154
- end_event, (ctx, flow_options) = yield
155
- true
156
- rescue
157
- false
158
- end
146
+ #:custom-handler
147
+ class MyTransaction
148
+ MyFailSignal = Class.new(Trailblazer::Activity::Signal)
149
+
150
+ def self.call((ctx, flow_options), *, &block)
151
+ yield # calls the wrapped steps
152
+ rescue
153
+ MyFailSignal
159
154
  end
160
155
  end
156
+ #:custom-handler end
161
157
 
162
- #:transaction-handler
163
- class MyTransaction
164
- def self.call((ctx, flow_options), *, &block)
165
- result = Sequel.transaction { yield }
158
+ #:custom
159
+ class Memo::Create < Trailblazer::Operation
160
+ step :find_model
161
+ step Wrap( MyTransaction ) {
162
+ step :update
163
+ step :rehash
164
+ },
165
+ Output(:success) => End(:transaction_worked),
166
+ Output(MyTransaction::MyFailSignal, :failure) => End(:transaction_failed)
167
+ step :notify
168
+ fail :log_error
169
+ #~methods
170
+ include T.def_steps(:find_model, :update, :notify, :log_error)
171
+ include Rehash
172
+ #~methods end
173
+ end
174
+ #:custom end
166
175
 
167
- signal = result ? Trailblazer::Operation::Railway.pass! : Trailblazer::Operation::Railway.fail!
176
+ it do
177
+ result = Memo::Create.( { seq: [] } )
178
+ result.inspect(:seq).must_equal %{<Result:false [[:find_model, :update, :rehash]] >}
179
+ result.event.inspect.must_equal %{#<Trailblazer::Activity::End semantic=:transaction_worked>}
180
+ end
168
181
 
169
- [ signal, [ctx, flow_options] ]
170
- end
182
+ it do
183
+ result = Memo::Create.( { seq: [], rehash_raise: true } )
184
+ result.inspect(:seq).must_equal %{<Result:false [[:find_model, :update, :rehash]] >}
185
+ result.event.inspect.must_equal %{#<Trailblazer::Activity::End semantic=:transaction_failed>}
171
186
  end
172
- #:transaction-handler end
187
+ end
188
+
189
+ =begin
190
+ When success: return the block's returns
191
+ When raise: return {Railway.pass!} and go "successful"
192
+ =end
193
+ class WrapGoesIntoPassFromRescueTest < Minitest::Spec
194
+ Memo = Module.new
173
195
 
174
- #:transaction
175
196
  class Memo::Create < Trailblazer::Operation
197
+ class HandleUnsafeProcess
198
+ def self.call((ctx), *, &block)
199
+ yield # calls the wrapped steps
200
+ rescue
201
+ [ Trailblazer::Operation::Railway.pass!, [ctx, {}] ]
202
+ end
203
+ end
204
+
176
205
  step :find_model
177
- step Wrap( MyTransaction ) {
206
+ step Wrap( HandleUnsafeProcess ) {
178
207
  step :update
179
208
  step :rehash
180
209
  }
181
210
  step :notify
182
211
  fail :log_error
212
+
183
213
  #~methods
184
214
  include T.def_steps(:find_model, :update, :notify, :log_error)
185
215
  include Rehash
186
216
  #~methods end
187
217
  end
188
- #:transaction end
189
218
 
190
219
  it { Memo::Create.( { seq: [] } ).inspect(:seq).must_equal %{<Result:true [[:find_model, :update, :rehash, :notify]] >} }
191
- it { Memo::Create.( { seq: [], rehash_raise: true } ).inspect(:seq).must_equal %{<Result:false [[:find_model, :update, :rehash, :log_error]] >} }
220
+ it { Memo::Create.( { seq: [], rehash_raise: true } ).inspect(:seq).must_equal %{<Result:true [[:find_model, :update, :rehash, :notify]] >} }
192
221
  end
193
222
 
194
223
  =begin
195
224
  When success: return the block's returns
196
- When raise: return {Railway.fail!} or {Railway.pass!}
225
+ When raise: return {true} and go "successful"
226
+ You can return boolean true in wrap.
197
227
  =end
198
- class WrapWithCustomEndsTest < Minitest::Spec
199
- Memo = Module.new
200
- Sequel = WrapWithTransactionTest::Sequel
228
+ class WrapGoesIntoBooleanTrueFromRescueTest < Minitest::Spec
229
+ Memo = Module.new
201
230
 
202
- #:custom-handler
203
- class MyTransaction
204
- MyFailSignal = Class.new(Trailblazer::Activity::Signal)
231
+ class Memo::Create < Trailblazer::Operation
232
+ class HandleUnsafeProcess
233
+ def self.call((ctx), *, &block)
234
+ yield # calls the wrapped steps
235
+ rescue
236
+ true
237
+ end
238
+ end
205
239
 
206
- def self.call((ctx, flow_options), *, &block)
207
- result = Sequel.transaction { yield }
240
+ step :find_model
241
+ step Wrap( HandleUnsafeProcess ) {
242
+ step :update
243
+ step :rehash
244
+ }
245
+ step :notify
246
+ fail :log_error
208
247
 
209
- signal = result ? Trailblazer::Operation::Railway.pass! : MyFailSignal
248
+ #~methods
249
+ include T.def_steps(:find_model, :update, :notify, :log_error)
250
+ include Rehash
251
+ #~methods end
252
+ end
210
253
 
211
- [ signal, [ctx, flow_options] ]
212
- end
254
+ it "translates true returned form a wrap to a signal with a `success` semantic" do
255
+ result = Memo::Create.( { seq: [], rehash_raise: true } )
256
+ result.inspect(:seq).must_equal %{<Result:true [[:find_model, :update, :rehash, :notify]] >}
257
+ result.event.inspect.must_equal %{#<Trailblazer::Activity::Railway::End::Success semantic=:success>}
213
258
  end
214
- #:custom-handler end
259
+ end
260
+
261
+ =begin
262
+ When success: return the block's returns
263
+ When raise: return {false} and go "failed"
264
+ You can return boolean false in wrap.
265
+ =end
266
+ class WrapGoesIntoBooleanFalseFromRescueTest < Minitest::Spec
267
+ Memo = Module.new
215
268
 
216
- #:custom
217
269
  class Memo::Create < Trailblazer::Operation
270
+ class HandleUnsafeProcess
271
+ def self.call((ctx), *, &block)
272
+ yield # calls the wrapped steps
273
+ rescue
274
+ false
275
+ end
276
+ end
277
+
218
278
  step :find_model
219
- step Wrap( MyTransaction ) {
279
+ step Wrap( HandleUnsafeProcess ) {
220
280
  step :update
221
281
  step :rehash
222
- },
223
- Output(:success) => End(:transaction_worked),
224
- Output(MyTransaction::MyFailSignal, :failure) => End(:transaction_failed)
282
+ }
225
283
  step :notify
226
284
  fail :log_error
285
+
227
286
  #~methods
228
287
  include T.def_steps(:find_model, :update, :notify, :log_error)
229
288
  include Rehash
230
289
  #~methods end
231
290
  end
232
- #:custom end
233
-
234
- it do
235
- result = Memo::Create.( { seq: [] } )
236
- result.inspect(:seq).must_equal %{<Result:false [[:find_model, :update, :rehash]] >}
237
- result.event.inspect.must_equal %{#<Trailblazer::Activity::End semantic=:transaction_worked>}
238
- end
239
291
 
240
- it do
292
+ it "translates false returned form a wrap to a signal with a `failure` semantic" do
241
293
  result = Memo::Create.( { seq: [], rehash_raise: true } )
242
- result.inspect(:seq).must_equal %{<Result:false [[:find_model, :update, :rehash]] >}
243
- result.event.inspect.must_equal %{#<Trailblazer::Activity::End semantic=:transaction_failed>}
294
+ result.inspect(:seq).must_equal %{<Result:false [[:find_model, :update, :rehash, :log_error]] >}
295
+ result.event.inspect.must_equal %{#<Trailblazer::Activity::Railway::End::Failure semantic=:failure>}
244
296
  end
245
297
  end
246
298
 
247
299
  =begin
248
300
  When success: return the block's returns
249
- When raise: return {Railway.pass!} and go "successful"
301
+ When raise: return {nil} and go "failed"
302
+ You can return nil in wrap.
250
303
  =end
251
- class WrapGoesIntoPassFromRescueTest < Minitest::Spec
304
+ class WrapGoesIntoNilFromRescueTest < Minitest::Spec
252
305
  Memo = Module.new
253
306
 
254
307
  class Memo::Create < Trailblazer::Operation
255
308
  class HandleUnsafeProcess
256
309
  def self.call((ctx), *, &block)
257
- begin
258
- yield # calls the wrapped steps
259
- rescue
260
- [ Trailblazer::Operation::Railway.pass!, [ctx, {}] ]
261
- end
310
+ yield # calls the wrapped steps
311
+ rescue
312
+ nil
262
313
  end
263
314
  end
264
315
 
@@ -276,7 +327,94 @@ When raise: return {Railway.pass!} and go "successful"
276
327
  #~methods end
277
328
  end
278
329
 
330
+ it "translates nil returned form a wrap to a signal with a `failure` semantic" do
331
+ result = Memo::Create.( { seq: [], rehash_raise: true } )
332
+ result.inspect(:seq).must_equal %{<Result:false [[:find_model, :update, :rehash, :log_error]] >}
333
+ result.event.inspect.must_equal %{#<Trailblazer::Activity::Railway::End::Failure semantic=:failure>}
334
+ end
335
+ end
336
+
337
+ =begin
338
+ When success: return the block's returns
339
+ When raise: return {Railway.fail!}
340
+ This one is mostly to show how one could wrap steps in a transaction
341
+ =end
342
+ class WrapWithTransactionTest < Minitest::Spec
343
+ Memo = Module.new
344
+
345
+ module Sequel
346
+ def self.transaction
347
+ end_event, (ctx, flow_options) = yield
348
+ end
349
+ end
350
+
351
+ #:transaction-handler
352
+ class MyTransaction
353
+ def self.call((ctx, flow_options), *, &block)
354
+ Sequel.transaction { yield } # calls the wrapped steps
355
+ rescue
356
+ [ Trailblazer::Operation::Railway.fail!, [ctx, flow_options] ]
357
+ end
358
+ end
359
+ #:transaction-handler end
360
+
361
+ #:transaction
362
+ class Memo::Create < Trailblazer::Operation
363
+ step :find_model
364
+ step Wrap( MyTransaction ) {
365
+ step :update
366
+ step :rehash
367
+ }
368
+ step :notify
369
+ fail :log_error
370
+ #~methods
371
+ include T.def_steps(:find_model, :update, :notify, :log_error)
372
+ include Rehash
373
+ #~methods end
374
+ end
375
+ #:transaction end
376
+
279
377
  it { Memo::Create.( { seq: [] } ).inspect(:seq).must_equal %{<Result:true [[:find_model, :update, :rehash, :notify]] >} }
280
- it { Memo::Create.( { seq: [], rehash_raise: true } ).inspect(:seq).must_equal %{<Result:true [[:find_model, :update, :rehash, :notify]] >} }
378
+ it { Memo::Create.( { seq: [], rehash_raise: true } ).inspect(:seq).must_equal %{<Result:false [[:find_model, :update, :rehash, :log_error]] >} }
379
+ end
380
+
381
+ =begin
382
+ When success: return {Railway.pass_fast!}
383
+ When failure: return {Railway.fail!}
384
+ This one is mostly to show how one could evaluate Wrap()'s return value based on Wrap() block's return
385
+ =end
386
+ class WrapWithBlockReturnSignatureCheckTest < Minitest::Spec
387
+ Memo = Module.new
388
+
389
+ #:handler-with-signature-evaluator
390
+ class HandleUnsafeProcess
391
+ def self.call((_ctx, _flow_options), *, &block)
392
+ signal, (ctx, flow_options) = yield
393
+ evaluated_signal = if signal.to_h[:semantic] == :success
394
+ Trailblazer::Operation::Railway.pass_fast!
395
+ else
396
+ Trailblazer::Operation::Railway.fail!
397
+ end
398
+ [ evaluated_signal, [ctx, flow_options] ]
399
+ end
400
+ end
401
+ #:handler-with-signature-evaluator end
402
+
403
+ #:transaction
404
+ class Memo::Create < Trailblazer::Operation
405
+ step :find_model
406
+ step Wrap( HandleUnsafeProcess ) {
407
+ step :update
408
+ }, fast_track: true # because Wrap can return pass_fast! now
409
+ step :notify
410
+ fail :log_error
411
+ #~methods
412
+ include T.def_steps(:find_model, :update, :notify, :log_error)
413
+ #~methods end
414
+ end
415
+ #:transaction end
416
+
417
+ it { Memo::Create.( { seq: [] } ).inspect(:seq).must_equal %{<Result:true [[:find_model, :update]] >} }
418
+ it { Memo::Create.( { seq: [], update: false } ).inspect(:seq).must_equal %{<Result:false [[:find_model, :update, :log_error]] >} }
281
419
  end
282
420
  end
@@ -19,11 +19,8 @@ class ModelTest < Minitest::Spec
19
19
  end
20
20
 
21
21
  # :new new.
22
- it { Create.(params: {})[:model].inspect.must_equal %{#<struct ModelTest::Song id=nil, title=nil>} }
23
- it do
24
-
25
- result = Create.(params: {})
26
-
22
+ it "initializes new model's instance" do
23
+ result = Create.()
27
24
  result[:model].inspect.must_equal %{#<struct ModelTest::Song id=nil, title=nil>}
28
25
  end
29
26
 
@@ -48,7 +45,7 @@ class ModelTest < Minitest::Spec
48
45
  step Trailblazer::Operation::Model Song, :find_by
49
46
  step :process
50
47
 
51
- def process(options, **); options["x"] = true end
48
+ def process(options, **); options[:x] = true end
52
49
  end
53
50
 
54
51
  # :find_by, exceptionless.
@@ -57,36 +54,36 @@ class ModelTest < Minitest::Spec
57
54
  step Trailblazer::Operation::Model( Song, :find_by, :title )
58
55
  step :process
59
56
 
60
- def process(options, **); options["x"] = true end
57
+ def process(options, **); options[:x] = true end
61
58
  end
62
59
 
63
60
  # can't find model.
64
61
  #- result object, model
65
62
  it do
66
- Find.(params: {id: nil})["result.model"].failure?.must_equal true
67
- Find.(params: {id: nil})["x"].must_be_nil
63
+ Find.(params: {id: nil})[:"result.model"].failure?.must_equal true
64
+ Find.(params: {id: nil})[:"x"].must_be_nil
68
65
  Find.(params: {id: nil}).failure?.must_equal true
69
66
  end
70
67
 
71
68
  #- result object, model
72
69
  it do
73
- Find.(params: {id: 9})["result.model"].success?.must_equal true
74
- Find.(params: {id: 9})["x"].must_equal true
70
+ Find.(params: {id: 9})[:"result.model"].success?.must_equal true
71
+ Find.(params: {id: 9})[:"x"].must_equal true
75
72
  Find.(params: {id: 9})[:model].inspect.must_equal %{#<struct ModelTest::Song id=9, title=nil>}
76
73
  end
77
74
 
78
75
  # can't find model by title.
79
76
  #- result object, model
80
77
  it do
81
- FindByKey.(params: {title: nil})["result.model"].failure?.must_equal true
82
- FindByKey.(params: {title: nil})["x"].must_be_nil
78
+ FindByKey.(params: {title: nil})[:"result.model"].failure?.must_equal true
79
+ FindByKey.(params: {title: nil})[:"x"].must_be_nil
83
80
  FindByKey.(params: {title: nil}).failure?.must_equal true
84
81
  end
85
82
 
86
83
  #- result object, model by title
87
84
  it do
88
- FindByKey.(params: {title: "Test"})["result.model"].success?.must_equal true
89
- FindByKey.(params: {title: "Test"})["x"].must_equal true
85
+ FindByKey.(params: {title: "Test"})[:"result.model"].success?.must_equal true
86
+ FindByKey.(params: {title: "Test"})[:"x"].must_equal true
90
87
  FindByKey.(params: {title: "Test"})[:model].inspect.must_equal %{#<struct ModelTest::Song id=2, title="Test">}
91
88
  end
92
89
  end