Class: Tap::Joins::Switch

Inherits:
Tap::Join show all
Defined in:
lib/tap/joins/switch.rb

Overview

A Switch join allows a block to determine which output from an array of outputs will receive the results of the input.

– Note that switch is NOT identified as a join that can be created from the command line. Switch inherently requires a block to select which output receives the input, and so cannot be loaded from data alone.

Switch facilitates in-code switch joins.

Defined Under Namespace

Classes: SwitchError

Instance Attribute Summary collapse

Attributes inherited from Tap::Join

#app, #inputs, #outputs

Instance Method Summary collapse

Methods inherited from Tap::Join

inherited, instantiate, intern, parse, parse!

Constructor Details

#initialize(config = {}, app = Tap::App.instance, &block) ⇒ Switch

Returns a new instance of Switch.



19
20
21
22
# File 'lib/tap/joins/switch.rb', line 19

def initialize(config={}, app=Tap::App.instance, &block)
  super(config, app)
  @selector = block
end

Instance Attribute Details

#selectorObject

An object responding to call that return the index of the output to that receives the result.



17
18
19
# File 'lib/tap/joins/switch.rb', line 17

def selector
  @selector
end

Instance Method Details

#call(result) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/tap/joins/switch.rb', line 29

def call(result)
  index = selector.call(result)
  
  unless index && output = outputs[index] 
    raise SwitchError, "no switch target at index: #{index}"
  end

  dispatch(output, result)
end

#join(inputs, outputs, &block) ⇒ Object



24
25
26
27
# File 'lib/tap/joins/switch.rb', line 24

def join(inputs, outputs, &block)
  @selector = block
  super(inputs, outputs)
end