trailblazer 2.0.7 → 2.1.0.beta1
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/CHANGES.md +35 -1
- data/Gemfile +6 -12
- data/README.md +3 -1
- data/Rakefile +6 -17
- data/lib/trailblazer.rb +7 -4
- data/lib/trailblazer/deprecation/call.rb +46 -0
- data/lib/trailblazer/deprecation/context.rb +43 -0
- data/lib/trailblazer/operation/contract.rb +40 -9
- data/lib/trailblazer/operation/deprecations.rb +21 -0
- data/lib/trailblazer/operation/guard.rb +5 -5
- data/lib/trailblazer/operation/model.rb +15 -10
- data/lib/trailblazer/operation/nested.rb +56 -85
- data/lib/trailblazer/operation/persist.rb +4 -2
- data/lib/trailblazer/operation/policy.rb +16 -7
- data/lib/trailblazer/operation/pundit.rb +3 -3
- data/lib/trailblazer/operation/representer.rb +5 -0
- data/lib/trailblazer/operation/rescue.rb +12 -9
- data/lib/trailblazer/operation/validate.rb +36 -29
- data/lib/trailblazer/operation/wrap.rb +49 -11
- data/lib/trailblazer/task.rb +20 -0
- data/lib/trailblazer/version.rb +1 -1
- data/test/benchmark.rb +63 -0
- data/test/deprecation/call_test.rb +42 -0
- data/test/deprecation/context_test.rb +19 -0
- data/test/docs/contract_test.rb +73 -53
- data/test/docs/dry_test.rb +2 -2
- data/test/docs/fast_test.rb +133 -13
- data/test/docs/guard_test.rb +28 -35
- data/test/docs/macro_test.rb +1 -1
- data/test/docs/model_test.rb +13 -13
- data/test/docs/nested_test.rb +54 -122
- data/test/docs/operation_test.rb +42 -43
- data/test/docs/pundit_test.rb +16 -16
- data/test/docs/representer_test.rb +18 -18
- data/test/docs/rescue_test.rb +29 -29
- data/test/docs/trace_test.rb +82 -0
- data/test/docs/wrap_test.rb +59 -26
- data/test/module_test.rb +75 -75
- data/test/nested_test.rb +293 -0
- data/test/operation/contract_test.rb +23 -153
- data/test/operation/dsl/contract_test.rb +9 -9
- data/test/operation/dsl/representer_test.rb +169 -169
- data/test/operation/model_test.rb +15 -21
- data/test/operation/persist_test.rb +18 -11
- data/test/operation/pundit_test.rb +25 -23
- data/test/operation/representer_test.rb +254 -254
- data/test/test_helper.rb +5 -2
- data/test/variables_test.rb +158 -0
- data/trailblazer.gemspec +1 -1
- data/untitled +33 -0
- metadata +25 -27
- data/lib/trailblazer/operation/callback.rb +0 -35
- data/lib/trailblazer/operation/procedural/contract.rb +0 -15
- data/lib/trailblazer/operation/procedural/validate.rb +0 -22
- data/test/operation/callback_test.rb +0 -70
- data/test/operation/dsl/callback_test.rb +0 -106
- data/test/operation/params_test.rb +0 -36
- data/test/operation/pipedream_test.rb +0 -59
- data/test/operation/pipetree_test.rb +0 -104
- data/test/operation/present_test.rb +0 -24
- data/test/operation/resolver_test.rb +0 -47
- data/test/operation_test.rb +0 -143
data/test/docs/operation_test.rb
CHANGED
@@ -11,8 +11,8 @@ class DocsOperationExampleTest < Minitest::Spec
|
|
11
11
|
step Model( Song, :new )
|
12
12
|
step :assign_current_user!
|
13
13
|
# ..
|
14
|
-
def assign_current_user!(options)
|
15
|
-
options[
|
14
|
+
def assign_current_user!(options, **)
|
15
|
+
options[:model].created_by = options[:current_user]
|
16
16
|
end
|
17
17
|
end
|
18
18
|
#:invocation-dep end
|
@@ -20,46 +20,44 @@ class DocsOperationExampleTest < Minitest::Spec
|
|
20
20
|
it do
|
21
21
|
current_user = User.new("Ema")
|
22
22
|
#:invocation-dep-call
|
23
|
-
result = Create.( { title: "Roxanne" },
|
23
|
+
result = Create.( params: { title: "Roxanne" }, :current_user => current_user )
|
24
24
|
#:invocation-dep-call end
|
25
25
|
|
26
26
|
#:invocation-dep-res
|
27
|
-
result[
|
28
|
-
result[
|
27
|
+
result[:current_user] #=> #<User name="Ema">
|
28
|
+
result[:model] #=> #<Song id=nil, title=nil, created_by=#<User name="Ema">>
|
29
29
|
#:invocation-dep-res end
|
30
30
|
end
|
31
31
|
|
32
|
-
it { Create.({ title: "Roxanne" },
|
32
|
+
it { Create.(params: { title: "Roxanne" }, :current_user => Module).inspect(:model).must_equal %{<Result:true [#<struct DocsOperationExampleTest::Song id=nil, title=nil, created_by=Module>] >} }
|
33
33
|
|
34
34
|
#:op
|
35
35
|
class Song::Create < Trailblazer::Operation
|
36
|
-
|
37
|
-
|
38
|
-
contract do
|
36
|
+
class Form < Reform::Form
|
39
37
|
property :title
|
40
38
|
validates :title, presence: true
|
41
39
|
end
|
42
40
|
|
43
41
|
step Model( Song, :new )
|
44
42
|
step :assign_current_user!
|
45
|
-
step Contract::Build()
|
43
|
+
step Contract::Build( constant: Form )
|
46
44
|
step Contract::Validate( )
|
47
45
|
failure :log_error!
|
48
46
|
step Contract::Persist( )
|
49
47
|
|
50
|
-
def log_error!(options)
|
48
|
+
def log_error!(options, **)
|
51
49
|
# ..
|
52
50
|
end
|
53
51
|
|
54
|
-
def assign_current_user!(options)
|
55
|
-
options[
|
56
|
-
options[
|
52
|
+
def assign_current_user!(options, **)
|
53
|
+
options[:model].created_by =
|
54
|
+
options[:current_user]
|
57
55
|
end
|
58
56
|
end
|
59
57
|
#:op end
|
60
58
|
|
61
|
-
it { Song::Create.({ }).inspect(
|
62
|
-
it { Song::Create.({ title: "Nothin'" },
|
59
|
+
it { Song::Create.(params: { }).inspect(:model).must_equal %{<Result:false [#<struct DocsOperationExampleTest::Song id=nil, title=nil, created_by=nil>] >} }
|
60
|
+
it { Song::Create.(params: { title: "Nothin'" }, :current_user=>Module).inspect(:model).must_equal %{<Result:true [#<struct DocsOperationExampleTest::Song id=nil, title="Nothin'", created_by=Module>] >} }
|
63
61
|
end
|
64
62
|
|
65
63
|
class DndTest < Minitest::Spec
|
@@ -83,8 +81,8 @@ class DocsResultTest < Minitest::Spec
|
|
83
81
|
step :validate!
|
84
82
|
|
85
83
|
def model!(options, current_user:, **)
|
86
|
-
options[
|
87
|
-
options[
|
84
|
+
options[:model] = Song.new
|
85
|
+
options[:model].created_by = current_user
|
88
86
|
end
|
89
87
|
|
90
88
|
def assign!(*, params:, model:, **)
|
@@ -102,13 +100,13 @@ class DocsResultTest < Minitest::Spec
|
|
102
100
|
it do
|
103
101
|
current_user = Struct.new(:email).new("nick@trailblazer.to")
|
104
102
|
#:step-res
|
105
|
-
result = Song::Create.({ title: "Roxanne" },
|
103
|
+
result = Song::Create.(params: { title: "Roxanne" }, :current_user => current_user)
|
106
104
|
|
107
|
-
result[
|
105
|
+
result[:model] #=> #<Song title="Roxanne", "created_by"=<User ...>
|
108
106
|
result["result.validate"] #=> true
|
109
107
|
#:step-res end
|
110
108
|
|
111
|
-
result.inspect(
|
109
|
+
result.inspect(:current_user, :model).must_equal %{<Result:true [#<struct email=\"nick@trailblazer.to\">, #<struct DocsResultTest::Song id=nil, title="Roxanne", created_by=#<struct email=\"nick@trailblazer.to\">>] >}
|
112
110
|
|
113
111
|
#:step-binary
|
114
112
|
result.success? #=> true
|
@@ -116,11 +114,11 @@ class DocsResultTest < Minitest::Spec
|
|
116
114
|
#:step-binary end
|
117
115
|
|
118
116
|
#:step-dep
|
119
|
-
result[
|
117
|
+
result[:current_user] #=> <User ...>
|
120
118
|
#:step-dep end
|
121
119
|
|
122
120
|
#:step-inspect
|
123
|
-
result.inspect(
|
121
|
+
result.inspect(:current_user, :model) #=> "<Result:true [#<User email=\"nick@tra... "
|
124
122
|
#:step-inspect end
|
125
123
|
end
|
126
124
|
end
|
@@ -133,6 +131,7 @@ class DocsDependencyTest < Minitest::Spec
|
|
133
131
|
|
134
132
|
#:dep-op
|
135
133
|
class Song::Create < Trailblazer::Operation
|
134
|
+
extend ClassDependencies
|
136
135
|
self["my.model.class"] = Song
|
137
136
|
|
138
137
|
#~dep-pipe
|
@@ -194,19 +193,19 @@ class DocsOperationAPIExampleTest < Minitest::Spec
|
|
194
193
|
failure :log_error!
|
195
194
|
step Contract::Persist()
|
196
195
|
|
197
|
-
def log_error!(options)
|
196
|
+
def log_error!(options, **)
|
198
197
|
# ..
|
199
198
|
end
|
200
199
|
|
201
|
-
def assign_current_user!(options)
|
202
|
-
options[
|
203
|
-
options[
|
200
|
+
def assign_current_user!(options, **)
|
201
|
+
options[:model].created_by =
|
202
|
+
options[:current_user]
|
204
203
|
end
|
205
204
|
end
|
206
205
|
#:op-api end
|
207
206
|
|
208
|
-
it { Song::Create.({
|
209
|
-
it { Song::Create.({ title: "Nothin'" },
|
207
|
+
it { Song::Create.(params: {}).inspect(:model).must_equal %{<Result:false [#<struct DocsOperationAPIExampleTest::Song id=nil, title=nil, created_by=nil>] >} }
|
208
|
+
it { Song::Create.(params: { title: "Nothin'" }, :current_user=>Module).inspect(:model).must_equal %{<Result:true [#<struct DocsOperationAPIExampleTest::Song id=nil, title="Nothin'", created_by=Module>] >} }
|
210
209
|
end
|
211
210
|
|
212
211
|
|
@@ -227,7 +226,7 @@ class DocsOperationInheritanceTest < Minitest::Spec
|
|
227
226
|
end
|
228
227
|
#:inh-new end
|
229
228
|
|
230
|
-
puts
|
229
|
+
puts Trailblazer::Operation::Inspect.(New, style: :row)
|
231
230
|
=begin
|
232
231
|
#:inh-new-pipe
|
233
232
|
0 =======================>>operation.new
|
@@ -243,7 +242,7 @@ class DocsOperationInheritanceTest < Minitest::Spec
|
|
243
242
|
end
|
244
243
|
#:inh-create end
|
245
244
|
|
246
|
-
puts
|
245
|
+
puts Trailblazer::Operation::Inspect.(Create, style: :row)
|
247
246
|
=begin
|
248
247
|
#:inh-create-pipe
|
249
248
|
0 =======================>>operation.new
|
@@ -279,7 +278,7 @@ class DocsOperationInheritanceTest < Minitest::Spec
|
|
279
278
|
end
|
280
279
|
#:override-new end
|
281
280
|
|
282
|
-
puts Song::New
|
281
|
+
puts Trailblazer::Operation::Inspect.(Song::New, style: :row)
|
283
282
|
=begin
|
284
283
|
#:override-pipe
|
285
284
|
Song::New["pipetree"].inspect(style: :row)
|
@@ -290,8 +289,8 @@ class DocsOperationInheritanceTest < Minitest::Spec
|
|
290
289
|
=end
|
291
290
|
|
292
291
|
it do
|
293
|
-
Song::New
|
294
|
-
Song::New.().inspect(
|
292
|
+
Trailblazer::Operation::Inspect.(Song::New).must_equal %{[>model.build,>contract.build]}
|
293
|
+
Song::New.(params: {}).inspect(:model).must_equal %{<Result:true [#<struct DocsOperationInheritanceTest::Song id=nil, title=nil, created_by=nil>] >}
|
295
294
|
end
|
296
295
|
end
|
297
296
|
|
@@ -309,7 +308,7 @@ class DocsOperationStepOptionsTest < Minitest::Spec
|
|
309
308
|
end
|
310
309
|
#:name-auto end
|
311
310
|
|
312
|
-
puts
|
311
|
+
puts Trailblazer::Operation::Inspect.(New, style: :row)
|
313
312
|
=begin
|
314
313
|
#:name-auto-pipe
|
315
314
|
0 =======================>>operation.new
|
@@ -323,7 +322,7 @@ class DocsOperationStepOptionsTest < Minitest::Spec
|
|
323
322
|
end
|
324
323
|
#:replace-inh end
|
325
324
|
|
326
|
-
puts
|
325
|
+
puts Trailblazer::Operation::Inspect.(Update, style: :row)
|
327
326
|
=begin
|
328
327
|
#:replace-inh-pipe
|
329
328
|
0 =======================>>operation.new
|
@@ -331,7 +330,7 @@ class DocsOperationStepOptionsTest < Minitest::Spec
|
|
331
330
|
#:replace-inh-pipe end
|
332
331
|
=end
|
333
332
|
|
334
|
-
it { Update.({}).inspect(
|
333
|
+
it { Update.(params: {}).inspect(:model).must_equal %{<Result:false [nil] >} }
|
335
334
|
|
336
335
|
|
337
336
|
# #:delete-inh
|
@@ -349,19 +348,19 @@ class DocsOperationStepOptionsTest < Minitest::Spec
|
|
349
348
|
# #:delete-inh-pipe end
|
350
349
|
# =end
|
351
350
|
|
352
|
-
# it { Noop.({}).inspect(
|
351
|
+
# it { Noop.({}).inspect(:model).must_equal %{<Result:false [nil] >} }
|
353
352
|
end
|
354
353
|
|
355
354
|
class ManualNameTest < Minitest::Spec
|
356
355
|
#:name-manu
|
357
356
|
class New < Trailblazer::Operation
|
358
|
-
step
|
357
|
+
step(Model( Song, :new ), {name: "build.song.model"})
|
359
358
|
step :validate_params!, name: "my.params.validate"
|
360
359
|
# ..
|
361
360
|
end
|
362
361
|
#:name-manu end
|
363
362
|
|
364
|
-
puts
|
363
|
+
puts Trailblazer::Operation::Inspect.(New, style: :row)
|
365
364
|
=begin
|
366
365
|
#:name-manu-pipe
|
367
366
|
0 =======================>>operation.new
|
@@ -380,7 +379,7 @@ class DocsOperationStepOptionsTest < Minitest::Spec
|
|
380
379
|
end
|
381
380
|
#:pos-before end
|
382
381
|
|
383
|
-
puts
|
382
|
+
puts Trailblazer::Operation::Inspect.(New, style: :row)
|
384
383
|
=begin
|
385
384
|
#:pos-before-pipe
|
386
385
|
0 =======================>>operation.new
|
@@ -391,11 +390,11 @@ class DocsOperationStepOptionsTest < Minitest::Spec
|
|
391
390
|
|
392
391
|
#:pos-inh
|
393
392
|
class Create < New
|
394
|
-
step :policy!,
|
393
|
+
step :policy!, before: "model.build"
|
395
394
|
end
|
396
395
|
#:pos-inh end
|
397
396
|
|
398
|
-
puts
|
397
|
+
puts Trailblazer::Operation::Inspect.(Create, style: :row)
|
399
398
|
=begin
|
400
399
|
#:pos-inh-pipe
|
401
400
|
0 =======================>>operation.new
|
data/test/docs/pundit_test.rb
CHANGED
@@ -29,13 +29,13 @@ class DocsPunditProcTest < Minitest::Spec
|
|
29
29
|
end
|
30
30
|
#:pundit end
|
31
31
|
|
32
|
-
it { Create
|
33
|
-
it { Create.({},
|
34
|
-
it { Create.({} ).inspect(
|
32
|
+
it { Trailblazer::Operation::Inspect.(Create).must_equal %{[>model.build,>policy.default.eval]} }
|
33
|
+
it { Create.(params: {}, current_user: Module).inspect(:model).must_equal %{<Result:true [#<struct DocsPunditProcTest::Song id=nil>] >} }
|
34
|
+
it { Create.(params: {} ).inspect(:model).must_equal %{<Result:false [#<struct DocsPunditProcTest::Song id=nil>] >} }
|
35
35
|
|
36
36
|
it do
|
37
37
|
#:pundit-result
|
38
|
-
result = Create.({},
|
38
|
+
result = Create.(params: {}, current_user: Module)
|
39
39
|
result["result.policy.default"].success? #=> true
|
40
40
|
result["result.policy.default"]["policy"] #=> #<MyPolicy ...>
|
41
41
|
#:pundit-result end
|
@@ -49,9 +49,9 @@ class DocsPunditProcTest < Minitest::Spec
|
|
49
49
|
step Policy::Pundit( MyPolicy, :new? ), override: true
|
50
50
|
end
|
51
51
|
|
52
|
-
it { New
|
53
|
-
it { New.({},
|
54
|
-
it { New.({},
|
52
|
+
it { Trailblazer::Operation::Inspect.(New).must_equal %{[>model.build,>policy.default.eval]} }
|
53
|
+
it { New.(params: {}, current_user: Class ).inspect(:model).must_equal %{<Result:true [#<struct DocsPunditProcTest::Song id=nil>] >} }
|
54
|
+
it { New.(params: {}, current_user: nil ).inspect(:model).must_equal %{<Result:false [#<struct DocsPunditProcTest::Song id=nil>] >} }
|
55
55
|
|
56
56
|
#---
|
57
57
|
#- override with :name
|
@@ -64,10 +64,10 @@ class DocsPunditProcTest < Minitest::Spec
|
|
64
64
|
step Policy::Pundit( MyPolicy, :new?, name: "first" ), override: true
|
65
65
|
end
|
66
66
|
|
67
|
-
it { Edit
|
68
|
-
it { Edit.({},
|
69
|
-
it { Update
|
70
|
-
it { Update.({},
|
67
|
+
it { Trailblazer::Operation::Inspect.(Edit).must_equal %{[>policy.first.eval,>policy.second.eval]} }
|
68
|
+
it { Edit.(params: {}, current_user: Class).inspect(:model).must_equal %{<Result:false [nil] >} }
|
69
|
+
it { Trailblazer::Operation::Inspect.(Update).must_equal %{[>policy.first.eval,>policy.second.eval]} }
|
70
|
+
it { Update.(params: {}, current_user: Class).inspect(:model).must_equal %{<Result:true [nil] >} }
|
71
71
|
|
72
72
|
#---
|
73
73
|
# dependency injection
|
@@ -80,8 +80,8 @@ class DocsPunditProcTest < Minitest::Spec
|
|
80
80
|
it {
|
81
81
|
result =
|
82
82
|
#:di-call
|
83
|
-
Create.({},
|
84
|
-
|
83
|
+
Create.(params: {},
|
84
|
+
current_user: Module,
|
85
85
|
"policy.default.eval" => Trailblazer::Operation::Policy::Pundit.build(AnotherPolicy, :create?)
|
86
86
|
)
|
87
87
|
#:di-call end
|
@@ -103,7 +103,7 @@ class PunditWithNameTest < Minitest::Spec
|
|
103
103
|
|
104
104
|
it {
|
105
105
|
#:name-call
|
106
|
-
result = Create.({},
|
106
|
+
result = Create.(params: {}, current_user: Module)
|
107
107
|
result["result.policy.after_model"].success? #=> true
|
108
108
|
#:name-call end
|
109
109
|
result["result.policy.after_model"].success?.must_equal true }
|
@@ -123,8 +123,8 @@ end
|
|
123
123
|
# #:class-level end
|
124
124
|
|
125
125
|
# it { Create.(); Create["result.policy"].must_equal nil }
|
126
|
-
# it { Create.({},
|
127
|
-
# it { Create.({} )["x"].must_equal nil }
|
126
|
+
# it { Create.(params: {}, current_user: Module)["x"].must_equal true }
|
127
|
+
# it { Create.(params: {} )["x"].must_equal nil }
|
128
128
|
# end
|
129
129
|
|
130
130
|
|
@@ -20,7 +20,7 @@ class DocsRepresenterInferTest < Minitest::Spec
|
|
20
20
|
#:infer end
|
21
21
|
|
22
22
|
let (:json) { MultiJson.dump(id: 1) }
|
23
|
-
it { Create.({},
|
23
|
+
it { Create.( params: {}, document: json ).inspect(:model).must_equal %{<Result:true [#<struct DocsRepresenterInferTest::Song id=1, title=nil>] >} }
|
24
24
|
end
|
25
25
|
|
26
26
|
#---
|
@@ -49,18 +49,18 @@ class DocsRepresenterExplicitTest < Minitest::Spec
|
|
49
49
|
#:explicit-op end
|
50
50
|
|
51
51
|
let (:json) { MultiJson.dump(id: 1) }
|
52
|
-
it { Create.({},
|
52
|
+
it { Create.(params: {}, document: json).inspect(:model).must_equal %{<Result:true [#<struct DocsRepresenterExplicitTest::Song id=1, title=nil>] >} }
|
53
53
|
it do
|
54
54
|
#:explicit-call
|
55
|
-
Create.({},
|
55
|
+
Create.(params: {}, document: '{"id": 1}')
|
56
56
|
#:explicit-call end
|
57
57
|
end
|
58
58
|
|
59
59
|
#- render
|
60
60
|
it do
|
61
61
|
#:render
|
62
|
-
result = Create.({},
|
63
|
-
json = result["representer.default.class"].new(result[
|
62
|
+
result = Create.( params: {}, document: '{"id": 1}' )
|
63
|
+
json = result["representer.default.class"].new(result[:model]).to_json
|
64
64
|
json #=> '{"id":1}'
|
65
65
|
#:render end
|
66
66
|
json.must_equal '{"id":1}'
|
@@ -81,12 +81,12 @@ class DocsRepresenterExplicitTest < Minitest::Spec
|
|
81
81
|
let (:xml) { %{<body><id>1</id></body>} }
|
82
82
|
it do
|
83
83
|
#:di-call
|
84
|
-
result = Create.({},
|
85
|
-
|
84
|
+
result = Create.(params: {},
|
85
|
+
document: '<body><id>1</id></body>',
|
86
86
|
"representer.default.class" => MyXMLRepresenter # injection
|
87
87
|
)
|
88
88
|
#:di-call end
|
89
|
-
result.inspect(
|
89
|
+
result.inspect(:model).must_equal %{<Result:true [#<struct DocsRepresenterExplicitTest::Song id="1", title=nil>] >}
|
90
90
|
end
|
91
91
|
end
|
92
92
|
|
@@ -112,8 +112,8 @@ class DocsRepresenterDITest < Minitest::Spec
|
|
112
112
|
end
|
113
113
|
|
114
114
|
let (:json) { MultiJson.dump(id: 1) }
|
115
|
-
it { Create.({},
|
116
|
-
"representer.default.class" => MyRepresenter).inspect(
|
115
|
+
it { Create.(params: {}, document: json,
|
116
|
+
"representer.default.class" => MyRepresenter).inspect(:model).must_equal %{<Result:true [#<struct DocsRepresenterDITest::Song id=1, title=nil>] >} }
|
117
117
|
end
|
118
118
|
|
119
119
|
#---
|
@@ -141,7 +141,7 @@ class DocsRepresenterInlineTest < Minitest::Spec
|
|
141
141
|
#:inline end
|
142
142
|
|
143
143
|
let (:json) { MultiJson.dump(id: 1) }
|
144
|
-
it { Create.({},
|
144
|
+
it { Create.(params: {}, document: json).inspect(:model).must_equal %{<Result:true [#<struct DocsRepresenterInlineTest::Song id=1, title=nil>] >} }
|
145
145
|
end
|
146
146
|
|
147
147
|
#---
|
@@ -163,8 +163,8 @@ class DocsRepresenterManualRenderTest < Minitest::Spec
|
|
163
163
|
end
|
164
164
|
|
165
165
|
it do
|
166
|
-
result =Show.({ id: 1 })
|
167
|
-
json = result["representer.default.class"].new(result[
|
166
|
+
result =Show.(params: { id: 1 })
|
167
|
+
json = result["representer.default.class"].new(result[:model]).to_json
|
168
168
|
json.must_equal %{{"id":1}}
|
169
169
|
end
|
170
170
|
end
|
@@ -236,19 +236,19 @@ class DocsRepresenterFullExampleTest < Minitest::Spec
|
|
236
236
|
#:full end
|
237
237
|
|
238
238
|
it do
|
239
|
-
result =Create.({},
|
239
|
+
result =Create.(params: {}, document: '{"title": "Tested"}')
|
240
240
|
|
241
|
-
json = result["representer.render.class"].new(result[
|
241
|
+
json = result["representer.render.class"].new(result[:model]).to_json
|
242
242
|
|
243
243
|
json.must_equal %{{"id":1,"title":"Tested","_links":{"self":{"href":"/songs/1"}}}}
|
244
244
|
|
245
245
|
|
246
246
|
#:full-call
|
247
247
|
def create
|
248
|
-
result = Create.(params,
|
248
|
+
result = Create.(params, document: request.body.read)
|
249
249
|
|
250
250
|
if result.success?
|
251
|
-
result["representer.render.class"].new(result[
|
251
|
+
result["representer.render.class"].new(result[:model]).to_json
|
252
252
|
else
|
253
253
|
result["representer.errors.class"].new(result["result.contract.default"]).to_json
|
254
254
|
end
|
@@ -257,7 +257,7 @@ class DocsRepresenterFullExampleTest < Minitest::Spec
|
|
257
257
|
end
|
258
258
|
|
259
259
|
it do
|
260
|
-
result =Create.({},
|
260
|
+
result =Create.(params: {}, document: '{"title": ""}')
|
261
261
|
|
262
262
|
if result.failure?
|
263
263
|
json = result["representer.errors.class"].new(result["result.contract.default"]).to_json
|
data/test/docs/rescue_test.rb
CHANGED
@@ -8,33 +8,33 @@ class NestedRescueTest < Minitest::Spec
|
|
8
8
|
|
9
9
|
class NestedInsanity < Trailblazer::Operation
|
10
10
|
step Rescue {
|
11
|
-
step ->(options) { options["a"] = true }
|
11
|
+
step ->(options, **) { options["a"] = true }
|
12
12
|
step Rescue {
|
13
|
-
step ->(options) { options["y"] = true }
|
14
|
-
success ->(options) { raise Y if options["raise-y"] }
|
15
|
-
step ->(options) { options["z"] = true }
|
13
|
+
step ->(options, **) { options["y"] = true }
|
14
|
+
success ->(options, **) { raise Y if options["raise-y"] }
|
15
|
+
step ->(options, **) { options["z"] = true }
|
16
16
|
}
|
17
|
-
step ->(options) { options["b"] = true }
|
18
|
-
success ->(options) { raise A if options["raise-a"] }
|
19
|
-
step ->(options) { options["c"] = true }
|
20
|
-
failure ->(options) { options["inner-err"] = true }
|
17
|
+
step ->(options, **) { options["b"] = true }
|
18
|
+
success ->(options, **) { raise A if options["raise-a"] }
|
19
|
+
step ->(options, **) { options["c"] = true }
|
20
|
+
failure ->(options, **) { options["inner-err"] = true }
|
21
21
|
}
|
22
|
-
step ->(options) { options["e"] = true }
|
23
|
-
failure ->(options) { options["outer-err"] = true }
|
22
|
+
step ->(options, **) { options["e"] = true }, name: "nested/e"
|
23
|
+
failure ->(options, **) { options["outer-err"] = true }, name: "nested/failure"
|
24
24
|
end
|
25
25
|
|
26
|
-
it { NestedInsanity
|
27
|
-
it { NestedInsanity.(
|
28
|
-
it { NestedInsanity.(
|
29
|
-
it { NestedInsanity.(
|
26
|
+
it { Trailblazer::Operation::Inspect.(NestedInsanity).must_match /\[>Rescue\(\d+\),>nested/ } # FIXME: better introspect tests for all id-generating macros.
|
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
|
+
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
|
+
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] >} }
|
30
30
|
|
31
31
|
#-
|
32
32
|
# inheritance
|
33
33
|
class UbernestedInsanity < NestedInsanity
|
34
34
|
end
|
35
35
|
|
36
|
-
it { UbernestedInsanity.(
|
37
|
-
it { UbernestedInsanity.(
|
36
|
+
it { UbernestedInsanity.().inspect("a", "y", "z", "b", "c", "e", "inner-err", "outer-err").must_equal %{<Result:true [true, true, true, true, true, true, nil, nil] >} }
|
37
|
+
it { UbernestedInsanity.( "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] >} }
|
38
38
|
end
|
39
39
|
|
40
40
|
class RescueTest < Minitest::Spec
|
@@ -66,8 +66,8 @@ class RescueTest < Minitest::Spec
|
|
66
66
|
end
|
67
67
|
#:simple end
|
68
68
|
|
69
|
-
it { Create.( id: 1, title: "Prodigal Son" )["contract.default"].model.inspect.must_equal %{#<struct RescueTest::Song id=1, title="Prodigal Son">} }
|
70
|
-
it { Create.( id: nil ).inspect(
|
69
|
+
it { Create.( params: {id: 1, title: "Prodigal Son"} )["contract.default"].model.inspect.must_equal %{#<struct RescueTest::Song id=1, title="Prodigal Son">} }
|
70
|
+
it { Create.( params: {id: nil} ).inspect(:model).must_equal %{<Result:false [nil] >} }
|
71
71
|
|
72
72
|
#-
|
73
73
|
# Rescue ExceptionClass, handler: ->(*) { }
|
@@ -91,10 +91,10 @@ class RescueTest < Minitest::Spec
|
|
91
91
|
end
|
92
92
|
#:name end
|
93
93
|
|
94
|
-
it { Create.( id: 1, title: "Prodigal Son" )["contract.default"].model.inspect.must_equal %{#<struct RescueTest::Song id=1, title="Prodigal Son">} }
|
95
|
-
it { Create.( id: 1, title: "Prodigal Son" ).inspect("x").must_equal %{<Result:true [nil] >} }
|
96
|
-
it { Create.( id: nil ).inspect(
|
97
|
-
it { assert_raises(RuntimeError) { Create.( id: "RuntimeError!" ) } }
|
94
|
+
it { Create.( params: {id: 1, title: "Prodigal Son"} )["contract.default"].model.inspect.must_equal %{#<struct RescueTest::Song id=1, title="Prodigal Son">} }
|
95
|
+
it { Create.( params: {id: 1, title: "Prodigal Son"} ).inspect("x").must_equal %{<Result:true [nil] >} }
|
96
|
+
it { Create.( params: {id: nil} ).inspect(:model, "x").must_equal %{<Result:false [nil, RescueTest::RecordNotFound] >} }
|
97
|
+
it { assert_raises(RuntimeError) { Create.( params: {id: "RuntimeError!"} ) } }
|
98
98
|
end
|
99
99
|
|
100
100
|
|
@@ -120,7 +120,7 @@ class RescueTest < Minitest::Spec
|
|
120
120
|
step Rescue( RecordNotFound, handler: :rollback! ) {
|
121
121
|
step Wrap ->(*, &block) { Sequel.transaction do block.call end } {
|
122
122
|
step Model( Song, :find )
|
123
|
-
step ->(options) { options[
|
123
|
+
step ->(options, *) { options[:model].lock! } # lock the model.
|
124
124
|
step Contract::Build( constant: MyContract )
|
125
125
|
step Contract::Validate( )
|
126
126
|
step Contract::Persist( method: :sync )
|
@@ -134,7 +134,7 @@ class RescueTest < Minitest::Spec
|
|
134
134
|
#~ex end
|
135
135
|
end
|
136
136
|
|
137
|
-
def error!(options)
|
137
|
+
def error!(options, *)
|
138
138
|
#~ex
|
139
139
|
options["err"] = true
|
140
140
|
#~ex end
|
@@ -142,13 +142,13 @@ class RescueTest < Minitest::Spec
|
|
142
142
|
end
|
143
143
|
#:example end
|
144
144
|
|
145
|
-
it { Create.( id: 1, title: "Pie" ).inspect(
|
145
|
+
it { Create.( params: {id: 1, title: "Pie"} ).inspect(:model, "x", "err").must_equal %{<Result:true [#<struct RescueTest::Song id=1, title=\"Pie\">, nil, nil] >} }
|
146
146
|
# raise exceptions in Model:
|
147
|
-
it { Create.( id: nil ).inspect(
|
148
|
-
it { assert_raises(RuntimeError) { Create.( id: "RuntimeError!" ) } }
|
147
|
+
it { Create.( params: {id: nil} ).inspect(:model, "x").must_equal %{<Result:false [nil, RescueTest::RecordNotFound] >} }
|
148
|
+
it { assert_raises(RuntimeError) { Create.( params: {id: "RuntimeError!"} ) } }
|
149
149
|
it do
|
150
|
-
Create.( id: 1, title: "Pie" )
|
151
|
-
Sequel.result.first.
|
150
|
+
Create.( params: {id: 1, title: "Pie"} )
|
151
|
+
Sequel.result.first.must_be_kind_of Trailblazer::Operation::Railway::End::Success
|
152
152
|
end
|
153
153
|
end
|
154
154
|
end
|