Class: Ripe::Blocks::SerialBlock

Inherits:
MultiBlock show all
Defined in:
lib/ripe/blocks/serial_block.rb

Overview

This class represents a parallel composition of blocks, in that the children blocks of an instance of this class are to be run in serial.

Instance Attribute Summary

Attributes inherited from Block

#blocks, #id, #vars

Instance Method Summary collapse

Methods inherited from MultiBlock

#targets_exist?, #topology

Methods inherited from Block

#+, #targets_exist?, #topology, #|

Constructor Details

#initialize(*blocks) ⇒ SerialBlock

Returns a new instance of SerialBlock.

Parameters:

  • blocks (Array<Block>)

    list of children blocks



14
15
16
# File 'lib/ripe/blocks/serial_block.rb', line 14

def initialize(*blocks)
  super(:+, *blocks)
end

Instance Method Details

#commandString

Return the string command of the subtree starting at the current block.

Returns:

  • (String)

    subtree command



21
22
23
# File 'lib/ripe/blocks/serial_block.rb', line 21

def command
  @blocks.map { |block| "(\n%s\n)" % block.command }.join(' ; ')
end

#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 MultiBlock.

A Ripe::Blocks::SerialBlock differs from a MultiBlock or ParallelBlock in that there is a linear dependency for its children blocks as they are to be run in serial. If a given block must be run, then all subsequent blocks that depend on it must be run as well.

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ripe/blocks/serial_block.rb', line 35

def prune(protect, depend)
  return super_prune(protect, depend) if !depend
  return self if protect

  @blocks = @blocks.map do |block|
    new_protect = !block.targets_exist?
    new_block = block.prune(protect, depend)
    protect = new_protect
    new_block
  end
  @blocks = @blocks.compact

  case @blocks.length
  when 0
    nil
  when 1
    @blocks.first
  else
    self
  end
end

#super_pruneObject



25
# File 'lib/ripe/blocks/serial_block.rb', line 25

alias :super_prune :prune