gritty 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.md +4 -0
  2. data/Rakefile +27 -0
  3. data/lib/gritty.rb +50 -0
  4. data/test/test_gritty.rb +61 -0
  5. metadata +63 -0
@@ -0,0 +1,4 @@
1
+ # Gritty
2
+ _Pretty-print + Graph = Gritty_
3
+
4
+ This library makes GraphViz graphs out of Ruby objects, for very simple Ruby objects. It uses Ryan Davis's Graph gem under the hood.
@@ -0,0 +1,27 @@
1
+ require 'rake/testtask'
2
+ require 'rubygems/package_task'
3
+
4
+ PKG_FILES = Dir['Rakefile', 'README.md', 'lib/**', 'test/**']
5
+ PKG_VERSION = '0.1'
6
+
7
+ spec = Gem::Specification.new do |s|
8
+ s.platform = Gem::Platform::RUBY
9
+ s.summary = 'Make GraphViz graphs out of simple Ruby objects'
10
+ s.name = 'gritty'
11
+ s.version = PKG_VERSION
12
+ s.authors = 'Ian Dees'
13
+ s.email = 'undees@gmail.com'
14
+ s.homepage = 'https://github.com/undees/gritty'
15
+ s.require_path = 'lib'
16
+ s.files = PKG_FILES
17
+ s.description = <<-EOF
18
+ This library makes GraphViz graphs out of Ruby objects, for very simple Ruby objects. It uses Ryan Davis's Graph gem under the hood. Pretty-print + Graph = Gritty.
19
+ EOF
20
+ s.add_runtime_dependency 'graph', '~> 2.5'
21
+ end
22
+
23
+ Gem::PackageTask.new(spec) do |pkg|
24
+ end
25
+
26
+ Rake::TestTask.new do |t|
27
+ end
@@ -0,0 +1,50 @@
1
+ require 'graph'
2
+
3
+ module Gritty
4
+ module AutoNode
5
+ def autonode label
6
+ @node_num = (@node_num || 0) + 1
7
+ name = "node#{@node_num}"
8
+ node name, label
9
+ name
10
+ end
11
+ end
12
+
13
+ class NodeBuilder
14
+ def initialize(graph)
15
+ @graph = graph
16
+ end
17
+
18
+ def build(obj, parent)
19
+ case obj
20
+ when Hash then
21
+ obj.each do |k, v|
22
+ node = @graph.autonode k.inspect
23
+ @graph.edge parent, node
24
+ build v, node
25
+ end
26
+ when Array then
27
+ obj.each do |o|
28
+ build o, parent
29
+ end
30
+ when Struct then
31
+ container = @graph.autonode obj.class.to_s
32
+ @graph.edge parent, container
33
+
34
+ obj.members.each do |k|
35
+ v = obj.send k
36
+ node = @graph.autonode k.inspect
37
+ @graph.edge container, node
38
+ build v, node
39
+ end
40
+ else
41
+ node = obj.inspect
42
+ @graph.edge parent, node
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ class Graph
49
+ include Gritty::AutoNode
50
+ end
@@ -0,0 +1,61 @@
1
+ $: << File.expand_path(File.basename(__FILE__) + '/../lib')
2
+
3
+ require 'minitest/autorun'
4
+ require 'minitest/spec'
5
+ require 'gritty'
6
+
7
+ include Gritty
8
+
9
+ describe NodeBuilder do
10
+ it 'builds a simple graph' do
11
+ g = digraph do
12
+ input = [{:foo => {:bar => "baz"}},
13
+ {:bar => {:baz => "quux"}}]
14
+ builder = NodeBuilder.new self
15
+ builder.build input, 'root'
16
+ end
17
+
18
+ expected = <<HERE.strip
19
+ digraph
20
+ {
21
+ "node1" [ label = ":foo" ];
22
+ "node2" [ label = ":bar" ];
23
+ "node3" [ label = ":bar" ];
24
+ "node4" [ label = ":baz" ];
25
+ "root" -> "node1";
26
+ "root" -> "node3";
27
+ "node1" -> "node2";
28
+ "node2" -> "\\"baz\\"";
29
+ "node3" -> "node4";
30
+ "node4" -> "\\"quux\\"";
31
+ }
32
+ HERE
33
+
34
+ g.to_s.must_equal expected
35
+ end
36
+
37
+ it 'understands Structs' do
38
+ Foo = Struct.new :bar, :baz
39
+ g = digraph do
40
+ input = Foo.new 'quux', 'quuux'
41
+ builder = NodeBuilder.new self
42
+ builder.build input, 'root'
43
+ end
44
+
45
+ expected = <<HERE.strip
46
+ digraph
47
+ {
48
+ "node1" [ label = "Foo" ];
49
+ "node2" [ label = ":bar" ];
50
+ "node3" [ label = ":baz" ];
51
+ "root" -> "node1";
52
+ "node1" -> "node2";
53
+ "node1" -> "node3";
54
+ "node2" -> "\\"quux\\"";
55
+ "node3" -> "\\"quuux\\"";
56
+ }
57
+ HERE
58
+
59
+ g.to_s.must_equal expected
60
+ end
61
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gritty
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ian Dees
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: graph
16
+ requirement: &70302944919460 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.5'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70302944919460
25
+ description: ! 'This library makes GraphViz graphs out of Ruby objects, for very simple
26
+ Ruby objects. It uses Ryan Davis''s Graph gem under the hood. Pretty-print + Graph
27
+ = Gritty.
28
+
29
+ '
30
+ email: undees@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - Rakefile
36
+ - README.md
37
+ - lib/gritty.rb
38
+ - test/test_gritty.rb
39
+ homepage: https://github.com/undees/gritty
40
+ licenses: []
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 1.8.11
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Make GraphViz graphs out of simple Ruby objects
63
+ test_files: []