Commit | Line | Data |
80010e2b |
1 | package DBIx::Class::NestedSets; |
2 | use strict; |
3 | use warnings; |
4 | |
5 | use base qw( DBIx::Class ); |
6 | |
7 | __PACKAGE__->mk_classdata( 'nested_left_column' ); |
8 | __PACKAGE__->mk_classdata( 'nested_right_column' ); |
9 | |
10 | sub 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 | |
16 | sub 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 | |
44 | sub extent { |
45 | my( $self ) = @_; |
46 | return ( |
47 | $self->get( $class->nested_right_column() ) - |
48 | $self->get( $class->nested_left_column() ) |
49 | ) + 1; |
50 | } |
51 | |
52 | 1; |