Rename move_up and move_down to move_previous and move_next.
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Tree / NestedSet.pm
CommitLineData
80010e2b 1package DBIx::Class::NestedSets;
2use strict;
3use warnings;
4
5use base qw( DBIx::Class );
6
7__PACKAGE__->mk_classdata( 'nested_left_column' );
8__PACKAGE__->mk_classdata( 'nested_right_column' );
9
10sub set_nested_columns {
11 my( $class, $left_column, $right_column ) = @_;
12 $class->nested_left_column( $left_column );
13 $class->nested_right_column( $right_column );
14}
15
16sub append_child {
17 my( $parent, $child ) = @_;
18
19 # Preload these since we will be useing them more than once.
20 my $right_column = $parent->nested_right_column();
21 my $left_column = $parent->nested_left_column();
22 my $parent_right = $parent->get($right_column);
23 my $child_extent = $child->extent();
24
25 # Find all nodes to the right of the parent, including the parent.
26 my $rs = $parent->search( {
27 $right_column => { '>=', $parent_right }
28 } );
29
30 # Shift all nodes to the right by the extent of the child.
31 $rs->update(
32 $right_column => {
33 $right_column => { '+', $child_extent }
34 }
35 );
36
37 # Pop the child in to the space that we opened up.
38 $child->set(
39 $left_column => $parent_right,
40 $right_column => ($parent_right + $child_extent) - 1,
41 );
42}
43
44sub extent {
45 my( $self ) = @_;
46 return (
47 $self->get( $class->nested_right_column() ) -
48 $self->get( $class->nested_left_column() )
49 ) + 1;
50}
51
521;