DBIx::Tree::MaterializedPath::TreeRepresentation - data structure for "materialized path" trees
Version 0.06
# Row data must be sorted by path: my $column_names = ['id', 'path', 'name']; my $subtree_data = [ [ 2, "1.1", "a"], [ 3, "1.2", "b"], [ 4, "1.3", "c"], [ 5, "1.3.1", "d"], [ 7, "1.3.1.1", "e"], [ 6, "1.3.2", "f"], ]; my $subtree_representation = DBIx::Tree::MaterializedPath::TreeRepresentation->new($node, $column_names, $subtree_data); $subtree_representation->traverse($coderef, $context);
This module implements a data structure that represents a tree (or subtree) as stored in the database.
Note: Normally these objects would not be created independently - call get_descendants() on a tree or a node to get its descendants as a DBIx::Tree::MaterializedPath::TreeRepresentation object, and then traverse() those descendants.
$subtree_data = DBIx::Tree::MaterializedPath::TreeRepresentation->new($node, $cols_listref, $rows_listref, $options_hashref);
new() expects a DBIx::Tree::MaterializedPath::Node object (representing the node that this data belongs to), a listref of database column names, and a listref of listrefs, each of which represents a node row in the database.
new()
At minimum, each row must contain entries for the id_column_name and the path_column_name as specified in the DBIx::Tree::MaterializedPath constructor. The rows should be sorted by path in ascending order.
Additionally, the row may contain entries for any metadata columns which are stored with the nodes.
One DBIx::Tree::MaterializedPath::Node object will be created in the data structure for each input row. If the optional parameters hashref contains a true value for "ignore_empty_hash", and if no metadata entries exist in the input row, then the node object's metadata will not be populated, and will only be retrieved from the database when the data() method is called on a given node.
$subtree_data->has_nodes()
Return true if the data structure contains any nodes.
$subtree_data->num_nodes()
Return the number of nodes in the data structure.
$subtree_data->traverse( $coderef, $optional_context )
Given a coderef, traverse down the data structure in leftmost depth-first order and apply the coderef at each node.
The first argument to the $coderef will be the node being traversed. The second argument to the $coderef will be that node's parent.
If supplied, $context will be the third argument to the coderef. $context can be a reference to a data structure that can allow information to be carried along from node to node while traversing the tree.
E.g. to count the number of descendants:
my $context = {count => 0}; my $coderef = sub { my ($node, $parent, $context) = @_; $context->{count}++; }; my $descendants = $node->get_descendants(); $descendants->traverse($coderef, $context); print "The node has $context->{count} descendants.\n";
Note that you may be able to use closure variables instead of passing them along in $context:
my $count = 0; my $coderef = sub { my ($node, $parent) = @_; $count++; }; my $descendants = $node->get_descendants(); $descendants->traverse($coderef, $context); print "The node has $count descendants.\n";
DBIx::Tree::MaterializedPath
DBIx::Tree::MaterializedPath::Node
DBIx::Tree::MaterializedPath::PathMapper
Please report any bugs or feature requests to bug-dbix-tree-materializedpath at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBIx-Tree-MaterializedPath. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-dbix-tree-materializedpath at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc DBIx::Tree::MaterializedPath
You can also look for information at:
RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Tree-MaterializedPath
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/DBIx-Tree-MaterializedPath
CPAN Ratings
http://cpanratings.perl.org/d/DBIx-Tree-MaterializedPath
Search CPAN
http://search.cpan.org/dist/DBIx-Tree-MaterializedPath
Larry Leszczynski, <larryl at cpan.org>
<larryl at cpan.org>
Copyright 2008 Larry Leszczynski, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install DBIx::Tree::MaterializedPath, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DBIx::Tree::MaterializedPath
CPAN shell
perl -MCPAN -e shell install DBIx::Tree::MaterializedPath
For more information on module installation, please visit the detailed CPAN module installation guide.