class LogBook::Tree
Attributes
action[RW]
depth[RW]
nodes[RW]
request_uuid[RW]
Public Class Methods
new(author: nil, action: nil, request_uuid: nil)
click to toggle source
# File lib/log_book/tree.rb, line 5 def initialize(author: nil, action: nil, request_uuid: nil) @nodes = {} @depth = 0 @author = author @action = action @request_uuid = request_uuid end
Public Instance Methods
add(record)
click to toggle source
# File lib/log_book/tree.rb, line 13 def add(record) node = nodes[record.recording_key] node = node ? node.merge(record) : nodes[record.recording_key] = Node.new(record) add_parent(node, record.parent) add_children(node, record.children) end
add_child(node, child)
click to toggle source
# File lib/log_book/tree.rb, line 37 def add_child(node, child) return unless child child_node = nodes[child.recording_key] ||= Node.new(child) child_node.parent = node node.children << child_node update_depth(node, node.depth) end
add_children(node, children)
click to toggle source
# File lib/log_book/tree.rb, line 29 def add_children(node, children) return unless children Array.wrap(children).each do |child| add_child(node, child) end end
add_parent(node, parent)
click to toggle source
# File lib/log_book/tree.rb, line 20 def add_parent(node, parent) return unless parent parent_node = nodes[parent.recording_key] ||= Node.new(parent) node.parent = parent_node parent_node.children << node update_depth(parent_node, parent_node.depth) end
at_depth(depth)
click to toggle source
# File lib/log_book/tree.rb, line 58 def at_depth(depth) nodes.select { |_, node| node.depth == depth} end
records(only_roots: false)
click to toggle source
# File lib/log_book/tree.rb, line 54 def records(only_roots: false) only_roots ? at_depth(0) : nodes end
update_depth(node, depth)
click to toggle source
# File lib/log_book/tree.rb, line 46 def update_depth(node, depth) node.depth = depth @depth = [@depth, depth].max node.children.each do |child| update_depth(child, depth + 1) end end