treevisitor 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +104 -0
- data/VERSION.yml +1 -1
- data/examples/{directory_without_subdirectory.rb → directory_walker/directory_without_subdirectory.rb} +6 -4
- data/examples/{find_files.rb → directory_walker/find_files.rb} +4 -3
- data/examples/{print_files.rb → directory_walker/print_files.rb} +5 -8
- data/examples/protovis/directory_to_json_visitor.rb +13 -0
- data/examples/protovis/index.html +87 -0
- data/examples/protovis/protovis-d3.2.js +15269 -0
- data/examples/protovis/treevisitor.js +33 -0
- data/examples/protovis/treevisitor.png +0 -0
- data/lib/treevisitor/basic_tree_node_visitor.rb +29 -0
- data/lib/treevisitor/cli/cli_tree.rb +43 -35
- data/lib/treevisitor/directory_walker.rb +260 -0
- data/lib/treevisitor/tree_node_visitor.rb +33 -5
- data/lib/treevisitor/{dir_processor.rb → util/dir_processor.rb} +0 -0
- data/lib/treevisitor/visitors/block_tree_node_visitor.rb +1 -1
- data/lib/treevisitor/visitors/build_dir_tree_visitor.rb +1 -1
- data/lib/treevisitor/visitors/callback_tree_node_visitor.rb +1 -2
- data/lib/treevisitor/visitors/callback_tree_node_visitor2.rb +1 -1
- data/lib/treevisitor/visitors/clone_tree_node_visitor.rb +1 -1
- data/lib/treevisitor/visitors/depth_tree_node_visitor.rb +1 -1
- data/lib/treevisitor/visitors/directory_to_hash_visitor.rb +33 -0
- data/lib/treevisitor/visitors/flat_print_tree_node_visitors.rb +1 -1
- data/lib/treevisitor/visitors/print_dir_tree_visitor.rb +1 -1
- data/lib/treevisitor/visitors/print_tree_node_visitor.rb +1 -1
- data/lib/treevisitor.rb +24 -8
- data/spec/fixtures/{test_dir → test_dir_1}/.dir_with_dot/dummy.txt +0 -0
- data/spec/fixtures/{test_dir → test_dir_1}/dir.1/dir.1.2/file.1.2.1 +0 -0
- data/spec/fixtures/{test_dir → test_dir_1}/dir.1/file.1.1 +0 -0
- data/spec/fixtures/{test_dir → test_dir_1}/dir.2/file.2.1 +0 -0
- data/spec/spec_helper.rb +6 -9
- data/spec/treevisitor/cli/cli_tree_spec.rb +5 -5
- data/spec/treevisitor/directory_walker_spec.rb +61 -0
- data/spec/treevisitor/tree_dsl_spec.rb +57 -0
- data/spec/treevisitor/tree_dsl_with_derived_class1_spec.rb +53 -0
- data/spec/treevisitor/tree_dsl_with_derived_class_spec.rb +51 -0
- data/spec/treevisitor/{dir_processor_spec.rb → util/dir_processor_spec.rb} +2 -4
- data/spec/treevisitor/visitor_dsl_spec.rb +32 -0
- data/spec/treevisitor/visitors/block_tree_node_visitor_spec.rb +27 -0
- data/spec/treevisitor/visitors/callback_tree_node_visitor2_spec.rb +38 -0
- data/spec/treevisitor/visitors/callback_tree_node_visitors_spec.rb +29 -0
- data/spec/treevisitor/visitors/depth_tree_node_visitor_spec.rb +30 -0
- data/tasks/jeweler.rake +4 -8
- data/treevisitor.gemspec +114 -0
- metadata +48 -29
- data/README.rdoc +0 -42
- data/lib/treevisitor/dir_tree_walker.rb +0 -165
- data/spec/treevisitor/dir_tree_walker_spec.rb +0 -50
- data/spec/treevisitor/tree_node_dsl_spec.rb +0 -153
- data/spec/treevisitor/tree_node_visitor_spec.rb +0 -70
- data/tasks/rubyforge.rake +0 -28
data/README.md
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
tree.rb / tree visitor library
|
2
|
+
================================================
|
3
|
+
|
4
|
+
**tree.rb** is a 'clone' of tree unix command. It is based on tree visitor library.
|
5
|
+
Tree visitor is an implementation of visitor design pattern.
|
6
|
+
|
7
|
+
<pre>
|
8
|
+
$ tree.rb lib
|
9
|
+
|
10
|
+
lib
|
11
|
+
|-- tree_visitor.rb
|
12
|
+
|-- treevisitor.rb
|
13
|
+
|-- treevisitor_cli.rb
|
14
|
+
`-- treevisitor
|
15
|
+
|-- abs_node.rb
|
16
|
+
|-- dir_processor.rb
|
17
|
+
|-- dir_tree_walker.rb
|
18
|
+
|-- leaf_node.rb
|
19
|
+
|-- tree_node.rb
|
20
|
+
|-- tree_node_visitor.rb
|
21
|
+
|-- cli
|
22
|
+
| `-- cli_tree.rb
|
23
|
+
`-- visitors
|
24
|
+
|-- block_tree_node_visitor.rb
|
25
|
+
|-- build_dir_tree_visitor.rb
|
26
|
+
|-- callback_tree_node_visitor.rb
|
27
|
+
|-- callback_tree_node_visitor2.rb
|
28
|
+
|-- clone_tree_node_visitor.rb
|
29
|
+
|-- depth_tree_node_visitor.rb
|
30
|
+
|-- flat_print_tree_node_visitors.rb
|
31
|
+
|-- print_dir_tree_visitor.rb
|
32
|
+
`-- print_tree_node_visitor.rb
|
33
|
+
</pre>
|
34
|
+
|
35
|
+
The **tree.rb** can generate json structure suitable to be visualized with [protovis][1] javascript library.
|
36
|
+
See the examples directory.
|
37
|
+
|
38
|
+
<pre>
|
39
|
+
$ ruby bin/tree.rb --format json lib
|
40
|
+
|
41
|
+
{
|
42
|
+
"lib": {
|
43
|
+
"tree_visitor.rb": 45,
|
44
|
+
"treevisitor": {
|
45
|
+
"abs_node.rb": 2858,
|
46
|
+
"basic_tree_node_visitor.rb": 578,
|
47
|
+
"cli": {
|
48
|
+
"cli_tree.rb": 2929
|
49
|
+
},
|
50
|
+
"directory_walker.rb": 5049,
|
51
|
+
"leaf_node.rb": 526,
|
52
|
+
"tree_node.rb": 5665,
|
53
|
+
"tree_node_visitor.rb": 1263,
|
54
|
+
"util": {
|
55
|
+
"dir_processor.rb": 928
|
56
|
+
},
|
57
|
+
"visitors": {
|
58
|
+
"block_tree_node_visitor.rb": 386,
|
59
|
+
"build_dir_tree_visitor.rb": 1167,
|
60
|
+
"callback_tree_node_visitor.rb": 1029,
|
61
|
+
"callback_tree_node_visitor2.rb": 1652,
|
62
|
+
"clone_tree_node_visitor.rb": 837,
|
63
|
+
"depth_tree_node_visitor.rb": 427,
|
64
|
+
"directory_to_hash_visitor.rb": 639,
|
65
|
+
"flat_print_tree_node_visitors.rb": 341,
|
66
|
+
"print_dir_tree_visitor.rb": 387,
|
67
|
+
"print_tree_node_visitor.rb": 707
|
68
|
+
}
|
69
|
+
},
|
70
|
+
"treevisitor.rb": 976,
|
71
|
+
"treevisitor_cli.rb": 87
|
72
|
+
}
|
73
|
+
</pre>
|
74
|
+
|
75
|
+
The json structure can be transformed into image:
|
76
|
+
|
77
|
+
<img src='https://github.com/tokiro/treevisitor/raw/master/examples/protovis/treevisitor.png'/>
|
78
|
+
|
79
|
+
### Documentation
|
80
|
+
|
81
|
+
http://rubydoc.info/gems/treevisitor
|
82
|
+
|
83
|
+
Try tree.rb -h to get help on command line.
|
84
|
+
See examples directory to use treevisitor in your code.
|
85
|
+
|
86
|
+
### INSTALL:
|
87
|
+
|
88
|
+
sudo gem install treevisitor
|
89
|
+
|
90
|
+
### Contributions
|
91
|
+
|
92
|
+
Everybody is welcome to contribute to this project by commenting the source code, suggesting modifications or new ideas,
|
93
|
+
reporting bugs, writing some documentation and, of course, you're also welcome to contribute with patches as well!
|
94
|
+
|
95
|
+
### Rubies
|
96
|
+
|
97
|
+
This gem have been tested on [MRI][8] 1.9.2.
|
98
|
+
|
99
|
+
### Copyright
|
100
|
+
|
101
|
+
Copyright (c) 2009-2011 tokiro.oyama@gmail.com. See LICENSE for details.
|
102
|
+
|
103
|
+
[1]: http://vis.stanford.edu/protovis/
|
104
|
+
[8]: http://www.ruby-lang.org/en/
|
data/VERSION.yml
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
-
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'ostruct'
|
3
|
+
|
4
|
+
cwd = File.expand_path( File.join( File.dirname(__FILE__), "..", "..", "lib" ) )
|
2
5
|
$:.unshift(cwd) unless $:.include?(cwd)
|
3
6
|
require 'treevisitor'
|
4
7
|
|
5
|
-
require 'ostruct'
|
6
8
|
#
|
7
|
-
# Find
|
9
|
+
# Find directories without subdirectories
|
8
10
|
#
|
9
|
-
class DirWithoutSubDir < TreeVisitor::
|
11
|
+
class DirWithoutSubDir < TreeVisitor::BasicTreeNodeVisitor
|
10
12
|
|
11
13
|
def initialize
|
12
14
|
super
|
@@ -1,10 +1,11 @@
|
|
1
|
-
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
cwd = File.expand_path( File.join( File.dirname(__FILE__), "..", "..", "lib" ) )
|
2
4
|
$:.unshift(cwd) unless $:.include?(cwd)
|
3
5
|
require 'treevisitor'
|
4
|
-
|
5
6
|
include TreeVisitor
|
6
7
|
|
7
|
-
class MyVisitor <
|
8
|
+
class MyVisitor < BasicTreeNodeVisitor
|
8
9
|
def visit_leaf_node( pathname )
|
9
10
|
puts pathname
|
10
11
|
end
|
@@ -1,15 +1,12 @@
|
|
1
|
-
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
cwd = File.expand_path( File.join( File.dirname(__FILE__), "..", "..", "lib" ) )
|
2
3
|
$:.unshift(cwd) unless $:.include?(cwd)
|
3
4
|
require 'treevisitor'
|
4
|
-
|
5
5
|
include TreeVisitor
|
6
6
|
|
7
|
-
|
8
|
-
|
7
|
+
dtw = DirTreeWalker.new( :ignore => ".git" )
|
8
|
+
dtw.run ".." do
|
9
|
+
on_visit_leaf_node do |pathname|
|
9
10
|
puts pathname
|
10
11
|
end
|
11
12
|
end
|
12
|
-
|
13
|
-
dtw = DirTreeWalker.new( ".." )
|
14
|
-
dtw.ignore ".git"
|
15
|
-
dtw.run( MyVisitor.new )
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
cwd = File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "lib"))
|
4
|
+
$:.unshift(cwd) unless $:.include?(cwd)
|
5
|
+
require 'treevisitor'
|
6
|
+
include TreeVisitor
|
7
|
+
|
8
|
+
dir = File.expand_path( File.join("..", "..", "lib") )
|
9
|
+
|
10
|
+
dtw = TreeVisitor::DirTreeWalker.new( :ignore => [/^\./, "doc", "pkg"] )
|
11
|
+
root = dtw.run(dir, DirectoryToHashVisitor.new(dir)).root
|
12
|
+
str = "var treevisitor = #{JSON.pretty_generate(root)};"
|
13
|
+
File.open( File.dirname(__FILE__) + "/treevisitor.js", "w"){ |f| f.write str }
|
@@ -0,0 +1,87 @@
|
|
1
|
+
<html>
|
2
|
+
<head>
|
3
|
+
<title>Indented Tree</title>
|
4
|
+
<script type="text/javascript" src="protovis-d3.2.js"></script>
|
5
|
+
<script type="text/javascript" src="treevisitor.js"></script>
|
6
|
+
|
7
|
+
<style type="text/css">
|
8
|
+
body {
|
9
|
+
margin: 0;
|
10
|
+
display: table;
|
11
|
+
height: 100%;
|
12
|
+
width: 100%;
|
13
|
+
font: 14px/134% Helvetica Neue, sans-serif;
|
14
|
+
}
|
15
|
+
|
16
|
+
#center {
|
17
|
+
display: table-cell;
|
18
|
+
vertical-align: middle;
|
19
|
+
}
|
20
|
+
|
21
|
+
#fig {
|
22
|
+
position: relative;
|
23
|
+
margin: auto;
|
24
|
+
}
|
25
|
+
|
26
|
+
</style>
|
27
|
+
</head>
|
28
|
+
<body>
|
29
|
+
|
30
|
+
|
31
|
+
<script type="text/javascript+protovis">
|
32
|
+
|
33
|
+
for (first in treevisitor) break;
|
34
|
+
|
35
|
+
var root = pv.dom(treevisitor)
|
36
|
+
.root(first)
|
37
|
+
.sort(function(a, b) pv.naturalOrder(a.nodeName, b.nodeName));
|
38
|
+
|
39
|
+
/* Recursively compute the package sizes. */
|
40
|
+
root.visitAfter(function(n) {
|
41
|
+
if (n.firstChild) {
|
42
|
+
n.nodeValue = pv.sum(n.childNodes, function(n) n.nodeValue);
|
43
|
+
}
|
44
|
+
});
|
45
|
+
|
46
|
+
var vis = new pv.Panel()
|
47
|
+
.width(400)
|
48
|
+
.height(function() (root.nodes().length + 1) * 12)
|
49
|
+
.margin(5);
|
50
|
+
|
51
|
+
var layout = vis.add(pv.Layout.Indent)
|
52
|
+
.nodes(function() root.nodes())
|
53
|
+
.depth(25)
|
54
|
+
.breadth(12);
|
55
|
+
|
56
|
+
layout.link.add(pv.Line);
|
57
|
+
|
58
|
+
var node = layout.node.add(pv.Panel)
|
59
|
+
.top(function(n) n.y - 6)
|
60
|
+
.height(12)
|
61
|
+
.right(6)
|
62
|
+
.strokeStyle(null)
|
63
|
+
.events("all")
|
64
|
+
.event("mousedown", toggle);
|
65
|
+
|
66
|
+
node.anchor("left").add(pv.Dot)
|
67
|
+
.strokeStyle("#1f77b4")
|
68
|
+
.fillStyle(function(n) n.toggled ? "#1f77b4" : n.firstChild ? "#aec7e8" : "#ff7f0e")
|
69
|
+
.title(function t(d) d.parentNode ? (t(d.parentNode) + "." + d.nodeName) : d.nodeName)
|
70
|
+
.anchor("right").add(pv.Label)
|
71
|
+
.text(function(n) n.nodeName);
|
72
|
+
|
73
|
+
node.anchor("right").add(pv.Label)
|
74
|
+
.textStyle(function(n) n.firstChild || n.toggled ? "#aaa" : "#000")
|
75
|
+
.text(function(n) (n.nodeValue >> 10) + "KB");
|
76
|
+
|
77
|
+
vis.render();
|
78
|
+
|
79
|
+
/* Toggles the selected node, then updates the layout. */
|
80
|
+
function toggle(n) {
|
81
|
+
n.toggle(pv.event.altKey);
|
82
|
+
return layout.reset().root;
|
83
|
+
}
|
84
|
+
|
85
|
+
</script>
|
86
|
+
</body>
|
87
|
+
</html>
|