Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Extracted xDOTScript to DOTScript class

  • Loading branch information...
commit 6a350effc87b0d80a0d663f0c8ea805e000803c9 1 parent abe4b04
@markus1189 markus1189 authored
Showing with 108 additions and 33 deletions.
  1. +12 −33 lib/graphviz.rb
  2. +58 −0 lib/graphviz/dot_script.rb
  3. +38 −0 test/test_dot_script.rb
View
45 lib/graphviz.rb
@@ -26,6 +26,7 @@
require 'graphviz/attrs'
require 'graphviz/constants'
require 'graphviz/elements'
+require 'graphviz/dot_script'
require 'graphviz/dot2ruby'
require 'graphviz/types'
@@ -423,27 +424,15 @@ def each_attribut(&b)
# * 2 = none
#
def output( hOpts = {} )
- xDOTScript = ""
+ xDOTScript = DOTScript.new
xLastType = nil
xSeparator = ""
xData = ""
lNotHugly = []
@elements_order.each { |kElement|
- if xLastType.nil? or xLastType != kElement["type"]
-
- if xData.length > 0
- case xLastType
- when "graph_attr"
- xDOTScript << " " + xData + ";\n"
-
- when "node_attr"
- xDOTScript << " node [" + xData + "];\n"
-
- when "edge_attr"
- xDOTScript << " edge [" + xData + "];\n"
- end
- end
+ unless xLastType and xLastType == kElement["type"]
+ xDOTScript.add_type(xLastType, xData) unless xData.empty?
xSeparator = ""
xData = ""
@@ -472,37 +461,25 @@ def output( hOpts = {} )
xSeparator = ", "
when "node"
- xDOTScript << " " + kElement["value"].output() + "\n"
+ xDOTScript << " " + kElement["value"].output()
when "edge"
- xDOTScript << " " + kElement["value"].output( @oGraphType ) + "\n"
+ xDOTScript << " " + kElement["value"].output( @oGraphType )
when "graph"
- xDOTScript << kElement["value"].output() + "\n"
+ xDOTScript << kElement["value"].output()
else
raise ArgumentError, "Don't know what to do with element type '#{kElement['type']}'"
end
}
- if xData.length > 0
- case xLastType
- when "graph_attr"
- xDOTScript << " " + xData + ";\n"
-
- when "node_attr"
- xDOTScript << " node [" + xData + "];\n"
+ xDOTScript.add_type(xLastType, xData) unless xData.empty?
- when "edge_attr"
- xDOTScript << " edge [" + xData + "];\n"
- end
- end
xDOTScript << "}"
if @oParentGraph
- xDOTScript = "subgraph #{GraphViz.escape(@name, :unquote_empty => true)} {\n" << xDOTScript
-
- return( xDOTScript )
+ xDOTScript.prepend("subgraph #{GraphViz.escape(@name, :unquote_empty => true)} {")
else
hOutput = {}
@@ -569,7 +546,9 @@ def output( hOpts = {} )
@output = hOutput if hOutput.size > 0
xStict = ((@strict && @oGraphType == "digraph") ? "strict " : "")
- xDOTScript = ("#{xStict}#{@oGraphType} #{GraphViz.escape(@name, :unquote_empty => true)} {\n" << xDOTScript).gsub( "\0", "" )
+ xDOTScript.prepend(
+ "#{xStict}#{@oGraphType} #{GraphViz.escape(@name, :unquote_empty => true)} {"
+ )
xOutputString = (@filename == String ||
@output.any? {|format, file| file == String })
View
58 lib/graphviz/dot_script.rb
@@ -0,0 +1,58 @@
+require "forwardable"
+class GraphViz
+ class DOTScript
+ extend Forwardable
+
+ def_delegators :@script, :end_with?
+
+ def initialize
+ @script = ''
+ end
+
+ def append(line)
+ @script << assure_ends_with(line, "\n")
+
+ self
+ end
+ alias :<< :append
+
+ def prepend(line)
+ @script = assure_ends_with(line,"\n") + @script
+
+ self
+ end
+
+ def add_type(type, data)
+ case type
+ when "graph_attr"
+ append_statement(" " + data)
+ when "node_attr"
+ append_statement(" node [" + data + "]")
+ when "edge_attr"
+ append_statement(" edge [" + data + "]")
+ else
+ raise ArgumentError,
+ "Unknown type: #{type}." <<
+ "Possible: 'graph_attr','node_attr','edge_attr'"
+ end
+
+ self
+ end
+
+ def to_str
+ @script
+ end
+ alias :to_s :to_str
+
+ private
+
+ def assure_ends_with(str,ending="\n")
+ str.end_with?("\n") ? str : str + "\n"
+ end
+
+ def append_statement(statement)
+ append(assure_ends_with(statement, ";\n"))
+ end
+
+ end
+end
View
38 test/test_dot_script.rb
@@ -0,0 +1,38 @@
+require "minitest/autorun"
+require_relative "../lib/graphviz/dot_script"
+
+describe GraphViz::DOTScript do
+ let(:script) { GraphViz::DOTScript.new }
+
+ it "appends a newline character if it is missing" do
+ str = "Test without newline"
+ script.append(str)
+ script.to_s.must_equal(str + "\n")
+ end
+
+ it "does not append a newline if already present" do
+ str = "Linebreak follows at my tail:\n"
+ script.append(str)
+ script.to_s.must_equal(str)
+ end
+
+ it "can prepend lines to its content" do
+ start_content = "I want to be at the top!\n"
+ additional_content = "No way!\n"
+
+ script.append(start_content)
+ script.prepend(additional_content)
+
+ script.to_s.must_equal(additional_content + start_content)
+ end
+
+ it "can add types with data" do
+ data = "some random data"
+ script.add_type("node_attr", data)
+ script.to_s.must_match(/\s*node\s*\[\s*#{data}\s*\]\s*/m)
+ end
+
+ it "raises an argument error on unknown types" do
+ -> { script.add_type("invalid", "") }.must_raise(ArgumentError)
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.