Class: InteractorsMindmap::TreeNode::Node
- Inherits:
-
Object
- Object
- InteractorsMindmap::TreeNode::Node
- Defined in:
- lib/interactors_mindmap/tree_node.rb
Overview
Node data structure to manage data
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#level ⇒ Object
Returns the value of attribute level.
-
#parent ⇒ Object
Returns the value of attribute parent.
Instance Method Summary collapse
- #add_child(child_data:) ⇒ Object
- #add_node_child(node:) ⇒ Object
- #children_data ⇒ Object
- #children_formatted_data ⇒ Object
- #clone_node ⇒ Object
- #formatted_data ⇒ Object
-
#initialize(data:, parent: nil) ⇒ Node
constructor
A new instance of Node.
- #recalculate_level ⇒ Object
- #replace_with(node:) ⇒ Object
- #root? ⇒ Boolean
- #search_node(data:) ⇒ Object
Constructor Details
#initialize(data:, parent: nil) ⇒ Node
Returns a new instance of Node.
60 61 62 63 64 65 66 |
# File 'lib/interactors_mindmap/tree_node.rb', line 60 def initialize(data:, parent: nil) @data = data @parent = parent @children = [] calculate_level end |
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
58 59 60 |
# File 'lib/interactors_mindmap/tree_node.rb', line 58 def children @children end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
57 58 59 |
# File 'lib/interactors_mindmap/tree_node.rb', line 57 def data @data end |
#level ⇒ Object
Returns the value of attribute level.
58 59 60 |
# File 'lib/interactors_mindmap/tree_node.rb', line 58 def level @level end |
#parent ⇒ Object
Returns the value of attribute parent.
58 59 60 |
# File 'lib/interactors_mindmap/tree_node.rb', line 58 def parent @parent end |
Instance Method Details
#add_child(child_data:) ⇒ Object
68 69 70 71 72 |
# File 'lib/interactors_mindmap/tree_node.rb', line 68 def add_child(child_data:) node = TreeNode::Node.new(data: child_data, parent: self) @children << node end |
#add_node_child(node:) ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/interactors_mindmap/tree_node.rb', line 74 def add_node_child(node:) clon = node.clone_node @children << clon clon.parent = self clon.recalculate_level end |
#children_data ⇒ Object
83 84 85 |
# File 'lib/interactors_mindmap/tree_node.rb', line 83 def children_data children.map(&:data) end |
#children_formatted_data ⇒ Object
87 88 89 |
# File 'lib/interactors_mindmap/tree_node.rb', line 87 def children_formatted_data children.map(&:formatted_data) end |
#clone_node ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/interactors_mindmap/tree_node.rb', line 132 def clone_node clon = clone if children.any? children_clones = [] children.each do |child| child.parent = clon children_clones << child.clone_node end clon.children = children_clones end clon end |
#formatted_data ⇒ Object
95 96 97 98 |
# File 'lib/interactors_mindmap/tree_node.rb', line 95 def formatted_data data_with_space = " #{data}" data_with_space.rjust(data_with_space.length + level, "#") end |
#recalculate_level ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/interactors_mindmap/tree_node.rb', line 110 def recalculate_level calculate_level return unless children.any? children.map(&:recalculate_level) end |
#replace_with(node:) ⇒ Object
100 101 102 103 104 105 106 107 108 |
# File 'lib/interactors_mindmap/tree_node.rb', line 100 def replace_with(node:) return "Cant replace root_node" if root? index = parent.children.find_index(self) clon = node.clone_node clon.parent = parent parent.children[index] = clon clon.recalculate_level end |
#root? ⇒ Boolean
91 92 93 |
# File 'lib/interactors_mindmap/tree_node.rb', line 91 def root? parent.nil? end |
#search_node(data:) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/interactors_mindmap/tree_node.rb', line 118 def search_node(data:) nodes = [] if children.any? children.each do |child| nodes << child.search_node(data:) end elsif self.data == data nodes << self end nodes.flatten end |