treevisitor 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION.yml +1 -1
- data/lib/treevisitor/tree_node_visitor.rb +20 -19
- data/lib/treevisitor/visitors/callback_tree_node_visitor.rb +45 -42
- data/lib/treevisitor/visitors/callback_tree_node_visitor2.rb +11 -15
- data/spec/treevisitor/tree_node_visitor_delegate_spec.rb +35 -0
- data/spec/treevisitor/{visitor_dsl_spec.rb → tree_node_visitor_dsl_spec.rb} +0 -0
- data/spec/treevisitor/visitors/{callback_tree_node_visitors_spec.rb → tree_node_visitors_spec.rb} +4 -4
- data/treevisitor.gemspec +7 -7
- metadata +10 -28
data/VERSION.yml
CHANGED
@@ -18,15 +18,13 @@ module TreeVisitor
|
|
18
18
|
#
|
19
19
|
class TreeNodeVisitor
|
20
20
|
|
21
|
-
def initialize(&block)
|
22
|
-
|
21
|
+
def initialize(delegate = nil, &block)
|
23
22
|
@on_enter_tree_node_blocks = []
|
24
23
|
@on_exit_tree_node_blocks = []
|
25
24
|
@on_visit_leaf_node_blocks = []
|
26
|
-
|
27
25
|
@stack = []
|
28
26
|
@root = nil
|
29
|
-
|
27
|
+
@delegate = delegate
|
30
28
|
if block
|
31
29
|
instance_eval(&block)
|
32
30
|
end
|
@@ -37,64 +35,67 @@ module TreeVisitor
|
|
37
35
|
#
|
38
36
|
def enter_node(tree_node)
|
39
37
|
parent = @stack.last
|
40
|
-
@
|
38
|
+
if @delegate
|
39
|
+
@delegate.enter_node(tree_node) if @delegate.respond_to? :enter_node
|
40
|
+
else
|
41
|
+
@on_enter_tree_node_blocks.each { |b| b.call(tree_node, parent) }
|
42
|
+
end
|
41
43
|
@root = tree_node if @stack.empty?
|
42
44
|
@stack.push(tree_node)
|
43
45
|
end
|
44
46
|
|
45
|
-
# alias :enter_tree_node :enter_node
|
46
|
-
|
47
47
|
#
|
48
48
|
# called on tree node at end of the visit i.e. oll subtree are visited
|
49
49
|
#
|
50
50
|
def exit_node(tree_node)
|
51
51
|
parent = @stack.last
|
52
|
-
@
|
52
|
+
if @delegate
|
53
|
+
@delegate.exit_node(tree_node) if @delegate.respond_to? :exit_node
|
54
|
+
else
|
55
|
+
@on_exit_tree_node_blocks.each { |b| b.call(tree_node, parent) }
|
56
|
+
end
|
53
57
|
@stack.pop
|
54
58
|
end
|
55
59
|
|
56
|
-
# alias :exit_tree_node :exit_node
|
57
|
-
|
58
60
|
#
|
59
61
|
# called when visit leaf node
|
60
62
|
#
|
61
63
|
def visit_leaf(leaf_node)
|
62
64
|
parent = @stack.last
|
63
|
-
@
|
65
|
+
if @delegate
|
66
|
+
@delegate.visit_leaf(leaf_node) if @delegate.respond_to? :visit_leaf
|
67
|
+
else
|
68
|
+
@on_visit_leaf_node_blocks.each { |b| b.call(leaf_node, parent) }
|
69
|
+
end
|
64
70
|
end
|
65
71
|
|
66
|
-
# alias :visit_leaf_node :visit_leaf
|
67
|
-
|
68
72
|
#
|
69
73
|
# add a block to be called when entering into a tree_node
|
70
74
|
#
|
71
75
|
def on_enter_node(&block)
|
76
|
+
raise "already defined a delegate" if @delegate
|
72
77
|
raise "block missing" unless block
|
73
78
|
@on_enter_tree_node_blocks << block
|
74
79
|
end
|
75
80
|
|
76
|
-
# alias :on_enter_tree_node :on_enter_node
|
77
|
-
|
78
81
|
#
|
79
82
|
# add a block to be called when exiting from a TreeNode
|
80
83
|
#
|
81
84
|
def on_exit_node(&block)
|
85
|
+
raise "already defined a delegate" if @delegate
|
82
86
|
raise "block missing" unless block
|
83
87
|
@on_exit_tree_node_blocks << block
|
84
88
|
end
|
85
89
|
|
86
|
-
# alias :on_exit_tree_node :on_exit_node
|
87
|
-
|
88
90
|
#
|
89
91
|
# add a block to be called when visiting a leaf node
|
90
92
|
#
|
91
93
|
def on_leaf(&block)
|
94
|
+
raise "already defined a delegate" if @delegate
|
92
95
|
raise "block missing" unless block
|
93
96
|
@on_visit_leaf_node_blocks << block
|
94
97
|
end
|
95
98
|
|
96
|
-
# alias :on_visit_leaf_node :on_leaf
|
97
|
-
|
98
99
|
end
|
99
100
|
|
100
101
|
end
|
@@ -1,42 +1,45 @@
|
|
1
|
-
|
2
|
-
module TreeVisitor
|
3
|
-
#
|
4
|
-
# Executes a block when enter in a node
|
5
|
-
# The block are defined from on_enter_X methods
|
6
|
-
# The blocks take as argument only the node
|
7
|
-
#
|
8
|
-
class CallbackTreeNodeVisitor
|
9
|
-
|
10
|
-
def initialize
|
11
|
-
@root
|
12
|
-
@stack
|
13
|
-
@action_enter_tree_node = nil
|
14
|
-
@action_visit_leaf_node = nil
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
@
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
end
|
1
|
+
## -*- coding: utf-8 -*-
|
2
|
+
#module TreeVisitor
|
3
|
+
# #
|
4
|
+
# # Executes a block when enter in a node
|
5
|
+
# # The block are defined from on_enter_X methods
|
6
|
+
# # The blocks take as argument only the node
|
7
|
+
# #
|
8
|
+
# class CallbackTreeNodeVisitor
|
9
|
+
#
|
10
|
+
# def initialize(delegate = nil)
|
11
|
+
# @root = nil
|
12
|
+
# @stack = []
|
13
|
+
# @action_enter_tree_node = nil
|
14
|
+
# @action_visit_leaf_node = nil
|
15
|
+
# @delegate = delegate
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# def on_enter_node(&block)
|
19
|
+
# raise "already defined a delegate" if @delegate
|
20
|
+
# @action_enter_tree_node = block
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# def on_visit_leaf(&block)
|
24
|
+
# raise "already defined a delegate" if @delegate
|
25
|
+
# @action_visit_leaf_node = block
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# def enter_node(tree_node)
|
29
|
+
# @root = tree_node if @stack.empty?
|
30
|
+
# @stack.push(tree_node)
|
31
|
+
# @action_enter_tree_node.call(tree_node) if @action_enter_tree_node
|
32
|
+
# @delegate.enter_node(tree_node) if @delegate
|
33
|
+
# end
|
34
|
+
#
|
35
|
+
# def exit_node(tree_node)
|
36
|
+
# @stack.pop
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
# def visit_leaf(leaf_node)
|
40
|
+
# @action_visit_leaf_node.call(leaf_node) if @action_visit_leaf_node
|
41
|
+
# @delegate.visit_leaf(leaf_node) if @delegate
|
42
|
+
# end
|
43
|
+
#
|
44
|
+
# end
|
45
|
+
#end
|
@@ -5,7 +5,7 @@ module TreeVisitor
|
|
5
5
|
# The block are defined from on_enter_X methods
|
6
6
|
# The blocks take as argument the node and the parent_node
|
7
7
|
#
|
8
|
-
class CallbackTreeNodeVisitor2
|
8
|
+
class CallbackTreeNodeVisitor2
|
9
9
|
|
10
10
|
attr_reader :root
|
11
11
|
|
@@ -16,21 +16,20 @@ module TreeVisitor
|
|
16
16
|
@delegate = delegate
|
17
17
|
end
|
18
18
|
|
19
|
-
def on_enter_node(&
|
20
|
-
|
19
|
+
def on_enter_node(&block)
|
20
|
+
raise "already defined a delegate" if @delegate
|
21
|
+
@action_enter_tree_node = block
|
21
22
|
end
|
22
23
|
|
23
|
-
def on_visit_leaf(&
|
24
|
-
|
24
|
+
def on_visit_leaf(&block)
|
25
|
+
raise "already defined a delegate" if @delegate
|
26
|
+
@action_visit_leaf_node = block
|
25
27
|
end
|
26
28
|
|
27
29
|
def enter_node(src_tree_node)
|
28
30
|
dst_parent_node = @stack.empty? ? nil : @stack.last
|
29
|
-
if @action_enter_tree_node
|
30
|
-
|
31
|
-
elsif @delegate
|
32
|
-
dst_tree_node = @delegate.on_enter_tree_node(src_tree_node, dst_parent_node)
|
33
|
-
end
|
31
|
+
dst_tree_node = @action_enter_tree_node.call(src_tree_node, dst_parent_node) if @action_enter_tree_node
|
32
|
+
dst_tree_node = @delegate.enter_node(src_tree_node, dst_parent_node) if @delegate and @delegate.respond_to? :enter_node
|
34
33
|
@root = dst_tree_node if @stack.empty?
|
35
34
|
@stack.push(dst_tree_node)
|
36
35
|
end
|
@@ -41,11 +40,8 @@ module TreeVisitor
|
|
41
40
|
|
42
41
|
def visit_leaf(src_leaf_node)
|
43
42
|
parent_node = @stack.last
|
44
|
-
if @action_visit_leaf_node
|
45
|
-
|
46
|
-
elsif @delegate
|
47
|
-
@delegate.on_visit_leaf_node(src_leaf_node, parent_node)
|
48
|
-
end
|
43
|
+
@action_visit_leaf_node.call(src_leaf_node, parent_node) if @action_visit_leaf_node
|
44
|
+
@delegate.visit_leaf(src_leaf_node, parent_node) if @delegate and @delegate.respond_to? :visit_leaf
|
49
45
|
end
|
50
46
|
end
|
51
47
|
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require File.join(File.dirname(__FILE__), "..", "spec_helper")
|
3
|
+
|
4
|
+
describe TreeNodeVisitor do
|
5
|
+
|
6
|
+
class Delegate
|
7
|
+
def enter_node(node)
|
8
|
+
@entered_node= true
|
9
|
+
end
|
10
|
+
|
11
|
+
def exit_node(node)
|
12
|
+
@exit_node= true
|
13
|
+
end
|
14
|
+
|
15
|
+
def visit_leaf(leaf)
|
16
|
+
@visited_leaf= true
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should use a delegate" do
|
21
|
+
|
22
|
+
delegate = Delegate.new
|
23
|
+
visitor = TreeNodeVisitor.new(delegate)
|
24
|
+
|
25
|
+
visitor.enter_node(nil)
|
26
|
+
delegate.instance_eval{ @entered_node }.should be_true
|
27
|
+
|
28
|
+
visitor.exit_node(nil)
|
29
|
+
delegate.instance_eval{ @exit_node }.should be_true
|
30
|
+
|
31
|
+
visitor.visit_leaf(nil)
|
32
|
+
delegate.instance_eval{ @visited_leaf }.should be_true
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
File without changes
|
data/spec/treevisitor/visitors/{callback_tree_node_visitors_spec.rb → tree_node_visitors_spec.rb}
RENAMED
@@ -1,7 +1,7 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
2
|
require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
|
3
3
|
|
4
|
-
describe
|
4
|
+
describe TreeNodeVisitor do
|
5
5
|
|
6
6
|
before do
|
7
7
|
ta = TreeNode.new( "a", nil )
|
@@ -14,11 +14,11 @@ describe "Tree Node Visitors" do
|
|
14
14
|
@tree = ta
|
15
15
|
end
|
16
16
|
|
17
|
-
it
|
17
|
+
it TreeNodeVisitor do
|
18
18
|
accumulator = []
|
19
|
-
visitor =
|
19
|
+
visitor = TreeNodeVisitor.new
|
20
20
|
visitor.on_enter_node{ |tree_node| accumulator << tree_node.content }
|
21
|
-
visitor.
|
21
|
+
visitor.on_leaf{ |leaf_node| accumulator << leaf_node.content }
|
22
22
|
@tree.accept( visitor )
|
23
23
|
accumulator.length.should == 5
|
24
24
|
accumulator.should == %w{ a 1 2 b 3 }
|
data/treevisitor.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{treevisitor}
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Tokiro"]
|
12
|
-
s.date = %q{2011-02-
|
12
|
+
s.date = %q{2011-02-06}
|
13
13
|
s.default_executable = %q{tree.rb}
|
14
14
|
s.description = %q{ Implementation of visitor design pattern. It contains a 'tree.rb'
|
15
15
|
command line clone of the tree unix tool.
|
@@ -61,7 +61,7 @@ Gem::Specification.new do |s|
|
|
61
61
|
]
|
62
62
|
s.homepage = %q{http://github.com/tokiro/treevisitor}
|
63
63
|
s.require_paths = ["lib"]
|
64
|
-
s.rubygems_version = %q{1.
|
64
|
+
s.rubygems_version = %q{1.5.0}
|
65
65
|
s.summary = %q{Implementation of visitor design pattern}
|
66
66
|
s.test_files = [
|
67
67
|
"spec/fixtures/test_dir_1/.dir_with_dot/dummy.txt",
|
@@ -76,16 +76,16 @@ Gem::Specification.new do |s|
|
|
76
76
|
"spec/treevisitor/tree_dsl_with_derived_class_spec.rb",
|
77
77
|
"spec/treevisitor/tree_node_paths_spec.rb",
|
78
78
|
"spec/treevisitor/tree_node_spec.rb",
|
79
|
+
"spec/treevisitor/tree_node_visitor_delegate_spec.rb",
|
80
|
+
"spec/treevisitor/tree_node_visitor_dsl_spec.rb",
|
79
81
|
"spec/treevisitor/util/dir_processor_spec.rb",
|
80
|
-
"spec/treevisitor/visitor_dsl_spec.rb",
|
81
82
|
"spec/treevisitor/visitors/block_tree_node_visitor_spec.rb",
|
82
83
|
"spec/treevisitor/visitors/callback_tree_node_visitor2_spec.rb",
|
83
|
-
"spec/treevisitor/visitors/
|
84
|
-
"spec/treevisitor/visitors/
|
84
|
+
"spec/treevisitor/visitors/depth_tree_node_visitor_spec.rb",
|
85
|
+
"spec/treevisitor/visitors/tree_node_visitors_spec.rb"
|
85
86
|
]
|
86
87
|
|
87
88
|
if s.respond_to? :specification_version then
|
88
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
89
89
|
s.specification_version = 3
|
90
90
|
|
91
91
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
metadata
CHANGED
@@ -1,12 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: treevisitor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 2
|
8
|
-
- 0
|
9
|
-
version: 0.2.0
|
4
|
+
prerelease:
|
5
|
+
version: 0.2.1
|
10
6
|
platform: ruby
|
11
7
|
authors:
|
12
8
|
- Tokiro
|
@@ -14,7 +10,7 @@ autorequire:
|
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
12
|
|
17
|
-
date: 2011-02-
|
13
|
+
date: 2011-02-06 00:00:00 +01:00
|
18
14
|
default_executable: tree.rb
|
19
15
|
dependencies:
|
20
16
|
- !ruby/object:Gem::Dependency
|
@@ -24,8 +20,6 @@ dependencies:
|
|
24
20
|
requirements:
|
25
21
|
- - ">="
|
26
22
|
- !ruby/object:Gem::Version
|
27
|
-
segments:
|
28
|
-
- 0
|
29
23
|
version: "0"
|
30
24
|
type: :runtime
|
31
25
|
prerelease: false
|
@@ -37,8 +31,6 @@ dependencies:
|
|
37
31
|
requirements:
|
38
32
|
- - ">="
|
39
33
|
- !ruby/object:Gem::Version
|
40
|
-
segments:
|
41
|
-
- 0
|
42
34
|
version: "0"
|
43
35
|
type: :development
|
44
36
|
prerelease: false
|
@@ -50,8 +42,6 @@ dependencies:
|
|
50
42
|
requirements:
|
51
43
|
- - ">="
|
52
44
|
- !ruby/object:Gem::Version
|
53
|
-
segments:
|
54
|
-
- 0
|
55
45
|
version: "0"
|
56
46
|
type: :development
|
57
47
|
prerelease: false
|
@@ -63,8 +53,6 @@ dependencies:
|
|
63
53
|
requirements:
|
64
54
|
- - ">="
|
65
55
|
- !ruby/object:Gem::Version
|
66
|
-
segments:
|
67
|
-
- 0
|
68
56
|
version: "0"
|
69
57
|
type: :development
|
70
58
|
prerelease: false
|
@@ -76,8 +64,6 @@ dependencies:
|
|
76
64
|
requirements:
|
77
65
|
- - ">="
|
78
66
|
- !ruby/object:Gem::Version
|
79
|
-
segments:
|
80
|
-
- 0
|
81
67
|
version: "0"
|
82
68
|
type: :development
|
83
69
|
prerelease: false
|
@@ -89,8 +75,6 @@ dependencies:
|
|
89
75
|
requirements:
|
90
76
|
- - ">="
|
91
77
|
- !ruby/object:Gem::Version
|
92
|
-
segments:
|
93
|
-
- 0
|
94
78
|
version: "0"
|
95
79
|
type: :development
|
96
80
|
prerelease: false
|
@@ -154,12 +138,13 @@ files:
|
|
154
138
|
- spec/treevisitor/tree_dsl_with_derived_class_spec.rb
|
155
139
|
- spec/treevisitor/tree_node_paths_spec.rb
|
156
140
|
- spec/treevisitor/tree_node_spec.rb
|
141
|
+
- spec/treevisitor/tree_node_visitor_delegate_spec.rb
|
142
|
+
- spec/treevisitor/tree_node_visitor_dsl_spec.rb
|
157
143
|
- spec/treevisitor/util/dir_processor_spec.rb
|
158
|
-
- spec/treevisitor/visitor_dsl_spec.rb
|
159
144
|
- spec/treevisitor/visitors/block_tree_node_visitor_spec.rb
|
160
145
|
- spec/treevisitor/visitors/callback_tree_node_visitor2_spec.rb
|
161
|
-
- spec/treevisitor/visitors/callback_tree_node_visitors_spec.rb
|
162
146
|
- spec/treevisitor/visitors/depth_tree_node_visitor_spec.rb
|
147
|
+
- spec/treevisitor/visitors/tree_node_visitors_spec.rb
|
163
148
|
- bin/tree.rb
|
164
149
|
has_rdoc: true
|
165
150
|
homepage: http://github.com/tokiro/treevisitor
|
@@ -175,21 +160,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
175
160
|
requirements:
|
176
161
|
- - ">="
|
177
162
|
- !ruby/object:Gem::Version
|
178
|
-
segments:
|
179
|
-
- 0
|
180
163
|
version: "0"
|
181
164
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
182
165
|
none: false
|
183
166
|
requirements:
|
184
167
|
- - ">="
|
185
168
|
- !ruby/object:Gem::Version
|
186
|
-
segments:
|
187
|
-
- 0
|
188
169
|
version: "0"
|
189
170
|
requirements: []
|
190
171
|
|
191
172
|
rubyforge_project:
|
192
|
-
rubygems_version: 1.
|
173
|
+
rubygems_version: 1.5.0
|
193
174
|
signing_key:
|
194
175
|
specification_version: 3
|
195
176
|
summary: Implementation of visitor design pattern
|
@@ -206,9 +187,10 @@ test_files:
|
|
206
187
|
- spec/treevisitor/tree_dsl_with_derived_class_spec.rb
|
207
188
|
- spec/treevisitor/tree_node_paths_spec.rb
|
208
189
|
- spec/treevisitor/tree_node_spec.rb
|
190
|
+
- spec/treevisitor/tree_node_visitor_delegate_spec.rb
|
191
|
+
- spec/treevisitor/tree_node_visitor_dsl_spec.rb
|
209
192
|
- spec/treevisitor/util/dir_processor_spec.rb
|
210
|
-
- spec/treevisitor/visitor_dsl_spec.rb
|
211
193
|
- spec/treevisitor/visitors/block_tree_node_visitor_spec.rb
|
212
194
|
- spec/treevisitor/visitors/callback_tree_node_visitor2_spec.rb
|
213
|
-
- spec/treevisitor/visitors/callback_tree_node_visitors_spec.rb
|
214
195
|
- spec/treevisitor/visitors/depth_tree_node_visitor_spec.rb
|
196
|
+
- spec/treevisitor/visitors/tree_node_visitors_spec.rb
|