Class: Society::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/society/node.rb

Overview

The Node class represents a single node in a graph. In this case, nodes are assumed to be either Classes or Modules.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, type:, edges: [], unresolved: [], meta: []) ⇒ Node

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: [])


19
20
21
22
23
24
25
# 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

Instance Attribute Details

#edgesObject (readonly)

Returns the value of attribute edges.



7
8
9
# File 'lib/society/node.rb', line 7

def edges
  @edges
end

#metaObject (readonly)

Returns the value of attribute meta.



7
8
9
# File 'lib/society/node.rb', line 7

def meta
  @meta
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/society/node.rb', line 7

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/society/node.rb', line 7

def type
  @type
end

#unresolvedObject (readonly)

Returns the value of attribute unresolved.



7
8
9
# File 'lib/society/node.rb', line 7

def unresolved
  @unresolved
end

Instance Method Details

#+(node) ⇒ Object

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.



45
46
47
48
49
50
51
52
53
54
55
# 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

#intersects?(node) ⇒ Boolean

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.

Returns:

  • (Boolean)


33
34
35
# File 'lib/society/node.rb', line 33

def intersects?(node)
  node.name == name && node.type == type
end

#to_sObject Also known as: inspect

Public: Return the name of the node.

Returns a string.



60
61
62
# File 'lib/society/node.rb', line 60

def to_s
  name.to_s
end