This repository has been archived by the owner on Oct 23, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4854ecd
Showing
4 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Gritty | ||
_Pretty-print + Graph = Gritty_ | ||
|
||
This library makes GraphViz graphs out of Ruby objects, for very simple Ruby objects. It uses Ryan Davis's Graph gem under the hood. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
require 'rake/testtask' | ||
|
||
Rake::TestTask.new do |t| | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'graph' | ||
|
||
module Gritty | ||
module AutoNode | ||
def autonode label | ||
@node_num = (@node_num || 0) + 1 | ||
name = "node#{@node_num}" | ||
node name, label | ||
name | ||
end | ||
end | ||
|
||
class NodeBuilder | ||
def initialize(graph) | ||
@graph = graph | ||
end | ||
|
||
def build(obj, parent) | ||
case obj | ||
when Hash then | ||
obj.each do |k, v| | ||
node = @graph.autonode k.inspect | ||
@graph.edge parent, node | ||
build v, node | ||
end | ||
when Array then | ||
obj.each do |o| | ||
build o, parent | ||
end | ||
else | ||
node = obj.inspect | ||
@graph.edge parent, node | ||
end | ||
end | ||
end | ||
end | ||
|
||
class Graph | ||
include Gritty::AutoNode | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
$: << File.expand_path(File.basename(__FILE__) + '/../lib') | ||
|
||
require 'minitest/autorun' | ||
require 'minitest/spec' | ||
require 'gritty' | ||
|
||
include Gritty | ||
|
||
describe NodeBuilder do | ||
it 'builds a simple graph' do | ||
g = digraph do | ||
input = [{:foo => {:bar => "baz"}}, | ||
{:bar => {:baz => "quux"}}] | ||
builder = NodeBuilder.new self | ||
builder.build input, 'root' | ||
end | ||
|
||
expected = <<HERE.strip | ||
digraph | ||
{ | ||
"node1" [ label = ":foo" ]; | ||
"node2" [ label = ":bar" ]; | ||
"node3" [ label = ":bar" ]; | ||
"node4" [ label = ":baz" ]; | ||
"root" -> "node1"; | ||
"root" -> "node3"; | ||
"node1" -> "node2"; | ||
"node2" -> "\\"baz\\""; | ||
"node3" -> "node4"; | ||
"node4" -> "\\"quux\\""; | ||
} | ||
HERE | ||
|
||
g.to_s.must_equal expected | ||
end | ||
end |