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.
Files changed (51) hide show
  1. data/README.md +104 -0
  2. data/VERSION.yml +1 -1
  3. data/examples/{directory_without_subdirectory.rb → directory_walker/directory_without_subdirectory.rb} +6 -4
  4. data/examples/{find_files.rb → directory_walker/find_files.rb} +4 -3
  5. data/examples/{print_files.rb → directory_walker/print_files.rb} +5 -8
  6. data/examples/protovis/directory_to_json_visitor.rb +13 -0
  7. data/examples/protovis/index.html +87 -0
  8. data/examples/protovis/protovis-d3.2.js +15269 -0
  9. data/examples/protovis/treevisitor.js +33 -0
  10. data/examples/protovis/treevisitor.png +0 -0
  11. data/lib/treevisitor/basic_tree_node_visitor.rb +29 -0
  12. data/lib/treevisitor/cli/cli_tree.rb +43 -35
  13. data/lib/treevisitor/directory_walker.rb +260 -0
  14. data/lib/treevisitor/tree_node_visitor.rb +33 -5
  15. data/lib/treevisitor/{dir_processor.rb → util/dir_processor.rb} +0 -0
  16. data/lib/treevisitor/visitors/block_tree_node_visitor.rb +1 -1
  17. data/lib/treevisitor/visitors/build_dir_tree_visitor.rb +1 -1
  18. data/lib/treevisitor/visitors/callback_tree_node_visitor.rb +1 -2
  19. data/lib/treevisitor/visitors/callback_tree_node_visitor2.rb +1 -1
  20. data/lib/treevisitor/visitors/clone_tree_node_visitor.rb +1 -1
  21. data/lib/treevisitor/visitors/depth_tree_node_visitor.rb +1 -1
  22. data/lib/treevisitor/visitors/directory_to_hash_visitor.rb +33 -0
  23. data/lib/treevisitor/visitors/flat_print_tree_node_visitors.rb +1 -1
  24. data/lib/treevisitor/visitors/print_dir_tree_visitor.rb +1 -1
  25. data/lib/treevisitor/visitors/print_tree_node_visitor.rb +1 -1
  26. data/lib/treevisitor.rb +24 -8
  27. data/spec/fixtures/{test_dir → test_dir_1}/.dir_with_dot/dummy.txt +0 -0
  28. data/spec/fixtures/{test_dir → test_dir_1}/dir.1/dir.1.2/file.1.2.1 +0 -0
  29. data/spec/fixtures/{test_dir → test_dir_1}/dir.1/file.1.1 +0 -0
  30. data/spec/fixtures/{test_dir → test_dir_1}/dir.2/file.2.1 +0 -0
  31. data/spec/spec_helper.rb +6 -9
  32. data/spec/treevisitor/cli/cli_tree_spec.rb +5 -5
  33. data/spec/treevisitor/directory_walker_spec.rb +61 -0
  34. data/spec/treevisitor/tree_dsl_spec.rb +57 -0
  35. data/spec/treevisitor/tree_dsl_with_derived_class1_spec.rb +53 -0
  36. data/spec/treevisitor/tree_dsl_with_derived_class_spec.rb +51 -0
  37. data/spec/treevisitor/{dir_processor_spec.rb → util/dir_processor_spec.rb} +2 -4
  38. data/spec/treevisitor/visitor_dsl_spec.rb +32 -0
  39. data/spec/treevisitor/visitors/block_tree_node_visitor_spec.rb +27 -0
  40. data/spec/treevisitor/visitors/callback_tree_node_visitor2_spec.rb +38 -0
  41. data/spec/treevisitor/visitors/callback_tree_node_visitors_spec.rb +29 -0
  42. data/spec/treevisitor/visitors/depth_tree_node_visitor_spec.rb +30 -0
  43. data/tasks/jeweler.rake +4 -8
  44. data/treevisitor.gemspec +114 -0
  45. metadata +48 -29
  46. data/README.rdoc +0 -42
  47. data/lib/treevisitor/dir_tree_walker.rb +0 -165
  48. data/spec/treevisitor/dir_tree_walker_spec.rb +0 -50
  49. data/spec/treevisitor/tree_node_dsl_spec.rb +0 -153
  50. data/spec/treevisitor/tree_node_visitor_spec.rb +0 -70
  51. 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,5 +1,5 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 1
4
- :patch: 5
4
+ :patch: 6
5
5
  :build:
@@ -1,12 +1,14 @@
1
- cwd = File.expand_path( File.join( File.dirname(__FILE__), "..", "lib" ) )
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 directory without subdirectories
9
+ # Find directories without subdirectories
8
10
  #
9
- class DirWithoutSubDir < TreeVisitor::TreeNodeVisitor
11
+ class DirWithoutSubDir < TreeVisitor::BasicTreeNodeVisitor
10
12
 
11
13
  def initialize
12
14
  super
@@ -1,10 +1,11 @@
1
- cwd = File.expand_path( File.join( File.dirname(__FILE__), "..", "lib" ) )
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 < TreeNodeVisitor
8
+ class MyVisitor < BasicTreeNodeVisitor
8
9
  def visit_leaf_node( pathname )
9
10
  puts pathname
10
11
  end
@@ -1,15 +1,12 @@
1
- cwd = File.expand_path( File.join( File.dirname(__FILE__), "..", "lib" ) )
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
- class MyVisitor < TreeNodeVisitor
8
- def visit_leaf_node( pathname )
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>