Class: Seafoam::Graph
- Inherits:
-
Object
- Object
- Seafoam::Graph
- Defined in:
- lib/seafoam/graph.rb
Overview
A graph, with properties, nodes, and edges. We don’t encapsulate the graph too much - be careful.
Instance Attribute Summary collapse
-
#edges ⇒ Object
readonly
Returns the value of attribute edges.
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
-
#props ⇒ Object
readonly
Returns the value of attribute props.
Instance Method Summary collapse
-
#create_edge(from, to, props = nil) ⇒ Object
Create an edge between two nodes.
-
#create_node(id, props = nil) ⇒ Object
Create a node.
-
#initialize(props = nil) ⇒ Graph
constructor
A new instance of Graph.
Constructor Details
#initialize(props = nil) ⇒ Graph
Returns a new instance of Graph.
7 8 9 10 11 |
# File 'lib/seafoam/graph.rb', line 7 def initialize(props = nil) @props = props || {} @nodes = {} @edges = [] end |
Instance Attribute Details
#edges ⇒ Object (readonly)
Returns the value of attribute edges.
5 6 7 |
# File 'lib/seafoam/graph.rb', line 5 def edges @edges end |
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
5 6 7 |
# File 'lib/seafoam/graph.rb', line 5 def nodes @nodes end |
#props ⇒ Object (readonly)
Returns the value of attribute props.
5 6 7 |
# File 'lib/seafoam/graph.rb', line 5 def props @props end |
Instance Method Details
#create_edge(from, to, props = nil) ⇒ Object
Create an edge between two nodes.
22 23 24 25 26 27 28 29 |
# File 'lib/seafoam/graph.rb', line 22 def create_edge(from, to, props = nil) props ||= {} edge = Edge.new(from, to, props) @edges.push edge from.outputs.push edge to.inputs.push edge edge end |
#create_node(id, props = nil) ⇒ Object
Create a node.
14 15 16 17 18 19 |
# File 'lib/seafoam/graph.rb', line 14 def create_node(id, props = nil) props ||= {} node = Node.new(id, props) @nodes[id] = node node end |