class Clearly::Query::Graph
Stores a graph and provides methods to operate on the graph. Graph
nodes are a hash, and one special key contains an array of child nodes.
Attributes
child_key[R]
name of the hash key that holds the child nodes
root_node[R]
root node
Public Class Methods
new(root_node, child_key)
click to toggle source
Public Instance Methods
branches()
click to toggle source
build an array that contains paths from the root to all leaves @return [Array] paths from root to leaf
# File lib/clearly/query/graph.rb, line 30 def branches if @discovered_nodes.blank? && @paths.blank? traverse_branches(@root_node, nil) end @paths end
Private Instance Methods
traverse_branches(current_node, current_path)
click to toggle source
# File lib/clearly/query/graph.rb, line 39 def traverse_branches(current_node, current_path) child_nodes = current_node.include?(@child_key) ? current_node[@child_key] : [] current_node_no_children = current_node.dup.except(@child_key) @discovered_nodes.push(current_node_no_children) if child_nodes.size > 0 current_node[@child_key].each do |node| child_node_no_children = node.dup.except(@child_key) unless @discovered_nodes.include?(child_node_no_children) node_path = current_path.nil? ? [] : current_path.dup node_path.push(current_node_no_children) traverse_branches(node, node_path) end end else node_path = current_path.nil? ? [] : current_path.dup node_path.push(current_node_no_children) @paths.push(node_path) end end