lib/transflow.rb in transflow-0.0.1 vs lib/transflow.rb in transflow-0.0.2

- old
+ new

@@ -1,73 +1,21 @@ -require 'transproc' require 'transflow/version' +require 'transflow/flow_dsl' -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 - +# Define a transaction flow +# +# @example +# +# container = { do_one: some_obj, do_two: some_obj } +# +# my_business_flow = Transflow(container: container) do +# step(:one, with: :do_one) { step(:two, with: :do_two } +# end +# +# my_business_flow[some_input] +# +# @options [Hash] options The option hash +# +# @api public def Transflow(options = {}, &block) Transflow::FlowDSL.new(options, &block).call end