77# 4. show how task can be replaced at runtime, e.g. for Nested
88# 5. how to call with kwargs, e.g. in Rescue?
99# 6. "scopes" for tracing? E.g. "only trace business steps"
10+ # 7. try saving memory by providing often-used Pipes, e.g. for IO?
1011
1112class RunnerInvokerTest < Minitest ::Spec
1213it do
@@ -135,7 +136,7 @@ def self.call(ctx, value:, **)
135136
136137 class Model___Input
137138 def self . call ( ctx , **)
138- ctx = Trailblazer :: Context ( ctx )
139+ ctx = Trailblazer . Context ( ctx )
139140
140141 return ctx , nil
141142 end
@@ -149,13 +150,20 @@ def self.call(ctx, signal:, **)
149150 end
150151
151152 class Create
152- def model ( ctx , params :, **)
153- ctx [ :model ] = "Object #{ params [ :id ] } "
153+ def model ( ctx , params :, **kws )
154+ ctx [ :spam ] = false
155+ ctx [ :model ] = "Object #{ params [ :id ] } / #{ kws . inspect } "
154156 end
155157
156158 def my_model_input ( ctx , params :, **)
157159 {
158- params : { id : params [ :id ] . inspect }
160+ params : { id : params [ :slug ] }
161+ }
162+ end
163+
164+ def my_model_output ( ctx , model :, **)
165+ {
166+ model : model
159167 }
160168 end
161169 end
@@ -169,15 +177,37 @@ def init_aggregate(ctx, **)
169177
170178 def add_value_to_aggregate ( ctx , aggregate :, value :, **)
171179 ctx [ :aggregate ] = aggregate . merge ( value )
180+
181+ return ctx , nil
182+ end
183+
184+ def save_original_application_ctx ( ctx , application_ctx :, **)
185+ ctx [ :original_application_ctx ] = application_ctx # the "outer ctx".
186+
187+ return ctx , nil
172188 end
173189
174190 def unscope___ ( ctx , application_ctx :, aggregate :, **)
175191 original , _ = ctx . decompose
176192
177- ctx = original . merge ( application_ctx : aggregate )
193+ ctx = original . merge ( application_ctx : Trailblazer :: Context ( aggregate ) ) # FIXME: separate step.
178194
179195 return ctx , nil
180196 end
197+
198+ # def create_input_ctx(ctx, aggregate:, **)
199+
200+ # end
201+
202+ def swap___ ( ctx , application_ctx :, original_application_ctx :, aggregate :, signal :, **)
203+ new_application_ctx = original_application_ctx . merge ( aggregate ) # DISCUSS: how to write on outer ctx?
204+
205+ original , _ = ctx . decompose
206+
207+ ctx = original . merge ( application_ctx : new_application_ctx )
208+
209+ return ctx , signal # FIXME: thiiiiiiiiiiiiiiiiiis neeeeds to be the last tw step.
210+ end
181211 end
182212 Io = IO___ . new
183213
@@ -198,30 +228,41 @@ def self.call(ctx, params:, **)
198228 end
199229 end
200230 more_model_input_pipe = pipeline_circuit (
201- # [:input, Model___Input], # DISCUSS: can we somehow save these steps?
202231 [ :invoke_callable , MoreModelInput , INVOKER___STEP_INTERFACE ] ,
203- # [:compute_binary_signal, ComputeBinarySignal],
204232 [ :add_value_to_aggregate , :add_value_to_aggregate , INVOKER___STEP_INTERFACE_ON_EXEC_CONTEXT , { exec_context : Io , use_application_ctx___ : false } ] ,
205- # [:output, Model___Output],
233+ )
234+
235+ my_model_output_pipe = pipeline_circuit (
236+ [ :invoke_instance_method , :my_model_output , INVOKER___STEP_INTERFACE_ON_EXEC_CONTEXT , { exec_context : Create . new } ] ,
237+ [ :add_value_to_aggregate , :add_value_to_aggregate , INVOKER___STEP_INTERFACE_ON_EXEC_CONTEXT , { exec_context : Io , use_application_ctx___ : false } ] ,
206238 )
207239
208240 model_input_pipe = pipeline_circuit (
209- [ :scope , Model___Input ] , # scope
241+ [ :save_original_application_ctx , :save_original_application_ctx , INVOKER___CIRCUIT_INTERFACE_ON_EXEC_CONTEXT , { exec_context : Io } ] ,
242+ [ :scope , Model___Input ] , # scope, so we don't pollute anything.
210243 [ :init_aggregate , :init_aggregate , INVOKER___CIRCUIT_INTERFACE_ON_EXEC_CONTEXT , { exec_context : Io } ] ,
211- [ :my_model_input , my_model_input_pipe , Circuit ::Processor ] ,
212- [ :more_model_input , more_model_input_pipe , Circuit ::Processor ] ,
244+ [ :my_model_input , my_model_input_pipe , Circuit ::Processor ] , # user filter.
245+ [ :more_model_input , more_model_input_pipe , Circuit ::Processor ] , # user filter.
213246 [ :unscope , :unscope___ , INVOKER___CIRCUIT_INTERFACE_ON_EXEC_CONTEXT , { exec_context : Io } ]
214247 )
215248
216- ctx , signal = Circuit ::Processor . ( model_input_pipe , {
217- application_ctx : { params : { id : 999 } } ,
218- exec_context : create_instance = Create . new ,
219- } )
249+ model_output_pipe = pipeline_circuit (
250+ [ :scope , Model___Input ] , # scope so we don't pollute
251+ [ :init_aggregate , :init_aggregate , INVOKER___CIRCUIT_INTERFACE_ON_EXEC_CONTEXT , { exec_context : Io } ] ,
252+ [ :my_model_output , my_model_output_pipe , Circuit ::Processor ] , # user filter.
253+ [ :swap___ , :swap___ , INVOKER___CIRCUIT_INTERFACE_ON_EXEC_CONTEXT , { exec_context : Io } ] ,
254+ )
220255
221- # ctx, signal = Circuit::Processor.(more_model_input_pipe, ctx)
256+ # ctx, signal = Circuit::Processor.(model_input_pipe, {
257+ # application_ctx: {params: {slug: 999}, noise: true},
258+ # exec_context: create_instance = Create.new,
259+ # })
222260
223- assert_equal ctx . inspect , %({:application_ctx=>{:params=>{:id=>"999"}, :more=>"{:id=>999}"}, :exec_context=>#{ create_instance } })
224- pp ctx
261+ # application_ctx = ctx[:application_ctx].merge(model: Object)
262+
263+ # ctx, signal = Circuit::Processor.(model_output_pipe, ctx.merge(application_ctx: application_ctx))
264+
265+ # assert_equal ctx.inspect, %({:application_ctx=>{:params=>{:id=>"999"}, :more=>"{:id=>999}"}, :exec_context=>#{create_instance}, noise})
225266
226267
227268
@@ -251,10 +292,12 @@ def self.call(ctx, model:, **)
251292 end
252293
253294 model_pipe = pipeline_circuit (
254- [ :input , Model___Input ] , # DISCUSS: can we somehow save these steps?
255- [ :invoke_instance_method , :model , INVOKER___STEP_INTERFACE_ON_EXEC_CONTEXT ] ,
295+ # [:input, Model___Input], # DISCUSS: can we somehow save these steps?
296+ [ :input , model_input_pipe , Circuit ::Processor ] ,
297+ [ :invoke_instance_method , :model , INVOKER___STEP_INTERFACE_ON_EXEC_CONTEXT , { exec_context : Create . new } ] ,
256298 [ :compute_binary_signal , ComputeBinarySignal ] ,
257- [ :output , Model___Output ] , # DISCUSS: can we somehow save these steps?
299+ # [:output, Model___Output], # DISCUSS: can we somehow save these steps?
300+ [ :output , model_output_pipe , Circuit ::Processor ] ,
258301 )
259302 # pp model_pipe
260303
@@ -293,9 +336,9 @@ def self.call(ctx, model:, **)
293336 )
294337
295338 # create_pipe = [
296- model = [ :model , model_pipe , Circuit ::Processor , { exec_context : Create . new . freeze } , ] # TODO: circuit_options should be set outside of Create, in the canonical invoke.
297- validate = [ :validate , validate_circuit , Circuit ::Processor , { exec_context : Validate . new . freeze } , ]
298- save = [ :save , save_pipe , Circuit ::Processor , { } ] # check that we don't have circuit_options anymore here?
339+ model = [ :Model , model_pipe , Circuit ::Processor , { exec_context : Create . new . freeze } , ] # TODO: circuit_options should be set outside of Create, in the canonical invoke.
340+ validate = [ :Validate , validate_circuit , Circuit ::Processor , { exec_context : Validate . new . freeze } , ]
341+ save = [ :Save , save_pipe , Circuit ::Processor , { } ] # check that we don't have circuit_options anymore here?
299342 # ]
300343
301344 create_success_terminus = [ :create_success_terminus , CREATE_FIXME_SUCCESS = Circuit ::Terminus ::Success . new ( semantic : :success ) , INVOKER___CIRCUIT_INTERFACE , { } ]
@@ -317,6 +360,7 @@ def self.call(ctx, model:, **)
317360 application_ctx : ctx
318361 }
319362
363+ puts "ciiii"
320364 # validation error:
321365 ctx , signal = Circuit ::Processor . ( create_circuit , create_ctx )
322366
0 commit comments