Class: InventoryRefresh::Graph
- Inherits:
-
Object
- Object
- InventoryRefresh::Graph
- Defined in:
- lib/inventory_refresh/graph.rb,
lib/inventory_refresh/graph/topological_sort.rb
Direct Known Subclasses
Defined Under Namespace
Classes: TopologicalSort
Instance Attribute Summary collapse
-
#edges ⇒ Object
readonly
Returns the value of attribute edges.
-
#fixed_edges ⇒ Object
readonly
Returns the value of attribute fixed_edges.
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
Instance Method Summary collapse
-
#initialize(nodes) ⇒ Graph
constructor
A new instance of Graph.
-
#to_graphviz(layers: nil) ⇒ String
Returns graph in GraphViz format, as a string.
Constructor Details
#initialize(nodes) ⇒ Graph
Returns a new instance of Graph.
8 9 10 11 12 13 14 |
# File 'lib/inventory_refresh/graph.rb', line 8 def initialize(nodes) @nodes = nodes @edges = [] @fixed_edges = [] construct_graph!(@nodes) end |
Instance Attribute Details
#edges ⇒ Object
Returns the value of attribute edges.
5 6 7 |
# File 'lib/inventory_refresh/graph.rb', line 5 def edges @edges end |
#fixed_edges ⇒ Object
Returns the value of attribute fixed_edges.
5 6 7 |
# File 'lib/inventory_refresh/graph.rb', line 5 def fixed_edges @fixed_edges end |
#nodes ⇒ Object
Returns the value of attribute nodes.
5 6 7 |
# File 'lib/inventory_refresh/graph.rb', line 5 def nodes @nodes end |
Instance Method Details
#to_graphviz(layers: nil) ⇒ String
Returns graph in GraphViz format, as a string. So it can be displayed.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/inventory_refresh/graph.rb', line 20 def to_graphviz(layers: nil) node_names = friendly_unique_node_names s = [] s << "digraph {" (layers || [nodes]).each_with_index do |layer_nodes, i| s << " subgraph cluster_#{i} { label = \"Layer #{i}\";" unless layers.nil? layer_nodes.each do |n| s << " #{node_names[n]}; \t// #{n.inspect}" end s << " }" unless layers.nil? end s << " // edges:" edges.each do |from, to| s << " #{node_names[from]} -> #{node_names[to]};" end s << "}" "#{s.join("\n")}\n" end |