treevisitor 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. data/README.md +6 -1
  2. data/Rakefile +6 -11
  3. data/lib/tree_visitor.rb +1 -1
  4. data/lib/treevisitor.rb +1 -38
  5. data/lib/treevisitor_cli.rb +2 -4
  6. data/treevisitor.gemspec +16 -28
  7. metadata +11 -143
  8. data/.gemtest +0 -0
  9. data/bin/tree.rb +0 -9
  10. data/examples/directory_walker/directory_without_subdirectory.rb +0 -37
  11. data/examples/directory_walker/find_files.rb +0 -18
  12. data/examples/directory_walker/print_files.rb +0 -12
  13. data/examples/protovis/directory_to_json_visitor.rb +0 -15
  14. data/examples/protovis/index.html +0 -87
  15. data/examples/protovis/protovis-r3.2.js +0 -277
  16. data/examples/protovis/treevisitor.js +0 -33
  17. data/examples/protovis/treevisitor.png +0 -0
  18. data/lib/treevisitor/abs_node.rb +0 -144
  19. data/lib/treevisitor/basic_tree_node_visitor.rb +0 -44
  20. data/lib/treevisitor/cli/cli_tree.rb +0 -106
  21. data/lib/treevisitor/directory_walker.rb +0 -300
  22. data/lib/treevisitor/leaf_node.rb +0 -33
  23. data/lib/treevisitor/tree_node.rb +0 -251
  24. data/lib/treevisitor/tree_node_visitor.rb +0 -119
  25. data/lib/treevisitor/util/dir_processor.rb +0 -46
  26. data/lib/treevisitor/version.rb +0 -4
  27. data/lib/treevisitor/visitors/block_tree_node_visitor.rb +0 -21
  28. data/lib/treevisitor/visitors/build_dir_tree_visitor.rb +0 -57
  29. data/lib/treevisitor/visitors/callback_tree_node_visitor2.rb +0 -48
  30. data/lib/treevisitor/visitors/clone_tree_node_visitor.rb +0 -39
  31. data/lib/treevisitor/visitors/depth_tree_node_visitor.rb +0 -27
  32. data/lib/treevisitor/visitors/directory_to_hash_visitor.rb +0 -33
  33. data/lib/treevisitor/visitors/flat_print_tree_node_visitors.rb +0 -20
  34. data/lib/treevisitor/visitors/print_dir_tree_visitor.rb +0 -21
  35. data/lib/treevisitor/visitors/print_tree_node_visitor.rb +0 -40
  36. data/spec/fixtures/test_dir_1/.dir_with_dot/dummy.txt +0 -0
  37. data/spec/fixtures/test_dir_1/dir.1/dir.1.2/file.1.2.1 +0 -0
  38. data/spec/fixtures/test_dir_1/dir.1/file.1.1 +0 -0
  39. data/spec/fixtures/test_dir_1/dir.2/file.2.1 +0 -0
  40. data/spec/fixtures/test_dir_2/[Dsube]/sub/.gitkeep +0 -0
  41. data/spec/spec_helper.rb +0 -28
  42. data/spec/treevisitor/cli/cli_tree_spec.rb +0 -69
  43. data/spec/treevisitor/directory_walker_spec.rb +0 -99
  44. data/spec/treevisitor/tree_dsl_spec.rb +0 -57
  45. data/spec/treevisitor/tree_dsl_with_derived_class1_spec.rb +0 -53
  46. data/spec/treevisitor/tree_dsl_with_derived_class_spec.rb +0 -51
  47. data/spec/treevisitor/tree_node_paths_spec.rb +0 -70
  48. data/spec/treevisitor/tree_node_spec.rb +0 -135
  49. data/spec/treevisitor/tree_node_visitor_delegate_spec.rb +0 -35
  50. data/spec/treevisitor/tree_node_visitor_dsl_spec.rb +0 -66
  51. data/spec/treevisitor/util/dir_processor_spec.rb +0 -13
  52. data/spec/treevisitor/visitors/block_tree_node_visitor_spec.rb +0 -25
  53. data/spec/treevisitor/visitors/callback_tree_node_visitor2_spec.rb +0 -38
  54. data/spec/treevisitor/visitors/depth_tree_node_visitor_spec.rb +0 -28
  55. data/spec/treevisitor/visitors/tree_node_visitors_spec.rb +0 -27
  56. data/tasks/rspec.rake +0 -34
  57. data/tasks/yard.rake +0 -36
@@ -1,33 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- module TreeVisitor
3
-
4
- #
5
- # Represent a LeafNode
6
- #
7
- class LeafNode < AbsNode
8
-
9
- #
10
- # @param [Object] content of node
11
- #
12
- def initialize( content, parent = nil )
13
- super( content )
14
- parent.add_leaf(self) if parent
15
- end
16
-
17
- #
18
- # @return false because a leaf_node cannot be a root
19
- #
20
- def root?
21
- false
22
- end
23
-
24
- #
25
- # @return [TreeNodeVisitor] the visitor
26
- #
27
- def accept( visitor )
28
- visitor.visit_leaf( self )
29
- visitor
30
- end
31
-
32
- end
33
- end
@@ -1,251 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- module TreeVisitor
3
- #
4
- # TreeNode can contains other TreeNode (children)
5
- # and can contains LeafNode (leaves)
6
- #
7
- # TreeNode @children -1---n-> TreeNode
8
- # @leaves -1---n-> LeafNode
9
- #
10
- class TreeNode < AbsNode
11
-
12
- class << self
13
-
14
- def create(class1 = TreeNode, class2 = LeafNode, &block)
15
- if class1.ancestors.include?(TreeNode) and class2.ancestors.include?(LeafNode)
16
- @tree_node_class = class1
17
- @leaf_node_class = class2
18
- elsif class1.ancestors.include?(LeafNode) and class2 == LeafNode
19
- @tree_node_class = self
20
- @leaf_node_class = class1
21
- end
22
-
23
- if @tree_node_class.nil? || @leaf_node_class.nil?
24
- raise "Must be specified class derived from TreeNode and LeafNode"
25
- end
26
-
27
- @scope_stack = []
28
- class_eval(&block)
29
- end
30
-
31
- private
32
-
33
- def node(*args, &block)
34
- parent_node = @scope_stack.length > 0 ? @scope_stack[-1] : nil
35
- args << parent_node
36
- tree_node = @tree_node_class.new(*args)
37
- @scope_stack.push tree_node
38
- if block
39
- if block.arity == 0 || block.arity == -1
40
- class_eval(&block)
41
- elsif block.arity == 1
42
- new_block = Proc.new { block.call(tree_node) }
43
- class_eval(&new_block)
44
- else
45
- raise "block take too much arguments #{block.arity}"
46
- end
47
- end
48
- @scope_stack.pop
49
- end
50
-
51
- def leaf(*args, &block)
52
- tree_node = @scope_stack[-1]
53
- args << tree_node
54
- leaf_node = @leaf_node_class.new(*args)
55
- if block
56
- if block.arity == 0 || block.arity == -1
57
- class_eval(&block)
58
- elsif block.arity == 1
59
- new_block = Proc.new { block.call(leaf_node) }
60
- class_eval(&new_block)
61
- else
62
- raise "block take too much arguments #{block.arity}"
63
- end
64
- end
65
- leaf_node
66
- end
67
- end
68
-
69
- attr_reader :leaves
70
- attr_reader :children
71
-
72
- #
73
- # @param [Object] content of this node
74
- #
75
- def initialize(content, parent = nil)
76
- @leaves = []
77
- @children = []
78
- super(content)
79
- parent.add_child(self) if parent
80
- end
81
-
82
- #
83
- # Test if is a root
84
- #
85
- def root?
86
- @parent.nil?
87
- end
88
-
89
- #
90
- # invalidate cached info
91
- # invalidate propagates form parent to children and leaves
92
- #
93
- def invalidate
94
- super
95
- @children.each { |c| c.invalidate }
96
- @leaves.each { |l| l.invalidate }
97
- end
98
-
99
- #
100
- # @return [FixNum] total number of nodes
101
- #
102
- def nr_nodes
103
- nr = @leaves.length + @children.length
104
- @children.inject(nr) { |sum, c| sum + c.nr_nodes }
105
- end
106
-
107
- #
108
- # @return [FixNum] total number of leaves
109
- #
110
- def nr_leaves
111
- @leaves.length + @children.inject(0) { |sum, child| sum + child.nr_leaves }
112
- end
113
-
114
- #
115
- # @return [FixNum] total number of children
116
- #
117
- def nr_children
118
- @children.length + @children.inject(0) { |sum, child| sum + child.nr_children }
119
- end
120
-
121
- #
122
- # Add a Leaf
123
- # @param [LeafNode]
124
- #
125
- # @return self
126
- #
127
- def add_leaf(leaf)
128
- return if leaf.parent == self
129
- if not leaf.parent.nil?
130
- leaf.remove_from_parent
131
- end
132
- leaf.parent = self
133
- if @leaves.length > 0
134
- @leaves.last.next = leaf
135
- leaf.prev = @leaves.last
136
- else
137
- leaf.prev = nil
138
- end
139
- leaf.next = nil
140
- leaf.invalidate
141
- @leaves << leaf
142
- self
143
- end
144
-
145
- #
146
- # Add a Tree
147
- # @param [LeafNode]
148
- #
149
- # @return self
150
- #
151
- def add_child(tree_node)
152
- return if tree_node.parent == self
153
- if not tree_node.parent.nil?
154
- tree_node.remove_from_parent
155
- else
156
- tree_node.prefix_path = nil
157
- end
158
- tree_node.invalidate
159
- tree_node.parent = self
160
- if @children.length > 0
161
- @children.last.next = tree_node
162
- tree_node.prev = @children.last
163
- else
164
- tree_node.prev = nil
165
- end
166
- tree_node.next = nil
167
- @children << tree_node
168
- self
169
- end
170
-
171
- #
172
- # Find a node down the hierarchy with content
173
- # @param [Object,Regexp] content of searched node
174
- # @return [Object, nil] nil if not found
175
- #
176
- def find(content = nil, &block)
177
- if content and block_given?
178
- raise "TreeNode::find - passed content AND block"
179
- end
180
-
181
- if content
182
- if content.class == Regexp
183
- block = proc { |l| l.content =~ content }
184
- else
185
- block = proc { |l| l.content == content }
186
- end
187
- end
188
- return self if block.call(self)
189
-
190
- leaf = @leaves.find { |l| block.call(l) }
191
- return leaf if leaf
192
-
193
- @children.each do |child|
194
- node = child.find &block
195
- return node if node
196
- end
197
- nil
198
- end
199
-
200
- #
201
- # return the visitor
202
- #
203
- def accept(visitor)
204
- visitor.enter_node(self)
205
- @leaves.each { |leaf|
206
- leaf.accept(visitor)
207
- }
208
- @children.each { |child|
209
- child.accept(visitor)
210
- }
211
- visitor.exit_node(self)
212
- visitor
213
- end
214
-
215
- #
216
- # Format the content of tree
217
- #
218
- def to_str(prefix= "")
219
- str = ""
220
-
221
- if root?
222
- str << to_s << "\n"
223
- else
224
- str << prefix
225
- if self.next
226
- str << '|-- '
227
- else
228
- str << '`-- '
229
- end
230
- str << to_s << "\n"
231
- prefix += self.next ? "| " : " "
232
- end
233
-
234
- @leaves.each do |leaf|
235
- str << prefix
236
- if !leaf.next.nil? or !@children.empty?
237
- str << '|-- '
238
- else
239
- str << '`-- '
240
- end
241
- str << leaf.to_s << "\n"
242
- end
243
-
244
- @children.each do |child|
245
- str << child.to_str(prefix)
246
- end
247
- str
248
- end
249
-
250
- end
251
- end
@@ -1,119 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- module TreeVisitor
3
-
4
- #
5
- # More complex TreeNodeVisitor, define a simple dsl
6
- # @example
7
- # TreeNodeVisitor.new do
8
- # on_enter_node do |node|
9
- # puts "hello #{node}"
10
- # end
11
- # on_exit_node do |node|
12
- # puts "bye #{node}"
13
- # end
14
- # on_leaf do |leaf|
15
- # puts "how you do #{leaf}"
16
- # end
17
- # end
18
- #
19
- class TreeNodeVisitor
20
-
21
- def initialize(delegate = nil, &block)
22
- @on_enter_tree_node_blocks = []
23
- @on_exit_tree_node_blocks = []
24
- @on_visit_leaf_node_blocks = []
25
- @stack = []
26
- @root = nil
27
- @delegate = delegate
28
- if block
29
- instance_eval(&block)
30
- end
31
- end
32
-
33
- #
34
- # called on tree node at start of the visit i.e. we start to visit the subtree
35
- #
36
- def enter_node(tree_node)
37
- parent = @stack.last
38
- if @delegate
39
- @delegate.enter_node(tree_node) if @delegate.respond_to? :enter_node
40
- else
41
- @on_enter_tree_node_blocks.each do |b|
42
- if b.arity == 1
43
- b.call(tree_node)
44
- elsif b.arity == 2
45
- b.call(tree_node, parent)
46
- end
47
- end
48
- end
49
- @root = tree_node if @stack.empty?
50
- @stack.push(tree_node)
51
- end
52
-
53
- #
54
- # called on tree node at end of the visit i.e. oll subtree are visited
55
- #
56
- def exit_node(tree_node)
57
- parent = @stack.last
58
- if @delegate
59
- @delegate.exit_node(tree_node) if @delegate.respond_to? :exit_node
60
- else
61
- @on_exit_tree_node_blocks.each do |b|
62
- if b.arity == 1
63
- b.call(tree_node)
64
- elsif b.arity == 2
65
- b.call(tree_node, parent)
66
- end
67
- end
68
- end
69
- @stack.pop
70
- end
71
-
72
- #
73
- # called when visit leaf node
74
- #
75
- def visit_leaf(leaf_node)
76
- parent = @stack.last
77
- if @delegate
78
- @delegate.visit_leaf(leaf_node) if @delegate.respond_to? :visit_leaf
79
- else
80
- @on_visit_leaf_node_blocks.each do |b|
81
- if b.arity == 1
82
- b.call(leaf_node)
83
- elsif b.arity == 2
84
- b.call(leaf_node, parent)
85
- end
86
- end
87
- end
88
- end
89
-
90
- #
91
- # add a block to be called when entering into a tree_node
92
- #
93
- def on_enter_node(&block)
94
- raise "already defined a delegate" if @delegate
95
- raise "block missing" unless block
96
- @on_enter_tree_node_blocks << block
97
- end
98
-
99
- #
100
- # add a block to be called when exiting from a TreeNode
101
- #
102
- def on_exit_node(&block)
103
- raise "already defined a delegate" if @delegate
104
- raise "block missing" unless block
105
- @on_exit_tree_node_blocks << block
106
- end
107
-
108
- #
109
- # add a block to be called when visiting a leaf node
110
- #
111
- def on_leaf(&block)
112
- raise "already defined a delegate" if @delegate
113
- raise "block missing" unless block
114
- @on_visit_leaf_node_blocks << block
115
- end
116
-
117
- end
118
-
119
- end
@@ -1,46 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- module TreeVisitor
3
-
4
- #
5
- # Visit a directory tree
6
- # not TreeNode related
7
- #
8
- class DirProcessor
9
-
10
- def initialize( &action )
11
- @processors = {}
12
- @default_processor = action
13
- end
14
-
15
- def add_processor( re, &action )
16
- @processors[ re ] = action
17
- end
18
-
19
- def process( dirname )
20
- @dirname = dirname
21
- old_dirname = Dir.pwd
22
- Dir.chdir( @dirname )
23
- Dir["**/*"].each { |f|
24
- pn = Pathname.new( f ).expand_path
25
- # puts "#{self.class.name}#loadfromdir #{f}"
26
- next if pn.directory?
27
- process_file( pn )
28
- }
29
- Dir.chdir( old_dirname )
30
- self
31
- end
32
-
33
- private
34
-
35
- def process_file( pn )
36
- # puts "file: #{f}"
37
- pair = @processors.find { |re,action| re =~ pn.to_s }
38
- unless pair.nil?
39
- pair[1].call( pn )
40
- else
41
- @default_processor.call( pn ) if @default_processor
42
- end
43
- end
44
-
45
- end
46
- end