Class: Society::Node
- Inherits:
-
Object
- Object
- Society::Node
- 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
-
#edges ⇒ Object
readonly
Returns the value of attribute edges.
-
#meta ⇒ Object
readonly
Returns the value of attribute meta.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#unresolved ⇒ Object
readonly
Returns the value of attribute unresolved.
Instance Method Summary collapse
-
#+(node) ⇒ Object
Public: Create a node representing the sum of the current node and another, intersecting node.
-
#initialize(name:, type:, edges: [], unresolved: [], meta: []) ⇒ Node
constructor
Public: Creates a new node.
-
#intersects?(node) ⇒ Boolean
Public: Reports whether another node intersects.
-
#to_s ⇒ Object
(also: #inspect)
Public: Return the name of the node.
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 = end |
Instance Attribute Details
#edges ⇒ Object (readonly)
Returns the value of attribute edges.
7 8 9 |
# File 'lib/society/node.rb', line 7 def edges @edges end |
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
7 8 9 |
# File 'lib/society/node.rb', line 7 def @meta end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/society/node.rb', line 7 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
7 8 9 |
# File 'lib/society/node.rb', line 7 def type @type end |
#unresolved ⇒ Object (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) = + node. return Node.new(name: name, type: type, edges: new_edges, unresolved: new_unresolved, 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.
33 34 35 |
# File 'lib/society/node.rb', line 33 def intersects?(node) node.name == name && node.type == type end |
#to_s ⇒ Object 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 |