Skip to content
Browse files

implement, document and test method dissolve()

  • Loading branch information...
1 parent 43115dc commit f568d7d6ed59c649d05e9790e2ff7719f8ebf252 @moritz committed
Showing with 25 additions and 0 deletions.
  1. +18 −0 lib/DBIx/Class/Tree/NestedSet.pm
  2. +7 −0 t/17-cutting.t
View
18 lib/DBIx/Class/Tree/NestedSet.pm
@@ -590,6 +590,19 @@ sub take_cutting {
return $self;
}
+sub dissolve {
+ my $self = shift;
+ my ($root, $left, $right, $level) = $self->_get_columns;
+ my $pk = ($self->result_source->primary_columns)[0];
+ $self->nodes_rs->search({$root => $self->$root})->update({
+ $level => 1,
+ $left => 1,
+ $right => 2,
+ $root => \"$pk",
+ });
+ return $self;
+}
+
# Move a node to the left
# Swap position with the sibling on the left
# returns the node it exchanged with on success, undef if it is already leftmost sibling
@@ -1359,6 +1372,11 @@ 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.
+=head2 dissolve
+
+Dissolves the entire thread, that is turn each node of the thread into a
+single-item tree of its own.
+
=head1 CAVEATS
=head2 Multiple Column Primary Keys
View
7 t/17-cutting.t
@@ -73,4 +73,11 @@ is_deeply(
[map { $_->id } $tree1, $child1_1, $child1_3, $child1_4, $gchild1_3, $gchild1_4],
'remainder of tree intact.');
+$subtree->dissolve;
+refresh();
+for ($subtree, $child1_2, $gchild1_1, $gchild1_2, $ggchild1) {
+ ok $_->id == $_->root_id, 'dissolved node stands alone'
+
+}
+
done_testing;

0 comments on commit f568d7d

Please sign in to comment.
Something went wrong with that request. Please try again.