Module: Graphviz

Defined in:
lib/graphviz/version.rb,
lib/graphviz.rb,
lib/graphviz/graph.rb

Overview

Copyright, 2014, by Samuel G. D. Williams. <www.codeotaku.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Defined Under Namespace

Classes: Edge, Graph, Node, OutputError

Constant Summary collapse

VERSION =

The version.

"0.3.0"

Class Method Summary collapse

Class Method Details

.output(graph, options = {}) ⇒ String

Outputs the graph using the dot executable.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :output_format (String) — default: 'pdf'

    The output format (e.g. pdf).

  • :path (String)

    The output path, if not specified data is returned.

  • :dot (String) — default: 'dot'

    The dot executable to use.

Returns:

  • (String)

    any data generated by the command unless an output path is specified.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/graphviz.rb', line 35

def self.output(graph, options = {})
	text = graph.to_dot
	
	output_format = options[:format]
	
	if options[:path]
		# Grab the output format from the file name:
		if options[:path] =~ /\.(.*?)$/
			output_format ||= $1
		end
		
		output_file = File.open(options[:path], "w")
	else
		output_file = IO.pipe
	end
	
	output, input = IO.pipe
	pid = Process.spawn(options[:dot] || "dot", "-T#{output_format}", :out => output_file, :in => output)
	
	output.close
	
	# Send graph data to process:
	input.write(graph.to_dot)
	input.close
	
	_, status = Process.wait2(pid)
	
	if status != 0
		raise OutputError.new("dot exited with status #{status}")
	end
		
	# Did we use a local pipe for output?
	if Array === output_file
		return task.output.read
	end
end