class Society::Node
The Node
class represents a single node in a graph. In this case, nodes are assumed to be either Classes or Modules.
Attributes
Public Class Methods
Public: Creates a new node.
name - Name to be assigned to the node. Assumed to be a Class or
Module name.
type - Type of node. Assumed to be :class or :module. edges - Edges which point to other nodes. unresolved - References to nodes which have not yet been resolved.
(default: [])
meta - Information to be tracked about the node itself.
(default: [])
# File lib/society/node.rb, line 19 def initialize(name:, type:, edges: [], unresolved: [], meta: []) @name = name @type = type @edges = edges @unresolved = unresolved @meta = meta end
Public Instance Methods
Public: Create a node representing the sum of the current node and another, intersecting node.
node - Another node object.
Returns self if self is passed. Returns nil if the nodes do not intersect. Returns a new node containing the sum of both nodes' edges otherwise.
# File lib/society/node.rb, line 45 def +(node) return self if self == node return nil unless self.intersects?(node) new_edges = accumulate_edges(edges, node.edges) new_unresolved = accumulate_edges(unresolved, node.unresolved) new_meta = meta + node.meta return Node.new(name: name, type: type, edges: new_edges, unresolved: new_unresolved, meta: new_meta) end
Public: Reports whether another node intersects. Nodes are considered to be intersecting if their name and type are equal.
node - Another Node
.
Returns true or false.
# File lib/society/node.rb, line 33 def intersects?(node) node.name == name && node.type == type end
Public: Return the name of the node.
Returns a string.
# File lib/society/node.rb, line 60 def to_s name.to_s end
Private Instance Methods
Internal: Reduce two lists of edges to a single list with equivalent edges summed.
edges_a - Array of Edges. edges_b - Array of Edges.
Returns an array of Edges.
# File lib/society/node.rb, line 74 def accumulate_edges(edges_a, edges_b) new_edges = (edges_a + edges_b).flatten.reduce([]) do |edges, edge| if edges.detect { |e| e.to == edge.to } edges.map { |e| e + edge || e } else edges + [edge] end end end