Sha256: 2993cd58e72224ceb4456de515297d27a5562f4fcc6922d754b87720f6139292

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

require 'transproc'
require 'transflow/version'

module Transflow
  class Transaction
    attr_reader :handler

    attr_reader :steps

    def initialize(steps, handler)
      @steps = steps
      @handler = handler
    end

    def call(*args)
      handler.call(*args)
    end
    alias_method :[], :call
  end

  class StepDSL
    attr_reader :name

    attr_reader :handler

    attr_reader :container

    attr_reader :steps

    def initialize(name, options, container, steps, &block)
      @name = name
      @handler = options.fetch(:with)
      @container = container
      @steps = steps
      instance_exec(&block) if block
    end

    def step(*args, &block)
      self.class.new(*args, container, steps, &block).call
    end

    def call
      steps[name] = container[handler]
    end
  end

  class FlowDSL
    attr_reader :options

    attr_reader :container

    attr_reader :steps

    def initialize(options, &block)
      @options = options
      @container = options.fetch(:container)
      @steps = {}
      instance_exec(&block)
    end

    def step(*args, &block)
      StepDSL.new(*args, container, steps, &block).call
    end

    def call
      Transaction.new(steps, steps.values.reverse.reduce(:>>))
    end
  end
end

def Transflow(options = {}, &block)
  Transflow::FlowDSL.new(options, &block).call
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
transflow-0.0.1 lib/transflow.rb