Much more in depth tests.
)->all())[0];
}
-=head2 move_up
+=head2 move_previous
- $employee->move_up();
+ $employee->move_previous();
-Swaps position with the sibling on position higher. 1 is returned on
-success, and 0 is returned if the objects is already the first one.
+Swaps position with the sibling on position previous in the list.
+1 is returned on success, and 0 is returned if the objects is already
+the first one.
=cut
-sub move_up {
+sub move_previous {
my( $self ) = @_;
my $previous = $self->previous_sibling();
return undef if (!$previous);
return 1;
}
-=head2 move_down
+=head2 move_next
- $employee->move_down();
+ $employee->move_next();
-Swaps position with the sibling on position lower. 1 is returned on
-success, and 0 is returned if the object is already at the last position.
+Swaps position with the sibling in the next position. 1 is returned on
+success, and 0 is returned if the object is already the last in the list.
=cut
-sub move_down {
+sub move_next {
my( $self ) = @_;
my $next = $self->next_sibling();
return undef if (!$next);
--- /dev/null
+package DBIx::Class::NestedSets;
+use strict;
+use warnings;
+
+use base qw( DBIx::Class );
+
+__PACKAGE__->mk_classdata( 'nested_left_column' );
+__PACKAGE__->mk_classdata( 'nested_right_column' );
+
+sub set_nested_columns {
+ my( $class, $left_column, $right_column ) = @_;
+ $class->nested_left_column( $left_column );
+ $class->nested_right_column( $right_column );
+}
+
+sub append_child {
+ my( $parent, $child ) = @_;
+
+ # Preload these since we will be useing them more than once.
+ my $right_column = $parent->nested_right_column();
+ my $left_column = $parent->nested_left_column();
+ my $parent_right = $parent->get($right_column);
+ my $child_extent = $child->extent();
+
+ # Find all nodes to the right of the parent, including the parent.
+ my $rs = $parent->search( {
+ $right_column => { '>=', $parent_right }
+ } );
+
+ # Shift all nodes to the right by the extent of the child.
+ $rs->update(
+ $right_column => {
+ $right_column => { '+', $child_extent }
+ }
+ );
+
+ # Pop the child in to the space that we opened up.
+ $child->set(
+ $left_column => $parent_right,
+ $right_column => ($parent_right + $child_extent) - 1,
+ );
+}
+
+sub extent {
+ my( $self ) = @_;
+ return (
+ $self->get( $class->nested_right_column() ) -
+ $self->get( $class->nested_left_column() )
+ ) + 1;
+}
+
+1;
sub run_tests {
my $schema = shift;
- my $artists = $schema->resultset("Artist")->search({},{order_by=>'position'});
+ my $artists = $schema->resultset("Artist");
- plan tests => $artists->count();
+ $artists->delete();
+ $artists->create({ artistid=>1, name=>"Joe" });
+ $artists->create({ artistid=>2, name=>"Bob" });
+ $artists->create({ artistid=>3, name=>"Ted" });
+ $artists->create({ artistid=>4, name=>"Ned" });
+ $artists->create({ artistid=>5, name=>"Don" });
+ $artists = $artists->search(undef,{order_by=>'position'});
+
+ plan tests => 230;
+
+ check_positions($schema);
+
+ my $artist;
+
+ foreach my $position (1..5) {
+
+ $artist = $artists->find({ position=>$position });
+ $artist->move_previous();
+ check_positions($schema);
+
+ $artist = $artists->find({ position=>$position });
+ $artist->move_next();
+ check_positions($schema);
+
+ $artist = $artists->find({ position=>$position });
+ $artist->move_first();
+ check_positions($schema);
+
+ $artist = $artists->find({ position=>$position });
+ $artist->move_last();
+ check_positions($schema);
+
+ foreach my $to_position (1..5) {
+
+ $artist = $artists->find({ position=>$position });
+ $artist->move_to($to_position);
+ check_positions($schema);
+
+ }
+
+ }
+
+}
+
+sub check_positions {
+ my $schema = shift;
+ my $artists = $schema->resultset("Artist")->search(undef,{order_by=>'position'});
my $expected_position = 0;
while (my $artist = $artists->next()) {
$expected_position ++;