Class: Connected::GenericNode

Inherits:
Object
  • Object
show all
Includes:
Vertex
Defined in:
lib/connected/generic_node.rb

Overview

Generic example node

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Vertex

#connection_to, #neighbors

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

#connectionsObject

Returns the value of attribute connections.



9
10
11
# File 'lib/connected/generic_node.rb', line 9

def connections
  @connections
end

#nameObject (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