Class: Ripe::Blocks::MultiBlock Abstract

Inherits:
Block
  • Object
show all
Defined in:
lib/ripe/blocks/multi_block.rb

Overview

This class is abstract.

This class represents a block composition, that is, a placeholder block that joins multiple blocks together.

This class only exists to provide a superclass for ParallelBlock and SerialBlock.

Direct Known Subclasses

ParallelBlock, SerialBlock

Instance Attribute Summary

Attributes inherited from Block

#blocks, #id, #vars

Instance Method Summary collapse

Methods inherited from Block

#+, #command, #|

Constructor Details

#initialize(id, *blocks) ⇒ MultiBlock

Create a new, empty Ripe::Blocks::MultiBlock.

Parameters:

  • id (String)

    a mandatory, but optionally unique identifier for the block

  • blocks (Array<Block>)

    list of children blocks



26
27
28
29
# File 'lib/ripe/blocks/multi_block.rb', line 26

def initialize(id, *blocks)
  # Ignore nil objects
  super(id, blocks.compact, {})
end

Instance Method Details

#prune(protect, depend) ⇒ Block?

Prune the subtree starting at the current block.

Unless the block is protected, attempt to prune all children blocks. If all blocks are pruned, return nothing. If a single block remains, return that block. If more than one block remains, return the current Ripe::Blocks::MultiBlock.

Parameters:

  • protect (Boolean)

    if the current block (and recursively, its children) should be protected from pruning – setting this parameter to true guarantees that the block will not be pruned

  • depend (Boolean)

    if the current block is unprotected because its parent (serially) needs to be executed

Returns:

  • (Block, nil)

    a Block representing the subtree that has not been pruned, and nil otherwise



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ripe/blocks/multi_block.rb', line 39

def prune(protect, depend)
  if !protect
    @blocks = @blocks.map { |block| block.prune(protect, depend) }.compact
    case @blocks.length
    when 0; nil
    when 1; @blocks.first
    else;   self
    end
  else
    self
  end
end

#targets_exist?Boolean

Test whether all targets for the current block exist.

A Ripe::Blocks::MultiBlock‘s targets exist if the targets of all its # children exist.

Returns:

  • (Boolean)

    whether all targets exist



58
59
60
# File 'lib/ripe/blocks/multi_block.rb', line 58

def targets_exist?
  @blocks.map(&:targets_exist?).inject(:&)
end

#topologyArray<Symbol, Array>

Return the topology of the subtree starting at the current block in a nested list format. The first element of any nested list is the identifier of the corresponding block in the subtree, and the subsequent elements are each a subtree corresponding to the children blocks of the current subtree.

Returns:

  • (Array<Symbol, Array>)

    topology nested list



65
66
67
# File 'lib/ripe/blocks/multi_block.rb', line 65

def topology
  [@id] + @blocks.map(&:topology)
end