Class: Ripe::Blocks::WorkingBlock

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

Overview

This class represents a CLI::TaskCLI that has been parametrized. In the block arborescence, WorkingBlocks are always leaf nodes.

See Also:

  • CLI::TaskCLI
  • WorkerController::Preparer#prepare

Direct Known Subclasses

LiquidBlock

Instance Attribute Summary

Attributes inherited from Block

#blocks, #id, #vars

Instance Method Summary collapse

Methods inherited from Block

#+, #|

Constructor Details

#initialize(filename, vars = {}) ⇒ WorkingBlock

Returns a new instance of WorkingBlock.

Parameters:

  • filename (String)

    filename of the template file

  • vars (Hash<Symbol, String>) (defaults to: {})

    key-value pairs



18
19
20
21
# File 'lib/ripe/blocks/working_block.rb', line 18

def initialize(filename, vars = {})
  @filename = filename
  super(File.basename(@filename), [], vars)
end

Instance Method Details

#commandString

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

The resulting string contains the result of the application of parameters to the task from which the Ripe::Blocks::WorkingBlock was defined.

Returns:

  • (String)

    subtree command

See Also:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ripe/blocks/working_block.rb', line 46

def command
  <<-EOF.gsub(/^[ ]+/, '')

  # <#{id}>

  #{declarations.join("\n")}

  exec 1>"$LOG" 2>&1

  #{File.new(@filename).read}
  echo "##.DONE.##"

  # </#{id}>
  EOF
end

#declarationsString

Return working block parameters as a sequence of bash variable assignments.

Returns:

  • (String)

    sequence of bash variable assignments



29
30
31
32
33
34
35
# File 'lib/ripe/blocks/working_block.rb', line 29

def declarations
  vars.map do |key, value|
    lh = key.upcase
    rh = value.is_a?(Array) ? "(\"#{value.join("\" \"")}\")" : "\"#{value}\""
    "#{lh}=#{rh}"
  end
end

#prune(protect, depend) ⇒ Block?

Prune the subtree starting at the current block.

A Ripe::Blocks::WorkingBlock will be pruned if its targets exists, unless the protect parameter is set to true.

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



68
69
70
# File 'lib/ripe/blocks/working_block.rb', line 68

def prune(protect, depend)
  targets_exist? && !protect ? nil : self
end

#targets_exist?Boolean

Test whether all targets for the current block exist.

For Ripe::Blocks::WorkingBlocks, if there is so much as a single target – a block variable starting with output_) that does not exist, return false. Otherwise, return true.

Returns:

  • (Boolean)

    whether all targets exist



79
80
81
82
83
84
85
# File 'lib/ripe/blocks/working_block.rb', line 79

def targets_exist?
  statuses = @vars.select { |key, _| !key[/^output_/].nil? }.values.flatten
  targets_exist = statuses.map { |target| File.exists? target }.inject(:&)

  # If there are no targets at all, then assume that all targets exist
  targets_exist == nil ? true : targets_exist
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.

Since a Ripe::Blocks::WorkingBlock is always a leaf node in the tree, the subtree starting at the leaf node only contains the Ripe::Blocks::WorkingBlock.

Returns:

  • (Array<Symbol, Array>)

    topology nested list



93
94
95
# File 'lib/ripe/blocks/working_block.rb', line 93

def topology
  [@id]
end