Class: Tap::Join

Inherits:
App::Api show all
Defined in:
lib/tap/join.rb

Overview

:startdoc::join an unsyncrhonized, multi-way join

Join defines an unsynchronized, multi-way join where n inputs send their results to m outputs. Flags can augment how the results are passed, in particular for array results.

Direct Known Subclasses

Tap::Joins::Switch, Tap::Joins::Sync

Instance Attribute Summary collapse

Attributes inherited from App::Api

#app

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from App::Api

help, inherited, parse, parser

Methods included from Signals

#signal, #signal?

Methods included from Signals::ModuleMethods

included

Constructor Details

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

Initializes a new join with the specified configuration.



134
135
136
137
138
# File 'lib/tap/join.rb', line 134

def initialize(config={}, app=Tap::App.instance)
  @inputs = nil
  @outputs = nil
  super
end

Instance Attribute Details

#inputsObject (readonly)

An array of input nodes, or nil if the join has not been set.



128
129
130
# File 'lib/tap/join.rb', line 128

def inputs
  @inputs
end

#outputsObject (readonly)

An array of output nodes, or nil if the join has not been set.



131
132
133
# File 'lib/tap/join.rb', line 131

def outputs
  @outputs
end

Class Method Details

.build(spec = {}, app = Tap::App.instance) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/tap/join.rb', line 47

def build(spec={}, app=Tap::App.instance)
  inputs = resolve(spec['inputs']) do |var|
    app.get(var) or raise "missing join input: #{var}"
  end
  
  outputs = resolve(spec['outputs']) do |var|
    app.get(var) or raise "missing join output: #{var}"
  end
  
  new(spec['config'] || {}, app).join(inputs, outputs)
end

.intern(config = {}, app = Tap::App.instance, &block) ⇒ Object

Instantiates a new join with the input arguments and overrides call with the block. The block will be called with the join instance and result.

Simply instantiates a new join if no block is given.



26
27
28
29
30
31
32
33
# File 'lib/tap/join.rb', line 26

def intern(config={}, app=Tap::App.instance, &block) # :yields: join, result
  instance = new(config, app)
  if block_given?
    instance.extend Intern(:call)
    instance.call_block = block
  end
  instance
end

.parse!(argv = ARGV, app = Tap::App.instance) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tap/join.rb', line 35

def parse!(argv=ARGV, app=Tap::App.instance)
  parser = self.parser
  argv = parser.parse!(argv, :add_defaults => false)
  instance = build({
    'config' => parser.nested_config,
    'inputs' => argv.shift,
    'outputs' => argv.shift
  }, app)
  
  [instance, argv]
end

Instance Method Details

#associationsObject



164
165
166
# File 'lib/tap/join.rb', line 164

def associations
  [inputs + outputs]
end

#call(result) ⇒ Object

Executes the join logic for self, which by default passes the result to each output.



158
159
160
161
162
# File 'lib/tap/join.rb', line 158

def call(result)
  outputs.each do |output|
    dispatch(output, result)
  end
end

#join(inputs, outputs) ⇒ Object

Sets self as a join between the inputs and outputs.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/tap/join.rb', line 141

def join(inputs, outputs)
  @inputs.each do |input|
    input.joins.delete(self)
  end if @inputs
  
  @inputs = inputs
  
  inputs.each do |input|
    input.joins << self
  end if inputs
  
  @outputs = outputs
  self
end

#to_specObject



168
169
170
171
172
173
# File 'lib/tap/join.rb', line 168

def to_spec
  spec = super
  spec['inputs'] = inputs.collect {|node| app.var(node) }
  spec['outputs'] = outputs.collect {|node| app.var(node) }
  spec
end