Class: LibraryTree::Node

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(mod) ⇒ Node

Create a node for a Ruby module

Parameters:

  • mod (Module)

    the module to track



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

#childrenString, Array<LibraryTree::Node> (readonly)

Returns:

  • (String)

    the name of the underlying Ruby module being tracked

  • (Array<LibraryTree::Node>)

    parents of this node (including modules)

  • (Array<LibraryTree::Node>)

    children of this node (included modules)



13
14
15
# File 'lib/library_tree/node.rb', line 13

def children
  @children
end

#nameString, Array<LibraryTree::Node> (readonly)

Returns:

  • (String)

    the name of the underlying Ruby module being tracked

  • (Array<LibraryTree::Node>)

    parents of this node (including modules)

  • (Array<LibraryTree::Node>)

    children of this node (included modules)



13
14
15
# File 'lib/library_tree/node.rb', line 13

def name
  @name
end

#parentsString, Array<LibraryTree::Node> (readonly)

Returns:

  • (String)

    the name of the underlying Ruby module being tracked

  • (Array<LibraryTree::Node>)

    parents of this node (including modules)

  • (Array<LibraryTree::Node>)

    children of this node (included modules)



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)

Parameters:



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)

Parameters:



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.

Parameters:

  • indent (Integer) (defaults to: 0)

    number of levels to indent (default: 0)

  • seen (Hash{Integer=>true}) (defaults to: {})

    a map of visited node object_ids to avoid cycles

Returns:

  • (String)

    a multi-line string representing the subtree



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)

Returns:

  • (Boolean)


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

Parameters:

  • seen (Hash{Integer=>true}) (defaults to: {})

    a map of visited node object_ids to avoid cycles

Returns:

  • (Hash{Symbol=>Object})

    a tree structure with :name and :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