Sha256: 90e0493337f4a42d1752eec9c3db393389968726d4de738e5c7f594f1b20a571

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

require 'wisper'

module Transflow
  class Publisher
    include Wisper::Publisher

    attr_reader :name

    attr_reader :op

    class Curried < Publisher
      attr_reader :publisher

      attr_reader :arity

      attr_reader :curry_args

      def initialize(publisher, curry_args = [])
        @publisher = publisher
        @arity = publisher.arity
        @curry_args = curry_args
      end

      def call(*args)
        all_args = curry_args + args

        if all_args.size == arity
          publisher.call(*all_args)
        else
          self.class.new(publisher, all_args)
        end
      end

      def subscribe(*args)
        publisher.subscribe(*args)
      end
    end

    def initialize(name, op)
      @name = name
      @op = op
    end

    def curry
      raise "can't curry publisher where operation arity is < 0" if arity < 0
      Curried.new(self)
    end

    def arity
      op.is_a?(Proc) ? op.arity : op.method(:call).arity
    end

    def call(*args)
      result = op.call(*args)
      broadcast(:"#{name}_success", result)
      result
    rescue => err
      broadcast(:"#{name}_failure", *args, err)
      raise err
    end
    alias_method :[], :call
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
transflow-0.2.0 lib/transflow/publisher.rb