awkward 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,23 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'autotest/restart'
4
+
5
+ # Autotest.add_hook :initialize do |at|
6
+ # at.extra_files << "../some/external/dependency.rb"
7
+ #
8
+ # at.libs << ":../some/external"
9
+ #
10
+ # at.add_exception 'vendor'
11
+ #
12
+ # at.add_mapping(/dependency.rb/) do |f, _|
13
+ # at.files_matching(/test_.*rb$/)
14
+ # end
15
+ #
16
+ # %w(TestA TestB).each do |klass|
17
+ # at.extra_class_map[klass] = "test/test_misc.rb"
18
+ # end
19
+ # end
20
+
21
+ # Autotest.add_hook :run_command do |at|
22
+ # system "rake build"
23
+ # end
@@ -0,0 +1,6 @@
1
+ === 1.0.0 / 2010-11-12
2
+
3
+ * 1 major enhancement
4
+
5
+ * Birthday!
6
+
@@ -0,0 +1,8 @@
1
+ .autotest
2
+ CHANGELOG.rdoc
3
+ Manifest.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/awkward.rb
7
+ lib/awkward/visitor.rb
8
+ test/test_awkward.rb
@@ -0,0 +1,60 @@
1
+ = awkward
2
+
3
+ * http://github.com/tenderlove/awkward
4
+
5
+ == DESCRIPTION:
6
+
7
+ Awkward helps you walk your Object Graph and output a dot file suitable for
8
+ viewing in Graphviz. Awkward doesn't know how to visit your objects, so you
9
+ must teach it by implementing a subclass of Awkward::Visitor.
10
+
11
+ == FEATURES/PROBLEMS:
12
+
13
+ * May not work
14
+
15
+ == SYNOPSIS:
16
+
17
+ class MyVisitor < Awkward::Visitor
18
+ def visit_My_Object object
19
+ edge(:some_method) do
20
+ visit object.some_method
21
+ end
22
+ end
23
+ end
24
+
25
+ viz = MyVisitor.new
26
+ viz.accept my_object
27
+ puts viz.to_dot
28
+
29
+ == REQUIREMENTS:
30
+
31
+ * FIX (list of requirements)
32
+
33
+ == INSTALL:
34
+
35
+ * FIX (sudo gem install, anything else)
36
+
37
+ == LICENSE:
38
+
39
+ (The MIT License)
40
+
41
+ Copyright (c) 2010 Aaron Patterson
42
+
43
+ Permission is hereby granted, free of charge, to any person obtaining
44
+ a copy of this software and associated documentation files (the
45
+ 'Software'), to deal in the Software without restriction, including
46
+ without limitation the rights to use, copy, modify, merge, publish,
47
+ distribute, sublicense, and/or sell copies of the Software, and to
48
+ permit persons to whom the Software is furnished to do so, subject to
49
+ the following conditions:
50
+
51
+ The above copyright notice and this permission notice shall be
52
+ included in all copies or substantial portions of the Software.
53
+
54
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
55
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
56
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
57
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
58
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
59
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
60
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,13 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+
6
+ Hoe.spec 'awkward' do
7
+ developer('Aaron Patterson', 'aaron@tenderlovemaking.com')
8
+ self.readme_file = 'README.rdoc'
9
+ self.history_file = 'CHANGELOG.rdoc'
10
+ self.extra_rdoc_files = FileList['*.rdoc']
11
+ end
12
+
13
+ # vim: syntax=ruby
@@ -0,0 +1,5 @@
1
+ require 'awkward/visitor'
2
+
3
+ module Awkward
4
+ VERSION = '1.0.0'
5
+ end
@@ -0,0 +1,101 @@
1
+ module Awkward
2
+ class Visitor
3
+ class Node < Struct.new :object, :name, :value # :nodoc:
4
+ def initialize object
5
+ super(object, object.class.name)
6
+ end
7
+ end
8
+
9
+ Edge = Struct.new :name, :left, :right
10
+
11
+ attr_reader :edges
12
+
13
+ def initialize
14
+ @nodes = []
15
+ @stack = []
16
+ @edges = []
17
+ @callstack = []
18
+ @seen = {}
19
+ end
20
+
21
+ def nodes
22
+ @seen.values
23
+ end
24
+
25
+ def accept o
26
+ return connect(@stack.last, @seen[o.object_id]) if @seen.key? o.object_id
27
+
28
+ node = Node.new o
29
+
30
+ @seen[o.object_id] = node
31
+
32
+ connect(@stack.last, node) unless @callstack.empty?
33
+
34
+ @stack.push node
35
+
36
+ send "visit_#{o.class.name.gsub('::', '_')}", o
37
+
38
+ @stack.pop
39
+ end
40
+
41
+ def to_dot
42
+ dot = <<-eodot
43
+ digraph "Graph" {
44
+ node [width=0.375,height=0.25,shape = "record"];
45
+ eodot
46
+
47
+ nodes.each do |node|
48
+ dot.concat <<-eonode
49
+ #{node.object_id} [label="<f0> #{[node.name, node.value].compact.join('|')}"];
50
+ eonode
51
+ end
52
+
53
+ edges.each do |edge|
54
+ dot.concat <<-eoedge
55
+ #{edge.left.object_id} -> #{edge.right.object_id} [label="#{edge.name}"];
56
+ eoedge
57
+ end
58
+ dot + "}"
59
+ end
60
+
61
+ private
62
+
63
+ def visit_Hash o
64
+ o.each_with_index do |(k,v),i|
65
+ edge("key: #{i}") { accept k }
66
+ edge("value: #{i}") { accept v }
67
+ end
68
+ end
69
+
70
+ def visit_Array o
71
+ o.each_with_index do |v, i|
72
+ edge(i) { accept v }
73
+ end
74
+ end
75
+
76
+
77
+ def escape string
78
+ string.gsub '"', '\"'
79
+ end
80
+
81
+ def visit_Symbol o
82
+ @seen[o.object_id].value = escape(o.to_s)
83
+ end
84
+
85
+ alias :visit_String :visit_Symbol
86
+ alias :visit_Regexp :visit_Symbol
87
+ alias :visit_Fixnum :visit_Symbol
88
+ alias :visit_Bignum :visit_Symbol
89
+ alias :visit_Float :visit_Symbol
90
+
91
+ def edge sym
92
+ @callstack.push sym
93
+ yield
94
+ @callstack.pop
95
+ end
96
+
97
+ def connect from, to
98
+ @edges << Edge.new(@callstack.last, from, to)
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,54 @@
1
+ require "test/unit"
2
+ require "awkward"
3
+
4
+ class TestAwkward < Test::Unit::TestCase
5
+ def setup
6
+ @tree = {
7
+ :foo => { 'bar' => %w{ a b c } }
8
+ }
9
+ @awkward = Awkward::Visitor.new
10
+ end
11
+
12
+ def test_leaves
13
+ [
14
+ /foo/,
15
+ 1,
16
+ 470948572349857203498572049358729345872345123435465,
17
+ "adsfadsfadsf",
18
+ 1.2,
19
+ :foo,
20
+ ].each do |value|
21
+ @awkward.accept value
22
+ end
23
+ end
24
+
25
+ def test_nodes
26
+ @awkward.accept @tree
27
+ assert_equal 8, @awkward.nodes.length
28
+ end
29
+
30
+ def test_edges
31
+ @awkward.accept @tree
32
+ assert_equal 7, @awkward.edges.length
33
+ end
34
+
35
+ def test_edges_have_nodes
36
+ @awkward.accept @tree
37
+ nodes = @awkward.edges.map { |e| [e.left, e.right] }.flatten.uniq
38
+ expected = @awkward.nodes
39
+ assert_equal expected.sort_by(&:object_id), nodes.sort_by(&:object_id)
40
+ end
41
+
42
+ def test_to_dot
43
+ @awkward.accept @tree
44
+ @awkward.to_dot
45
+ end
46
+
47
+ def test_cyclic
48
+ foo = {}
49
+ foo[:a] = { :b => { :c => foo } }
50
+
51
+ @awkward.accept foo
52
+ @awkward.to_dot
53
+ end
54
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: awkward
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Aaron Patterson
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-11-13 00:00:00 -06:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rubyforge
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 2
30
+ - 0
31
+ - 4
32
+ version: 2.0.4
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: hoe
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 2
45
+ - 6
46
+ - 2
47
+ version: 2.6.2
48
+ type: :development
49
+ version_requirements: *id002
50
+ description: |-
51
+ Awkward helps you walk your Object Graph and output a dot file suitable for
52
+ viewing in Graphviz. Awkward doesn't know how to visit your objects, so you
53
+ must teach it by implementing a subclass of Awkward::Visitor.
54
+ email:
55
+ - aaron@tenderlovemaking.com
56
+ executables: []
57
+
58
+ extensions: []
59
+
60
+ extra_rdoc_files:
61
+ - Manifest.txt
62
+ - CHANGELOG.rdoc
63
+ - README.rdoc
64
+ files:
65
+ - .autotest
66
+ - CHANGELOG.rdoc
67
+ - Manifest.txt
68
+ - README.rdoc
69
+ - Rakefile
70
+ - lib/awkward.rb
71
+ - lib/awkward/visitor.rb
72
+ - test/test_awkward.rb
73
+ has_rdoc: true
74
+ homepage: http://github.com/tenderlove/awkward
75
+ licenses: []
76
+
77
+ post_install_message:
78
+ rdoc_options:
79
+ - --main
80
+ - README.rdoc
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ requirements: []
100
+
101
+ rubyforge_project: awkward
102
+ rubygems_version: 1.3.7
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: Awkward helps you walk your Object Graph and output a dot file suitable for viewing in Graphviz
106
+ test_files:
107
+ - test/test_awkward.rb