Class: LibraryTree::Node
- Inherits:
-
Object
- Object
- LibraryTree::Node
- Defined in:
- lib/library_tree/node.rb
Overview
Represents a tracked module and its relationships Parents are modules that include this module Children are modules that this module is included into. In Ruby, when A includes B, A depends on B. We will model edges parent -> child as: including_module -> included_module
Instance Attribute Summary collapse
- #children ⇒ String, Array<LibraryTree::Node> readonly
- #name ⇒ String, Array<LibraryTree::Node> readonly
- #parents ⇒ String, Array<LibraryTree::Node> readonly
Instance Method Summary collapse
-
#add_child(child) ⇒ void
Add a child node (an included module).
-
#add_parent(parent) ⇒ void
Add a parent node (a module that includes this module).
-
#initialize(mod) ⇒ Node
constructor
Create a node for a Ruby module.
-
#render(indent = 0, seen = {}) ⇒ String
Render a tree view starting from this node.
-
#root? ⇒ Boolean
Whether this node has no parents (it is a root).
-
#to_h(seen = {}) ⇒ Hash{Symbol=>Object}
Return a Hash representation of this node and its children.
Constructor Details
#initialize(mod) ⇒ Node
Create a node for a Ruby module
17 18 19 20 21 |
# File 'lib/library_tree/node.rb', line 17 def initialize(mod) @name = mod.name || mod.inspect @parents = [] # Array<Node> @children = [] # Array<Node> end |
Instance Attribute Details
#children ⇒ String, Array<LibraryTree::Node> (readonly)
13 14 15 |
# File 'lib/library_tree/node.rb', line 13 def children @children end |
#name ⇒ String, Array<LibraryTree::Node> (readonly)
13 14 15 |
# File 'lib/library_tree/node.rb', line 13 def name @name end |
#parents ⇒ String, Array<LibraryTree::Node> (readonly)
13 14 15 |
# File 'lib/library_tree/node.rb', line 13 def parents @parents end |
Instance Method Details
#add_child(child) ⇒ void
This method returns an undefined value.
Add a child node (an included module)
26 27 28 29 |
# File 'lib/library_tree/node.rb', line 26 def add_child(child) return if @children.include?(child) @children << child end |
#add_parent(parent) ⇒ void
This method returns an undefined value.
Add a parent node (a module that includes this module)
34 35 36 37 |
# File 'lib/library_tree/node.rb', line 34 def add_parent(parent) return if @parents.include?(parent) @parents << parent end |
#render(indent = 0, seen = {}) ⇒ String
Render a tree view starting from this node.
61 62 63 64 65 66 67 68 69 |
# File 'lib/library_tree/node.rb', line 61 def render(indent = 0, seen = {}) return "#{" " * indent}* #{name} (…cycle…)\n" if seen[object_id] seen[object_id] = true out = "#{" " * indent}* #{name}\n" children.each do |child| out += child.render(indent + 1, seen.dup) end out end |
#root? ⇒ Boolean
Whether this node has no parents (it is a root)
41 42 43 |
# File 'lib/library_tree/node.rb', line 41 def root? @parents.empty? end |
#to_h(seen = {}) ⇒ Hash{Symbol=>Object}
Return a Hash representation of this node and its children
48 49 50 51 52 53 54 55 |
# File 'lib/library_tree/node.rb', line 48 def to_h(seen = {}) return {name: name} if seen[object_id] seen[object_id] = true { name: name, children: children.map { |c| c.to_h(seen) }, } end |