Class: Gush::Graph

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(workflow, options = {}) ⇒ Graph

Returns a new instance of Graph.



9
10
11
12
13
# File 'lib/gush/graph.rb', line 9

def initialize(workflow, options = {})
  @workflow = workflow
  @filename = options.fetch(:filename, "graph.png")
  @path = options.fetch(:path, Pathname.new(Dir.tmpdir).join(filename))
end

Instance Attribute Details

#end_nodeObject (readonly)

Returns the value of attribute end_node.



7
8
9
# File 'lib/gush/graph.rb', line 7

def end_node
  @end_node
end

#filenameObject (readonly)

Returns the value of attribute filename.



7
8
9
# File 'lib/gush/graph.rb', line 7

def filename
  @filename
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/gush/graph.rb', line 7

def path
  @path
end

#start_nodeObject (readonly)

Returns the value of attribute start_node.



7
8
9
# File 'lib/gush/graph.rb', line 7

def start_node
  @start_node
end

#workflowObject (readonly)

Returns the value of attribute workflow.



7
8
9
# File 'lib/gush/graph.rb', line 7

def workflow
  @workflow
end

Instance Method Details

#vizObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/gush/graph.rb', line 15

def viz
  @graph = Graphviz::Graph.new(**graph_options)
  @start_node = add_node('start', shape: 'diamond', fillcolor: '#CFF09E')
  @end_node = add_node('end', shape: 'diamond', fillcolor: '#F56991')

  # First, create nodes for all jobs
  @job_name_to_node_map = {}
  workflow.jobs.each do |job|
    add_job_node(job)
  end

  # Next, link up the jobs with edges
  workflow.jobs.each do |job|
    link_job_edges(job)
  end

  format = 'png'
  file_format = path.split('.')[-1]
  format = file_format if file_format.length == 3

  Graphviz::output(@graph, path: path, format: format)
end