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

edges[R]
meta[R]
name[R]
type[R]
unresolved[R]

Public Class Methods

new(name:, type:, edges: [], unresolved: [], meta: []) click to toggle source

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

+(node) click to toggle source

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
inspect()
Alias for: to_s
intersects?(node) click to toggle source

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
to_s() click to toggle source

Public: Return the name of the node.

Returns a string.

# File lib/society/node.rb, line 60
def to_s
  name.to_s
end
Also aliased as: inspect

Private Instance Methods

accumulate_edges(edges_a, edges_b) click to toggle source

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