add basic cache tests/documentation
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Positioned.pm
index d11c711..a8daf69 100644 (file)
@@ -83,11 +83,14 @@ sub siblings {
     my( $self ) = @_;
     my $position_column = $self->position_column;
     my $rs = $self->search(
-        { $position_column => { '!=' => $self->get_column($position_column) } },
+        {
+            $position_column => { '!=' => $self->get_column($position_column) },
+            $self->_parent_clause(),
+        },
         { order_by => $self->position_column },
     );
-    if (wantarray()) { return $rs->all(); }
-    else { return $rs; }
+    return $rs->all() if (wantarray());
+    return $rs;
 }
 
 =head2 first_sibling
@@ -101,7 +104,7 @@ Returns the first sibling object.
 sub first_sibling {
     my( $self ) = @_;
     return ($self->search(
-        {},
+        { $self->_parent_clause() },
         { rows=>1, order_by => $self->position_column },
     )->all())[0];
 }
@@ -117,7 +120,7 @@ Return the last sibling.
 sub last_sibling {
     my( $self ) = @_;
     return ($self->search(
-        {},
+        { $self->_parent_clause() },
         { rows=>1, order_by => $self->position_column.' DESC' },
     )->all())[0];
 }
@@ -135,7 +138,10 @@ sub previous_sibling {
     my( $self ) = @_;
     my $position_column = $self->position_column;
     return ($self->search(
-        { $position_column => { '<' => $self->get_column($position_column) } },
+        {
+            $position_column => { '<' => $self->get_column($position_column) },
+            $self->_parent_clause(),
+        },
         { rows=>1, order_by => $position_column.' DESC' },
     )->all())[0];
 }
@@ -153,21 +159,25 @@ sub next_sibling {
     my( $self ) = @_;
     my $position_column = $self->position_column;
     return ($self->search(
-        { $position_column => { '>' => $self->get_column($position_column) } },
+        {
+            $position_column => { '>' => $self->get_column($position_column) },
+            $self->_parent_clause(),
+        },
         { rows=>1, order_by => $position_column },
     )->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 +190,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);
@@ -227,7 +237,7 @@ success, and 0 is returned if the object is already the last one.
 
 sub move_last {
     my( $self ) = @_;
-    my $count = $self->search()->count();
+    my $count = $self->search({$self->_parent_clause()})->count();
     return $self->move_to( $count );
 }
 
@@ -250,7 +260,8 @@ sub move_to {
         -and => [
             $position_column => { ($from_position>$to_position?'<':'>') => $from_position },
             $position_column => { ($from_position>$to_position?'>=':'<=') => $to_position },
-        ]
+        ],
+        $self->_parent_clause(),
     });
     my $op = ($from_position>$to_position) ? '+' : '-';
     $rs->update({
@@ -272,7 +283,7 @@ the table +1, thus positioning the new record at the last position.
 sub insert {
     my $self = shift;
     my $position_column = $self->position_column;
-    $self->set_column( $position_column => $self->count()+1 ) 
+    $self->set_column( $position_column => $self->search( {$self->_parent_clause()} )->count()+1 ) 
         if (!$self->get_column($position_column));
     $self->next::method( @_ );
 }
@@ -291,13 +302,32 @@ sub delete {
     $self->next::method( @_ );
 }
 
-1;
-__END__
+=head1 PRIVATE METHODS
+
+These methods are used internally.  You should never have the 
+need to use them.
+
+=head2 _parent_clause
+
+  sub _parent_clause {
+    my( $self ) = @_;
+    return ( parent_id => $self->parent_id );
+  }
+
+This method is a placeholder for you, or another component, to 
+provide additional limits for all the various queries in this 
+module.  This allows for more than one positionable list within 
+the same table since any move_* method will adhere to the clause 
+that you specify.
 
-=head1 TODO
+=cut
 
-Support foreign keys that cause rows to be members of mini 
-positionable sets.
+sub _parent_clause {
+    return ();
+}
+
+1;
+__END__
 
 =head1 BUGS