Class: Connected::GenericNode
- Inherits:
-
Object
- Object
- Connected::GenericNode
- Includes:
- Vertex
- Defined in:
- lib/connected/generic_node.rb
Overview
Generic example node
Instance Attribute Summary collapse
-
#connections ⇒ Object
Returns the value of attribute connections.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #connects_to(other, metric: 1, state: :open, directed: false) ⇒ Object
- #disconnect_from(other, directed: false) ⇒ Object
-
#initialize(name) ⇒ GenericNode
constructor
A new instance of GenericNode.
Methods included from Vertex
Constructor Details
#initialize(name) ⇒ GenericNode
Returns a new instance of GenericNode.
11 12 13 14 |
# File 'lib/connected/generic_node.rb', line 11 def initialize(name) @name = name @connections = [] end |
Instance Attribute Details
#connections ⇒ Object
Returns the value of attribute connections.
9 10 11 |
# File 'lib/connected/generic_node.rb', line 9 def connections @connections end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/connected/generic_node.rb', line 8 def name @name end |
Instance Method Details
#connects_to(other, metric: 1, state: :open, directed: false) ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/connected/generic_node.rb', line 16 def connects_to(other, metric: 1, state: :open, directed: false) # Only one connection between nodes return true if neighbors.include?(other) connections << GenericConnection.new( from: self, to: other, metric: metric, state: state.to_sym ) other.connects_to(self, metric: metric, state: state) unless directed end |
#disconnect_from(other, directed: false) ⇒ Object
27 28 29 30 |
# File 'lib/connected/generic_node.rb', line 27 def disconnect_from(other, directed: false) connections.delete_if { |c| c.to == other } other.disconnect_from(self) if other.neighbors.include?(self) && !directed end |