lib/transflow/publisher.rb in transflow-0.2.0 vs lib/transflow/publisher.rb in transflow-0.3.0

- old
+ new

@@ -1,15 +1,36 @@ require 'wisper' +require 'kleisli' +require 'transflow/errors' + module Transflow class Publisher include Wisper::Publisher attr_reader :name attr_reader :op + def self.[](name, op, options = {}) + type = + if options[:monadic] + Monadic + else + self + end + type.new(name, op) + end + + class Monadic < Publisher + def call(*args) + op.(*args) + .or { |result| broadcast_failure(*args, result) and Left(result) } + .>-> value { broadcast_success(value) and Right(value) } + end + end + class Curried < Publisher attr_reader :publisher attr_reader :arity @@ -50,14 +71,27 @@ op.is_a?(Proc) ? op.arity : op.method(:call).arity end def call(*args) result = op.call(*args) - broadcast(:"#{name}_success", result) + broadcast_success(result) result - rescue => err - broadcast(:"#{name}_failure", *args, err) - raise err + rescue StepError => err + broadcast_failure(*args, err) and raise(err) end alias_method :[], :call + + def subscribe(listeners, *args) + Array(listeners).each { |listener| super(listener, *args) } + end + + private + + def broadcast_success(result) + broadcast(:"#{name}_success", result) + end + + def broadcast_failure(*args, err) + broadcast(:"#{name}_failure", *args, err) + end end end