Sha256: 1b83d1e528bd41a761806693f889cd5ff0a3cd30519ce9a41f15d36794be5341

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

require 'tap/join'

module Tap
  module Joins
    
    # 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.
    class Switch < Join
      
      # An object responding to call that return the index of the output
      # to that receives the result.
      attr_accessor :selector
      
      def initialize(config={}, app=Tap::App.current, &block)
        super(config, app)
        @selector = block
      end
      
      def join(inputs, outputs, &block)
        @selector = block
        super(inputs, outputs)
      end

      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
      
      # Raised by a Switch join to indicate when a switch index is out of bounds.
      class SwitchError < RuntimeError # :nodoc:
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tap-1.4.0 lib/tap/joins/switch.rb
tap-1.3.0 lib/tap/joins/switch.rb