Class: Clearly::Query::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/clearly/query/graph.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_node, child_key) ⇒ Clearly::Query::DepthFirstSearch

Create a new Graph.

Parameters:

  • root_node (Array)
  • child_key (Symbol)


18
19
20
21
22
23
24
25
26
# File 'lib/clearly/query/graph.rb', line 18

def initialize(root_node, child_key)
  @root_node = root_node
  @child_key = child_key

  @discovered_nodes = []
  @paths = []

  self
end

Instance Attribute Details

#child_keyObject (readonly)

name of the hash key that holds the child nodes



12
13
14
# File 'lib/clearly/query/graph.rb', line 12

def child_key
  @child_key
end

#root_nodeObject (readonly)

root node



9
10
11
# File 'lib/clearly/query/graph.rb', line 9

def root_node
  @root_node
end

Instance Method Details

#branchesArray

build an array that contains paths from the root to all leaves

Returns:

  • (Array)

    paths from root to leaf



30
31
32
33
34
35
# File 'lib/clearly/query/graph.rb', line 30

def branches
  if @discovered_nodes.blank? && @paths.blank?
    traverse_branches(@root_node, nil)
  end
  @paths
end