Module: RecursivelyEnumerable
- Included in:
- Ole::Storage::Dirent
- Defined in:
- lib/ole/support.rb
Overview
Include this module into a class that defines #each_child. It should maybe use #each instead, but its easier to be more specific, and use an alias.
I don’t want to force the class to cache children (eg where children are loaded on request in pst), because that forces the whole tree to be loaded. So, the methods should only call #each_child once, and breadth first iteration holds its own copy of the children around.
Main methods are #recursive, and #to_tree
Instance Method Summary collapse
- #each_recursive(mode = :depth_first) {|_self| ... } ⇒ Object
- #each_recursive_breadth_first(&block) ⇒ Object
- #each_recursive_depth_first(&block) ⇒ Object
-
#recursive(mode = :depth_first) ⇒ Object
the idea of this function, is to allow use of regular Enumerable methods in a recursive fashion.
-
#to_tree(io = nil, method = :inspect) ⇒ Object
streams a “tree” form of the recursively enumerable structure to
io
, or return a string form instead ifio
is not specified.
Instance Method Details
#each_recursive(mode = :depth_first) {|_self| ... } ⇒ Object
111 112 113 114 115 |
# File 'lib/ole/support.rb', line 111 def each_recursive mode=:depth_first, &block # we always actually yield ourself (the tree root) before recursing yield self send "each_recursive_#{mode}", &block end |
#each_recursive_breadth_first(&block) ⇒ Object
102 103 104 105 106 107 108 109 |
# File 'lib/ole/support.rb', line 102 def each_recursive_breadth_first(&block) children = [] each_child do |child| children << child if child.respond_to? :each_recursive_breadth_first yield child end children.each { |child| child.each_recursive_breadth_first(&block) } end |
#each_recursive_depth_first(&block) ⇒ Object
93 94 95 96 97 98 99 100 |
# File 'lib/ole/support.rb', line 93 def each_recursive_depth_first(&block) each_child do |child| yield child if child.respond_to? :each_recursive_depth_first child.each_recursive_depth_first(&block) end end end |
#recursive(mode = :depth_first) ⇒ Object
the idea of this function, is to allow use of regular Enumerable methods in a recursive fashion. eg:
# just looks at top level children
root.find { |child| child.some_condition? }
# recurse into all children getting non-folders, breadth first
root.recursive(:breadth_first).select { |child| !child.folder? }
# just get everything
items = root.recursive.to_a
127 128 129 |
# File 'lib/ole/support.rb', line 127 def recursive mode=:depth_first to_enum(:each_recursive, mode) end |
#to_tree(io = nil, method = :inspect) ⇒ Object
streams a “tree” form of the recursively enumerable structure to io
, or return a string form instead if io
is not specified.
mostly a debugging aid. can specify a different method
to call if desired, though it should return a single line string.
136 137 138 139 140 141 142 143 |
# File 'lib/ole/support.rb', line 136 def to_tree io=nil, method=:inspect unless io to_tree io = StringIO.new return io.string end io << "- #{send method}\n" to_tree_helper io, method, ' ' end |