Class: Dsl::Graph::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/dsl/graph/graph.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label = "graph") ⇒ Graph

Returns a new instance of Graph.



15
16
17
18
19
20
# File 'lib/dsl/graph/graph.rb', line 15

def initialize(label="graph")
  @label = label
  @next = { node: 0, edge: 0}
  @nodes = {}
  @edges = {}
end

Instance Attribute Details

#edgesObject (readonly)

Returns the value of attribute edges.



13
14
15
# File 'lib/dsl/graph/graph.rb', line 13

def edges
  @edges
end

#labelObject

Returns the value of attribute label.



11
12
13
# File 'lib/dsl/graph/graph.rb', line 11

def label
  @label
end

#nodesObject (readonly)

Returns the value of attribute nodes.



12
13
14
# File 'lib/dsl/graph/graph.rb', line 12

def nodes
  @nodes
end

Instance Method Details

#add_edge(node1, node2, label) ⇒ Object



29
30
31
32
33
34
# File 'lib/dsl/graph/graph.rb', line 29

def add_edge(node1, node2, label)
  id = (@next[:edge] += 1)
  edge = Edge.new(id, node1, node2, label)
  @edges[id] = edge
  edge
end

#add_node(label) ⇒ Object



22
23
24
25
26
27
# File 'lib/dsl/graph/graph.rb', line 22

def add_node(label)
  id = (@next[:node] += 1)
  node = Node.new(id, label)
  @nodes[id] = node
  node
end

#debugObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dsl/graph/graph.rb', line 36

def debug
  puts "graph: #{@label}"
  puts "> nodes (#{@nodes.size})"
  @nodes.each do |id, node|
    puts "  - node(#{node.id}): #{node} "
  end
  puts "> edges (#{@edges.size})"
  @edges.each do |id, edge| 
    puts "  - edge(#{edge.id}): #{edge.from} --(#{edge})--> #{edge.to}"
  end
end

#export(filename = nil) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/dsl/graph/graph.rb', line 48

def export(filename=nil)
  if filename.nil?
    filename = "#{graph.label}.yaml"
    filename = "graph.yaml" if graph.label.nil? || graph.label.empty?
  end
  ExportGraph.new(self, filename).call
end

#load(filename = nil) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/dsl/graph/graph.rb', line 56

def load(filename=nil)
  if filename.nil?
    filename = "#{graph.label}.yaml"
    filename = "graph.yaml" if graph.label.nil? || graph.label.empty?
  end
  LoadGraph.new(filename).call
end

#run(&block) ⇒ Object



64
65
66
67
# File 'lib/dsl/graph/graph.rb', line 64

def run(&block)
  instance_eval(&block) 
  self
end