@@ -140,35 +140,45 @@ module Flows
140
140
# # steps implementations here
141
141
# end
142
142
#
143
- # ## Callbacks
143
+ # ## Callbacks and metadata
144
144
#
145
145
# You may want to have some logic to execute before all steps, or after all, or before each, or after each.
146
146
# For example to inject generalized execution process logging.
147
147
# To achieve this you can use callbacks:
148
148
#
149
149
# class MySCP < Flows::SharedContextPipeline
150
- # before_all do |klass, context |
151
- # # you can modify execution context here
150
+ # before_all do |klass, data, meta |
151
+ # # you can modify execution data context and metadata here
152
152
# # return value will be ignored
153
153
# end
154
154
#
155
- # after_all do |klass, pipeline_result|
156
- # # you must provide final result object for pipeline here
155
+ # after_all do |klass, pipeline_result, data, meta|
156
+ # # you can modify execution data context and metadata here
157
+ # # you must return a final result object here
157
158
# # if no modifications needed - just return provided pipeline_result
158
159
# end
159
160
#
160
- # before_each do |klass, step_name, context |
161
- # # you can modify context here
161
+ # before_each do |klass, step_name, data, meta |
162
+ # # you can modify execution data context and metadata here
162
163
# # return value will be ignored
163
164
# end
164
165
#
165
- # after_each do |klass, step_name, context, step_result|
166
- # # you can modify context here
167
- # # you must not modify step_result
168
- # # context already has data from step_result at the moment of execution
166
+ # after_each do |klass, step_name, step_result, data, meta|
167
+ # # you can modify execution data context and metadata here
169
168
# # return value will be ignored
169
+ # #
170
+ # # callback executed after context is updated with result data
171
+ # # (in the case of normal steps, mutation steps update context directly)
172
+ # #
173
+ # # DO NOT MODIFY RESULT OBJECT HERE - IT CAN BROKE MUTATION STEPS
170
174
# end
171
175
# end
176
+ #
177
+ # Metadata - is a Hash which is shared between step executions.
178
+ # This hash becomes metadata of a final {Flows::Result}.
179
+ #
180
+ # Metadata is designed to store non-business data such as execution times,
181
+ # some library specific data, and so on.
172
182
class SharedContextPipeline
173
183
extend ::Flows ::Plugin ::ImplicitInit
174
184
@@ -192,24 +202,25 @@ def initialize
192
202
# Executes pipeline with provided keyword arguments, returns Result Object.
193
203
#
194
204
# @return [Flows::Result]
195
- def call ( **kwargs ) # rubocop:disable Metrics/MethodLength
205
+ def call ( **data ) # rubocop:disable Metrics/MethodLength
196
206
klass = self . class
197
- context = { data : kwargs , class : klass }
207
+ meta = { }
208
+ context = { data : data , meta : meta , class : klass }
198
209
199
210
klass . before_all_callbacks . each do |callback |
200
- callback . call ( klass , context [ : data] )
211
+ callback . call ( klass , data , meta )
201
212
end
202
213
203
214
flow_result = @__flow . call ( nil , context : context )
204
215
205
216
final_result = flow_result . class . new (
206
- context [ : data] ,
217
+ data ,
207
218
status : flow_result . status ,
208
- meta : { last_step : context [ :last_step ] }
219
+ meta : meta
209
220
)
210
221
211
222
klass . after_all_callbacks . reduce ( final_result ) do |result , callback |
212
- callback . call ( klass , result )
223
+ callback . call ( klass , result , data , meta )
213
224
end
214
225
end
215
226
end
0 commit comments