lib/dirty_pipeline/transition.rb in dirty_pipeline-0.6.2 vs lib/dirty_pipeline/transition.rb in dirty_pipeline-0.6.3
- old
+ new
@@ -1,46 +1,49 @@
module DirtyPipeline
class Transition
- def Error(error)
+ def Failure(error)
+ railway.switch_to(:undo)
throw :fail_transition, error
end
def Success(changes = nil)
+ case railway.active
+ when "call"
+ railway.switch_to(:finalize) if respond_to?(:finalize)
+ when "finalize"
+ railway.switch_to(:call)
+ end
throw :success, changes.to_h
end
-
def self.finalize(*args, **kwargs)
event, pipeline, *args = args
- instance = new(event, *args, **kwargs)
+ instance = new(event, pipeline.railway, *args, **kwargs)
return unless instance.respond_to?(:finalize)
- pipeline.railway.switch_to(:call)
instance.finalize(pipeline.subject)
end
def self.undo(*args, **kwargs)
event, pipeline, *args = args
- instance = new(event, *args, **kwargs)
+ instance = new(event, pipeline.railway, *args, **kwargs)
return unless instance.respond_to?(:undo)
instance.undo(pipeline.subject)
end
def self.call(*args, **kwargs)
event, pipeline, *args = args
- instance = new(event, *args, **kwargs)
- pipeline.railway[:undo] << event if instance.respond_to?(:undo)
- if instance.respond_to?(:finalize)
- pipeline.railway[:finalize] << event
- pipeline.railway.switch_to(:finalize)
- end
- new(event, *args, **kwargs).call(pipeline.subject)
+ instance = new(event, pipeline.railway, *args, **kwargs)
+ pipeline.railway[:undo] << event
+ pipeline.railway[:finalize] << event
+ instance.call(pipeline.subject)
end
- attr_reader :event
- def initialize(event, *, **)
+ attr_reader :event, :railway
+ def initialize(event, railway, *, **)
@event = event
+ @railway = railway
end
- def fetch(key)
+ def cache(key)
event.cache.fetch(key) { event.cache[key] = yield }
end
end
end