Skip to content
This repository has been archived by the owner on Oct 23, 2019. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
undees committed Aug 3, 2011
0 parents commit 4854ecd
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
@@ -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.
4 changes: 4 additions & 0 deletions Rakefile
@@ -0,0 +1,4 @@
require 'rake/testtask'

Rake::TestTask.new do |t|
end
40 changes: 40 additions & 0 deletions lib/gritty.rb
@@ -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
36 changes: 36 additions & 0 deletions test/test_gritty.rb
@@ -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

0 comments on commit 4854ecd

Please sign in to comment.