Many methods have alternative names, e.g. "left_siblings" and "previous_siblings"
This is in deference to the DBIx::Class::Ordered module which uses terms "previous" "next" "first" and
"last".
Similarly DBIx::Class::Tree::AdjacencyList::Ordered uses terms "append", "prepend", "before" and "after"
However, my preference to use terms "left" and "right" consistently when using this module. However, the
other names are available if you are more familiar with those modules.
tree_columns
__PACKAGE__->tree_columns({
left_column => 'lft',
right_column => 'rgt',
root_column => 'root_id',
level_column => 'level',
});
Declare the name of the columns defined in the database schema.
None of these columns should be modified outside if this module. left_column and right_column are
unlikely to be of any use to your application. They should be integer fields.
Multiple trees are allowed in the same table, each tree will have a unique value in the root_column. In
the current implementation this should be an integer field
The level_column may be of use in your application, it defines the depth of each node in the tree (with
the root at level zero).
create
my $tree = $schema->resultset('My::Department')->create({
name = 'Head Office',
});
my $tree = $schema->resultset('My::Department')->create({
name = 'UK Office',
root_id = $uk_office_ident,
});
Creates a new root node.
If the root_column (root_id) is not provided then it defaults to producing a node where the root_column
has the same value as the primary key. This will croak if the table is defined with multiple key primary
index.
Note that no checks (yet) are made to stop you creating another key with the same root_id as an existing
tree. If you do so you will get into a terrible mess!
delete
$department->delete;
This will delete the node and all descendants. Cascade Delete is turned off in the has_many relationships
"nodes" "children" "descendants" so that delete DTRT.
is_root
if ($node->is_root) {
print "Node is a root\n";
}
Returns true if the $node is a root node
is_branch
$has_children = $node->is_branch;
Returns true if the node is a branche (i.e. has children)
is_leaf
$is_terminal_node = $node->is_leaf;
Returns true if the node is a leaf (i.e. it has no children)
siblings
@siblings = $node->siblings;
$siblings_rs = $node->siblings;
Returns all siblings of this $node excluding $node itself.
Since a root node has no siblings it returns undef.
left_siblings(orprevious_siblings)
@younger_siblings = $node->left_siblings;
$younger_siblings_rs = $node->left_siblings;
Returns all siblings of this $node to the left this $node.
Since a root node has no siblings it returns undef.
right_siblings(ornext_siblings)
@older_siblings = $node->right_siblings;
$older_siblings_rs = $node->right_siblings;
Returns all siblings of this $node to the right of this $node.
Since a root node has no siblings it returns undef.
left_sibling(orprevious_sibling)
$younger_sibling = $node->left_sibling;
Returns the sibling immediately to the left of this $node (if any).
right_sibling(ornext_sibling)
$older_sibling = $node->right_sibling;
Returns the sibling immediately to the right of this $node (if any).
leftmost_sibling(orfirst_sibling)
$youngest_sibling = $node->leftmost_sibling;
Returns the left most sibling relative to this $node (if any).
Does not return this $node if this node is the leftmost sibling.
rightmost_sibling(orlast_sibling)
$oldest_sibling = $node->rightmost_sibling;
Returns the right most sibling relative to this $node (if any).
Does not return this $node if this node is the rightmost sibling.
CREATEMETHODS
The following create methods create a new node in relation to an existing node.
create_right_sibling
$bart->create_right_sibling({ name => 'Lisa' });
Create a new node as a right sibling to $bart.
create_left_sibling
$bart->create_left_sibling({ name => 'Maggie' });
Create a new node as a left sibling to $bart.
create_rightmost_child
$homer->create_rightmost_child({ name => 'Lisa' });
Create a new node as a rightmost child to $homer
create_leftmost_child
$homer->create_leftmost_child({ name => 'Maggie' });
Create a new node as a leftmost child to $homer
ATTACHMETHODS
The following attach methods take an existing node (and all of it's descendants) and attaches them to the
tree in relation to an existing node.
The node being inserted can either be from the same tree (as identified by the root_column) or from
another tree. If the root of another tree is attached then the whole of that tree becomes a sub-tree of
this node's tree.
The only restriction is that the node being attached cannot be an ancestor of this node.
When attaching multiple nodes we try to DWIM so that the order they are specified in the call represents
the order they appear in the siblings list.
e.g. if we had a parent with children A,B,C,D,E
and we attached nodes 1,2,3 in the following calls, we expect the following results.
$parent->attach_rightmost_child 1,2,3 gives us children A,B,C,D,E,1,2,3
$parent->attach_leftmost_child 1,2,3 gives us children 1,2,3,A,B,C,D,E
$child_C->attach_right_sibling 1,2,3 gives us children A,B,C,1,2,3,D,E
$child_C->attach_left_sibling 1,2,3 gives us children A,B,1,2,3,C,D,E
$child_C->attach_rightmost_sibling 1,2,3 gives us children A,B,C,D,E,1,2,3
$child_C->attach_leftmost_sibling 1,2,3 gives us children 1,2,3,A,B,C,D,E
attach_rightmost_child(orappend_child)
$parent->attach_rightmost_child($other_node);
$parent->attach_rightmost_child($other_node_1, $other_node_2, ...);
Attaches the other_nodes to $parent as the rightmost children.
attach_leftmost_child
$parent->attach_leftmost_child($other_node);
$parent->attach_leftmost_child($other_node_1, $other_node_2, ...);
Attaches the other_nodes to $parent as the leftmost children.
attach_right_sibling(orattach_after)
$node->attach_right_sibling($other_node);
$node->attach_right_sibling($other_node_1, $other_node_2, ...);
Attaches the other_nodes to $node as it's siblings.
attach_left_sibling
$node->attach_left_sibling($other_node);
$node->attach_left_sibling($other_node_1, $other_node_2, ...);
Attaches the other_nodes to $node as it's left siblings.
attach_rightmost_sibling
$node->attach_rightmost_sibling($other_node);
$node->attach_rightmost_sibling($other_node_1, $other_node_2, ...);
Attaches the other_nodes to $node as it's rightmost siblings.
attach_leftmost_sibling
$node->attach_leftmost_sibling($other_node);
$node->attach_leftmost_sibling($other_node_1, $other_node_2, ...);
Attaches the other_nodes to $node as it's leftmost siblings.
move_left(ormove_previous)
$node->move_left;
Exchange the $node with the sibling immediately to the left and return the node it exchanged with.
If the $node is already the leftmost node then no exchange takes place and the method returns undef.
move_right(ormove_next)
$node->move_right;
Exchange the $node with the sibling immediately to the right and return the node it exchanged with.
If the $node is already the rightmost node then no exchange takes place and the method returns undef.
move_leftmost(ormove_first)
$node->move_leftmost;
Exchange the $node with the leftmost sibling and return the node it exchanged with.
If the $node is already the leftmost node then no exchange takes place and the method returns undef.
move_rightmost(ormove_last)
$node->move_rightmost;
Exchange the $node with the rightmost sibling and return the node it exchanged with.
If the $node is already the rightmost node then no exchange takes place and the method returns undef.
CUTTINGMETHODStake_cutting
Cuts the invocant and its descendants out of the tree they are in, making the invocant the root of a new
tree. Returns the modified invocant.
dissolve
Dissolves the entire thread, that is turn each node of the thread into a single-item tree of its own.