Skip to content

Feature: walk_tree_to_children #43

Open
@ioquatix

Description

@ioquatix

I'd like to see something like this included next to TreeWalker::walk_tree:

module ActsAsTree::TreeWalker
    def self.prune
        throw :prune
    end

    def walk_tree_to_children(children, &block)
        marked = Set.new(children.collect(&:self_and_ancestors).flatten)

        walk_tree do |node, level|
            throw :prune unless marked.include?(node)

            yield node, level
        end
    end

    def walk_tree_dfs(where = {}, node = nil, level = 0, &block)
        nodes = (node.nil? ? roots : node.children).where(where)

        nodes.each do |child|
            catch(:prune) do
                yield(child, level)

                walk_tree_dfs(where, child, level + 1, &block)
            end
        end
    end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions