Sha256: 45adc4ac3a33df5fb95e4cb49276a00b25112086c8e1400b86b1ccba45f40ac6

Contents?: true

Size: 1.36 KB

Versions: 5

Compression:

Stored size: 1.36 KB

Contents

module DirtyPipeline
  class Transition
    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, pipeline.railway, *args, **kwargs)
      return unless instance.respond_to?(:finalize)
      instance.finalize(pipeline.subject)
    end

    def self.undo(*args, **kwargs)
      event, pipeline, *args = args
      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, pipeline.railway, *args, **kwargs)
      pipeline&.railway&.send(:[], :undo)&.send(:<<, event)
      pipeline&.railway&.send(:[], :finalize)&.send(:<<, event)
      instance.call(pipeline.subject)
    end

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

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dirty_pipeline-0.8.3 lib/dirty_pipeline/transition.rb
dirty_pipeline-0.8.2 lib/dirty_pipeline/transition.rb
dirty_pipeline-0.8.1 lib/dirty_pipeline/transition.rb
dirty_pipeline-0.7.1 lib/dirty_pipeline/transition.rb
dirty_pipeline-0.7.0 lib/dirty_pipeline/transition.rb