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

#inputs, #outputs

Attributes inherited from App::Api

#app

Instance Method Summary collapse

Methods inherited from Tap::Join

#associations, build, #to_spec

Methods inherited from App::Api

#associations, build, help, inherited, #inspect, parse, parse!, parser, #to_spec

Methods included from Signals

#sig, #signal, #signal?, #signals

Methods included from Signals::ModuleMethods

included

Constructor Details

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

Returns a new instance of Switch.



21
22
23
24
# File 'lib/tap/joins/switch.rb', line 21

def initialize(config={}, app=Tap::App.current, &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.



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

def selector
  @selector
end

Instance Method Details

#call(result) ⇒ Object



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

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

  exe(output, result)
end

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



26
27
28
29
# File 'lib/tap/joins/switch.rb', line 26

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