Sha256: 23f8bc2ed60dec2bcba693b5a9f87005402d486821f38a67d37c40f2c1a73722

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

module DirtyPipeline
  class Transition
    def Abort()
      throw :fail_operation, :abort
    end

    def Error(error)
      throw :fail_operation, error
    end

    def Success(changes = nil)
      throw :success, changes.to_h
    end

    def self.finalize(*args, **kwargs)
      event, pipeline, *args = args
      instance = new(event, *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)
      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)
    end

    attr_reader :event
    def initialize(event, *, **)
      @event = event
    end

    def fetch(key)
      event.cache.fetch(key) { event.cache[key] = yield }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dirty_pipeline-0.6.0 lib/dirty_pipeline/transition.rb