Class: Yuzu::Core::Visitor
Overview
Visitor is a convenience mechanism for traversing a tree structure. To be traversable, objects in the data structure must have the following interface defined:
def children
return an array of nodes or nil
end
WebsiteBase and Path have this interface, so are traversable. Given a Proc that returns a boolean, we can limit the operation performed by the visitor to a subset of available nodes.
Usage:
filter_for_files_only = proc {|c| c.file?}
v = Visitor.new(filter_for_files_only)
v.traverse(root) do |child|
# Do something with child.
end
Each node passing the ‘filter_for_files_only` filter will be executed in the given block.
Instance Method Summary collapse
-
#initialize(filter = nil) ⇒ Visitor
constructor
Create a visitor.
- #traverse(container, &block) ⇒ Object
Constructor Details
#initialize(filter = nil) ⇒ Visitor
Create a visitor
28 29 30 31 |
# File 'lib/yuzu/core/visitor.rb', line 28 def initialize(filter=nil) #raise "Visitor must be given a Proc." if not filter.is_a?(Proc) @filter = filter.nil? ? proc {|c| true} : filter end |
Instance Method Details
#traverse(container, &block) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/yuzu/core/visitor.rb', line 33 def traverse(container, &block) return if container.children.nil? container.children.each do |child| block.call(child) if @filter.call(child) self.traverse(child, &block) if not child.children.nil? end end |