Class: PiecePipe::MethodStep

Inherits:
Step
  • Object
show all
Defined in:
lib/piece_pipe/method_step.rb

Instance Attribute Summary

Attributes inherited from Step

#source

Instance Method Summary collapse

Methods inherited from Step

#to_enum

Constructor Details

#initialize(meth) ⇒ MethodStep

Returns a new instance of MethodStep.



3
4
5
6
7
# File 'lib/piece_pipe/method_step.rb', line 3

def initialize(meth)
  raise "method cannot be nil" if meth.nil?
  raise "method must accept 1 or 2 arguments; it accepts #{meth.arity}" if meth.arity != 1 and meth.arity != 2
  @method = meth
end

Instance Method Details

#process(item) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/piece_pipe/method_step.rb', line 11

def process(item)
  case @method.arity
  when 1
    # The method takes 1 argument, which we assume to be the input item.
    # We also assume that the method RETURNS the ONLY item it wishes to produce.
    # Will always produce exactly 1 output, even if that's just nil.
    produce @method.call(item)
  when 2
    # The method takes 2 arguments.  Assume first is input item, second is "producer",
    # ie, a means for the method to produce 0, 1 or many outputs at its discretion.
    # (by calling producer.produce)
    # Return value of @method.call is NOT considered.
    @method.call item, self
  end
end