lib/dirty_pipeline/transition.rb in dirty_pipeline-0.4.0 vs lib/dirty_pipeline/transition.rb in dirty_pipeline-0.5.0

- old
+ new

@@ -6,34 +6,43 @@ def Error(error) throw :fail_with_error, error end - def Success(output = nil, after_commit: nil, &block) - result = [output.to_h] - after_commit = Array(after_commit) << block if block_given? - result += Array(after_commit) if after_commit - throw :success, result + def Success(changes = nil) + throw :success, changes.to_h end + def self.finalize(*args, **kwargs) + event, pipeline, *args = args + instance = new(event, *args, **kwargs) + pipeline.railway.switch_to(:call) + return unless instance.respond_to?(:finalize) + instance.finalize(pipeline.subject) + end + def self.undo(*args, **kwargs) - pipeline = args.shift - instance = new(pipeline, *args, **kwargs) + event, pipeline, *args = args + instance = new(event, *args, **kwargs) return unless instance.respond_to?(:undo) instance.undo(pipeline.subject) end def self.call(*args, **kwargs) - pipeline = args.shift - new(pipeline, *args, **kwargs).call(pipeline.subject) + event, pipeline, *args = args + instance = new(event, *args, **kwargs) + pipeline.railway[:undo] << event if instance.respond_to?(:undo) + pipeline.railway[:finalize] << event if instance.respond_to?(:finalize) + pipeline.railway.switch_to(:finalize) if instance.respond_to?(:finalize) + new(event, *args, **kwargs).call(pipeline.subject) end - attr_reader :pipeline - def initialize(pipeline, *, **) - @pipeline = pipeline + attr_reader :event + def initialize(event, *, **) + @event = event end def fetch(key) - pipeline.cache.fetch(key) { pipeline.cache[key] = yield } + event.cache.fetch(key) { event.cache[key] = yield } end end end