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

- old
+ new

@@ -6,12 +6,49 @@ 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)