class AwsCftTools::DependencyTree::Nodes

Manages a list of nodes or vertices. Edges pass from a filename node to a variable node or from a variable node to a filename node, but never from a filename node to a filename node or from a variable node to a variable node.

Public Class Methods

new() click to toggle source
# File lib/aws_cft_tools/dependency_tree/nodes.rb, line 13
def initialize
  @nodes = default_hash
  @inverse_nodes = default_hash
end

Public Instance Methods

dependencies_for(node) click to toggle source

Computes the direct dependencies of a node that are of the same type as the node. If the node is a filename, then the returned nodes will be filenames. Likewise with variable names.

@param node [String] @return [Array<String>]

# File lib/aws_cft_tools/dependency_tree/nodes.rb, line 25
def dependencies_for(node)
  double_hop(@nodes, node.to_s)
end
dependents_for(node) click to toggle source

Computes the things dependent on the given node. If the node is a filename, then the returned nodes will be filenames. Likewise with variable names.

@param node [String] @return [Array<String>]

# File lib/aws_cft_tools/dependency_tree/nodes.rb, line 36
def dependents_for(node)
  double_hop(@inverse_nodes, node.to_s)
end

Private Instance Methods

default_hash() click to toggle source
# File lib/aws_cft_tools/dependency_tree/nodes.rb, line 66
def default_hash
  Hash.new { |hash, key| hash[key] = [] }
end
double_hop(set, node) click to toggle source
# File lib/aws_cft_tools/dependency_tree/nodes.rb, line 52
def double_hop(set, node)
  # we hop from a filename to a variable child to a filename child
  #     or from a variable to a filename child to a variable child
  set[node].flat_map { |neighbor| set[neighbor] }.uniq
end
tsort_each_child(node, &block) click to toggle source
# File lib/aws_cft_tools/dependency_tree/nodes.rb, line 62
def tsort_each_child(node, &block)
  @nodes[node].each(&block) if @nodes.include?(node)
end
tsort_each_node(&block) click to toggle source
# File lib/aws_cft_tools/dependency_tree/nodes.rb, line 58
def tsort_each_node(&block)
  @nodes.each_key(&block)
end