From: Aran Deltac Date: Sun, 19 Mar 2006 18:36:10 +0000 (+0000) Subject: Rename move_up and move_down to move_previous and move_next. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=80010e2b4d41c53e39b3f08d86c65953ded171ce;p=dbsrgits%2FDBIx-Class-Historic.git Rename move_up and move_down to move_previous and move_next. Much more in depth tests. --- diff --git a/lib/DBIx/Class/Positioned.pm b/lib/DBIx/Class/Positioned.pm index d11c711..35db710 100644 --- a/lib/DBIx/Class/Positioned.pm +++ b/lib/DBIx/Class/Positioned.pm @@ -158,16 +158,17 @@ sub next_sibling { )->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); @@ -180,16 +181,16 @@ sub move_up { 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); diff --git a/lib/DBIx/Class/Tree/AdjacencyList.pm b/lib/DBIx/Class/Tree/AdjacencyList.pm new file mode 100644 index 0000000..e69de29 diff --git a/lib/DBIx/Class/Tree/NestedSet.pm b/lib/DBIx/Class/Tree/NestedSet.pm new file mode 100644 index 0000000..d56d98c --- /dev/null +++ b/lib/DBIx/Class/Tree/NestedSet.pm @@ -0,0 +1,52 @@ +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; diff --git a/t/run/26positioned.tl b/t/run/26positioned.tl index 8981ca1..997f970 100644 --- a/t/run/26positioned.tl +++ b/t/run/26positioned.tl @@ -3,10 +3,56 @@ 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 ++;