Class: Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/pomona/tree.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTree

Returns a new instance of Tree.



9
10
11
# File 'lib/pomona/tree.rb', line 9

def initialize
  @data = { tree_array: [] }
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/pomona/tree.rb', line 7

def data
  @data
end

Instance Method Details

#add_node(node_hash, parent_id = nil) ⇒ Object



13
14
15
16
17
18
# File 'lib/pomona/tree.rb', line 13

def add_node(node_hash, parent_id = nil)
  #Creates a new node to be attached to a parent
  #Grants a generated id to the node
  node = Node.new(node_hash, next_id, parent_id)
  attach_to_parent(node, parent_id)
end

#find(id) ⇒ Object



39
40
41
42
# File 'lib/pomona/tree.rb', line 39

def find(id)
  #Finds and returns a Node by its id using the Extractor
  Extractor.find_node_by_id(id, nodes)
end

#nodesObject



34
35
36
37
# File 'lib/pomona/tree.rb', line 34

def nodes
  #Returns an array of all of the nodes attached to the tree
  @data[:tree_array]
end

#remove_node(id) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/pomona/tree.rb', line 20

def remove_node(id)
  #Removes a node from the Tree by its id
  #Removes all of their descendents as well
  node_to_delete = find(id)
  parent_node = find_parent_node(node_to_delete)
  Pruner.remove_node_and_descendents(node_to_delete, parent_node)
end

#values_at(keys) ⇒ Object



28
29
30
31
32
# File 'lib/pomona/tree.rb', line 28

def values_at(keys)
  #Returns an array of data from the tree based on the given keys
  data = Extractor.get_all_by_keys(nodes, keys)
  flatten_output_array(data)
end