trailblazer-macro 2.1.11 → 2.1.13
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +6 -4
- data/.github/workflows/ci_jruby.yml +19 -0
- data/.github/workflows/ci_legacy.yml +19 -0
- data/.github/workflows/ci_truffleruby.yml +19 -0
- data/CHANGES.md +33 -0
- data/Gemfile +7 -5
- data/lib/trailblazer/macro/each.rb +187 -0
- data/lib/trailblazer/macro/model.rb +4 -5
- data/lib/trailblazer/macro/nested.rb +130 -68
- data/lib/trailblazer/macro/rescue.rb +2 -0
- data/lib/trailblazer/macro/strategy.rb +32 -0
- data/lib/trailblazer/macro/version.rb +1 -1
- data/lib/trailblazer/macro/wrap.rb +72 -65
- data/lib/trailblazer/macro.rb +53 -2
- data/test/docs/autogenerated/operation_each_test.rb +585 -0
- data/test/docs/autogenerated/operation_model_test.rb +263 -0
- data/test/docs/each_test.rb +940 -0
- data/test/docs/macro_test.rb +18 -0
- data/test/docs/model_test.rb +204 -88
- data/test/docs/nested_static_test.rb +730 -0
- data/test/docs/wrap_test.rb +348 -66
- data/test/test_helper.rb +29 -0
- data/trailblazer-macro.gemspec +2 -6
- metadata +21 -58
- data/.rubocop.yml +0 -6
- data/.rubocop_todo.yml +0 -423
- data/test/docs/nested_test.rb +0 -218
- data/test/operation/model_test.rb +0 -89
- data/test/operation/nested_test.rb +0 -98
data/test/docs/macro_test.rb
CHANGED
@@ -31,6 +31,24 @@ class DocsMacroTest < Minitest::Spec
|
|
31
31
|
it { Trailblazer::Developer.railway(Create).must_equal %{[>my_policy.manager]} }
|
32
32
|
end
|
33
33
|
|
34
|
+
|
35
|
+
class MacroAssignVariableTest < Minitest::Spec
|
36
|
+
it do
|
37
|
+
my_exec_context = Class.new do
|
38
|
+
def my_dataset(ctx, my_array:, **)
|
39
|
+
my_array.reverse
|
40
|
+
end
|
41
|
+
end.new
|
42
|
+
|
43
|
+
dataset_task = Trailblazer::Macro.task_adapter_for_decider(:my_dataset, variable_name: :dataset)
|
44
|
+
|
45
|
+
signal, (ctx, _) = dataset_task.([{my_array: [1,2]}, {}], exec_context: my_exec_context)
|
46
|
+
|
47
|
+
assert_equal signal, Trailblazer::Activity::Right
|
48
|
+
assert_equal ctx.inspect, %{{:my_array=>[1, 2], :dataset=>[2, 1]}}
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
34
52
|
# injectable option
|
35
53
|
# nested pipe
|
36
54
|
# using macros in macros
|
data/test/docs/model_test.rb
CHANGED
@@ -15,122 +15,149 @@ class DocsModelTest < Minitest::Spec
|
|
15
15
|
end
|
16
16
|
|
17
17
|
#:op
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
module Song::Activity
|
19
|
+
class Create < Trailblazer::Activity::Railway
|
20
|
+
step Model(Song, :new)
|
21
|
+
step :validate
|
22
|
+
step :save
|
23
|
+
#~meths
|
24
|
+
include T.def_steps(:validate, :save)
|
25
|
+
#~meths end
|
26
|
+
end
|
21
27
|
end
|
22
28
|
#:op end
|
23
29
|
|
30
|
+
#:update
|
31
|
+
module Song::Activity
|
32
|
+
class Update < Trailblazer::Activity::Railway
|
33
|
+
step Model(Song, :find_by)
|
34
|
+
step :validate
|
35
|
+
step :save
|
36
|
+
#~meths
|
37
|
+
include T.def_steps(:validate, :save)
|
38
|
+
#~meths end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
#:update end
|
24
42
|
|
25
43
|
it "defaults {:params} to empty hash when not passed" do
|
26
|
-
|
27
|
-
|
28
|
-
assert_equal %{#<struct DocsModelTest::Song id=nil, title=nil>}, result[:model].inspect
|
44
|
+
assert_invoke Song::Activity::Create, seq: "[:validate, :save]",
|
45
|
+
expected_ctx_variables: {model: Song.new}
|
29
46
|
|
30
|
-
|
31
|
-
|
32
|
-
assert_equal "nil", result[:model].inspect
|
47
|
+
assert_invoke Song::Activity::Update, seq: "[]",
|
48
|
+
terminus: :failure
|
33
49
|
end
|
34
50
|
|
51
|
+
#~ctx_to_result
|
35
52
|
it do
|
36
53
|
#:create
|
37
|
-
|
38
|
-
|
54
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Create, params: {}, seq: [])
|
55
|
+
puts ctx[:model] #=> #<struct Song id=nil, title=nil>
|
39
56
|
#:create end
|
40
57
|
|
41
|
-
|
58
|
+
assert_invoke Song::Activity::Create, params: {},
|
59
|
+
seq: "[:validate, :save]", expected_ctx_variables: {model: Song.new}
|
42
60
|
end
|
43
61
|
|
62
|
+
it do
|
63
|
+
#:update-ok
|
64
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {id: 1}, seq: [])
|
65
|
+
ctx[:model] #=> #<Song id=1, ...>
|
66
|
+
puts signal #=> #<Trailblazer::Activity::End semantic=:success>
|
67
|
+
#:update-ok end
|
44
68
|
|
45
|
-
|
46
|
-
|
47
|
-
step Model( Song, :find_by )
|
48
|
-
# ..
|
69
|
+
assert_equal ctx[:model].inspect, %{#<struct #{Song} id=1, title=nil>}
|
70
|
+
assert_equal signal.to_h[:semantic], :success
|
49
71
|
end
|
50
|
-
#:update end
|
51
72
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
73
|
+
it do
|
74
|
+
#:update-fail
|
75
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {})
|
76
|
+
ctx[:model] #=> nil
|
77
|
+
puts signal #=> #<Trailblazer::Activity::End semantic=:failure>
|
78
|
+
#:update-fail end
|
58
79
|
|
59
|
-
|
60
|
-
|
61
|
-
step Model( Song, :find_by, not_found_terminus: true )
|
62
|
-
# ..
|
80
|
+
assert_equal ctx[:model].inspect, %{nil}
|
81
|
+
assert_equal signal.to_h[:semantic], :failure
|
63
82
|
end
|
64
|
-
|
83
|
+
#~ctx_to_result end
|
84
|
+
end
|
65
85
|
|
66
|
-
|
67
|
-
|
68
|
-
result = Update.(params: { id: 1 })
|
69
|
-
result[:model] #=> #<struct Song id=1, title="nil">
|
70
|
-
#:update-ok end
|
86
|
+
class DocsModelFindByTitleTest < Minitest::Spec
|
87
|
+
Song = Class.new(DocsModelTest::Song)
|
71
88
|
|
72
|
-
|
89
|
+
#:update-with-find-by-key
|
90
|
+
module Song::Activity
|
91
|
+
class Update < Trailblazer::Activity::Railway
|
92
|
+
step Model(Song, :find_by, :title) # third positional argument.
|
93
|
+
step :validate
|
94
|
+
step :save
|
95
|
+
#~meths
|
96
|
+
include T.def_steps(:validate, :save)
|
97
|
+
#~meths end
|
98
|
+
end
|
73
99
|
end
|
100
|
+
#:update-with-find-by-key end
|
74
101
|
|
102
|
+
#~ctx_to_result
|
75
103
|
it do
|
76
104
|
#:update-with-find-by-key-ok
|
77
|
-
|
78
|
-
|
105
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {title: "Test"}, seq: [])
|
106
|
+
ctx[:model] #=> #<struct Song id=2, title="Test">
|
79
107
|
#:update-with-find-by-key-ok end
|
80
108
|
|
81
|
-
|
109
|
+
assert_equal ctx[:model].inspect, %{#<struct #{Song} id=2, title="Test">}
|
82
110
|
end
|
83
111
|
|
84
112
|
it do
|
85
|
-
#:
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
#:update-fail end
|
90
|
-
result[:model].must_be_nil
|
91
|
-
result.success?.must_equal false
|
92
|
-
end
|
93
|
-
|
94
|
-
it do
|
95
|
-
#:update-with-find-by-key-fail
|
96
|
-
result = UpdateWithFindByKey.(params: {title: nil})
|
97
|
-
result[:model] #=> nil
|
98
|
-
result.success? #=> false
|
99
|
-
#:update-with-find-by-key-fail end
|
100
|
-
result[:model].must_be_nil
|
101
|
-
result.success?.must_equal false
|
113
|
+
#:key-title-fail
|
114
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {title: nil}, seq: [])
|
115
|
+
assert_equal ctx[:model].inspect, %{nil}
|
116
|
+
#:key-title-fail end
|
102
117
|
end
|
118
|
+
#~ctx_to_result end
|
119
|
+
end
|
103
120
|
|
104
|
-
|
105
|
-
|
106
|
-
result = UpdateFailureWithModelNotFound.(params: {title: nil})
|
107
|
-
result[:model] #=> nil
|
108
|
-
result.success? #=> false
|
109
|
-
result.event #=> #<Trailblazer::Activity::End semantic=:not_found>
|
110
|
-
#:update-with-not-found-end-use end
|
111
|
-
|
112
|
-
result[:model].must_be_nil
|
113
|
-
result.success?.must_equal false
|
114
|
-
result.event.inspect.must_equal %{#<Trailblazer::Activity::End semantic=:not_found>}
|
115
|
-
end
|
121
|
+
class DocsModelAccessorTest < Minitest::Spec
|
122
|
+
Song = Class.new(DocsModelTest::Song)
|
116
123
|
|
117
124
|
#:show
|
118
|
-
|
119
|
-
|
120
|
-
|
125
|
+
module Song::Activity
|
126
|
+
class Update < Trailblazer::Activity::Railway
|
127
|
+
step Model(Song, :[])
|
128
|
+
step :validate
|
129
|
+
step :save
|
130
|
+
#~meths
|
131
|
+
include T.def_steps(:validate, :save)
|
132
|
+
#~meths end
|
133
|
+
end
|
121
134
|
end
|
122
135
|
#:show end
|
123
136
|
|
137
|
+
#~ctx_to_result
|
124
138
|
it do
|
125
|
-
result = Show.(params: { id: 1 })
|
126
|
-
|
127
139
|
#:show-ok
|
128
|
-
|
129
|
-
|
140
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {id: 1}, seq: [])
|
141
|
+
ctx[:model] #=> #<struct Song id=1, title="Roxanne">
|
130
142
|
#:show-ok end
|
131
143
|
|
132
|
-
|
133
|
-
|
144
|
+
assert_equal ctx[:model].inspect, %{#<struct #{Song} id=100, title=nil>}
|
145
|
+
end
|
146
|
+
#~ctx_to_result end
|
147
|
+
end
|
148
|
+
|
149
|
+
class DocsModelDependencyInjectionTest < Minitest::Spec
|
150
|
+
Song = Class.new(DocsModelTest::Song)
|
151
|
+
|
152
|
+
module Song::Activity
|
153
|
+
class Create < Trailblazer::Activity::Railway
|
154
|
+
step Model(Song, :new)
|
155
|
+
step :validate
|
156
|
+
step :save
|
157
|
+
#~meths
|
158
|
+
include T.def_steps(:validate, :save)
|
159
|
+
#~meths end
|
160
|
+
end
|
134
161
|
end
|
135
162
|
|
136
163
|
it "allows injecting {:model.class} and friends" do
|
@@ -138,37 +165,126 @@ class DocsModelTest < Minitest::Spec
|
|
138
165
|
end
|
139
166
|
|
140
167
|
#:di-model-class
|
141
|
-
|
168
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Create, params: {}, :"model.class" => Hit, seq: [])
|
142
169
|
#:di-model-class end
|
143
170
|
|
144
|
-
|
171
|
+
assert_equal ctx[:model].inspect, %{#<struct #{Hit} id=nil, title=nil>}
|
145
172
|
|
146
173
|
# inject all variables
|
147
174
|
#:di-all
|
148
|
-
|
175
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Create,
|
149
176
|
params: {title: "Olympia"}, # some random variable.
|
150
177
|
"model.class": Hit,
|
151
178
|
"model.action": :find_by,
|
152
|
-
"model.find_by_key": :
|
179
|
+
"model.find_by_key": :title, seq: []
|
153
180
|
)
|
154
181
|
#:di-all end
|
155
182
|
|
156
|
-
|
183
|
+
assert_equal ctx[:model].inspect, %{#<struct #{Hit} id=2, title="Olympia">}
|
184
|
+
end
|
157
185
|
|
158
186
|
# use empty Model() and inject {model.class} and {model.action}
|
159
|
-
|
160
|
-
|
187
|
+
class DocsModelEmptyDITest < Minitest::Spec
|
188
|
+
Song = Class.new(DocsModelTest::Song)
|
189
|
+
Hit = Class.new(Song)
|
190
|
+
|
191
|
+
#:op-model-empty
|
192
|
+
module Song::Activity
|
193
|
+
class Create < Trailblazer::Activity::Railway
|
194
|
+
step Model()
|
195
|
+
step :validate
|
196
|
+
step :save
|
197
|
+
#~meths
|
198
|
+
include T.def_steps(:validate, :save)
|
199
|
+
#~meths end
|
200
|
+
end
|
201
|
+
#:op-model-empty end
|
202
|
+
end
|
203
|
+
|
204
|
+
it do
|
205
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Create, params: {}, :"model.class" => Hit, seq: [])
|
206
|
+
assert_equal ctx[:model].inspect, %{#<struct #{Hit} id=nil, title=nil>}
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
class DocsModelIOTest < Minitest::Spec
|
211
|
+
Song = Class.new(DocsModelTest::Song)
|
212
|
+
Hit = Class.new(Song)
|
213
|
+
|
214
|
+
it "allows to use composable I/O with macros" do
|
215
|
+
#:in
|
216
|
+
module Song::Activity
|
161
217
|
class Create < Trailblazer::Operation
|
162
|
-
step Model()
|
163
|
-
|
218
|
+
step Model(Song, :find_by),
|
219
|
+
In() => ->(ctx, my_id:, **) { ctx.merge(params: {id: my_id}) } # Model() needs {params[:id]}.
|
220
|
+
# ...
|
164
221
|
end
|
165
|
-
|
166
|
-
end
|
222
|
+
end
|
223
|
+
#:in end
|
224
|
+
|
225
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Create, params: {}, my_id: 1, :"model.class" => Hit)
|
226
|
+
assert_equal ctx[:model].inspect, %{#<struct #{Hit} id=1, title=nil>}
|
227
|
+
=begin
|
228
|
+
#:in-call
|
229
|
+
result = Create.(my_id: 1)
|
230
|
+
#:in-call end
|
231
|
+
=end
|
232
|
+
end
|
233
|
+
end
|
234
|
+
end
|
167
235
|
|
168
|
-
|
236
|
+
class Model404TerminusTest < Minitest::Spec
|
237
|
+
Song = Class.new(DocsModelTest::Song)
|
238
|
+
#:update-with-not-found-end
|
239
|
+
module Song::Activity
|
240
|
+
class Update < Trailblazer::Activity::Railway
|
241
|
+
step Model(Song, :find_by, not_found_terminus: true)
|
242
|
+
step :validate
|
243
|
+
step :save
|
244
|
+
#~meths
|
245
|
+
include T.def_steps(:validate, :save)
|
246
|
+
#~meths end
|
247
|
+
end
|
248
|
+
end
|
249
|
+
#:update-with-not-found-end end
|
169
250
|
|
170
|
-
|
251
|
+
it do
|
252
|
+
assert_invoke Song::Activity::Update, params: {id: 1},
|
253
|
+
seq: "[:validate, :save]", expected_ctx_variables: {model: Song.find_by(id: 1)}
|
254
|
+
assert_invoke Song::Activity::Update, params: {id: nil}, terminus: :not_found
|
255
|
+
|
256
|
+
#:not_found
|
257
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {id: nil})
|
258
|
+
puts signal #=> #<Trailblazer::Activity::End semantic=:not_found>
|
259
|
+
#:not_found end
|
260
|
+
end
|
261
|
+
end
|
171
262
|
|
263
|
+
class Model404TerminusTest < Minitest::Spec
|
264
|
+
Song = Class.new(DocsModelTest::Song)
|
265
|
+
#:update-with-not-found-end
|
266
|
+
class Song
|
267
|
+
module Activity
|
268
|
+
class Update < Trailblazer::Activity::Railway
|
269
|
+
step Model(Song, :find_by, not_found_terminus: true)
|
270
|
+
step :validate
|
271
|
+
step :save
|
272
|
+
#~meths
|
273
|
+
include T.def_steps(:validate, :save)
|
274
|
+
#~meths end
|
275
|
+
end
|
276
|
+
end
|
277
|
+
end
|
278
|
+
#:update-with-not-found-end end
|
172
279
|
|
280
|
+
it do
|
281
|
+
assert_invoke Song::Activity::Update, params: {id: 1},
|
282
|
+
seq: "[:validate, :save]", expected_ctx_variables: {model: Song.find_by(id: 1)}
|
283
|
+
assert_invoke Song::Activity::Update, params: {id: nil}, terminus: :not_found
|
284
|
+
|
285
|
+
#:not_found
|
286
|
+
signal, (ctx, _) = Trailblazer::Activity::TaskWrap.invoke(Song::Activity::Update, [{params: {id: nil}},{}])
|
287
|
+
puts signal #=> #<Trailblazer::Activity::End semantic=:not_found>
|
288
|
+
#:not_found end
|
173
289
|
end
|
174
290
|
end
|