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
sub first_sibling {
my( $self ) = @_;
return ($self->search(
- {},
+ { $self->_parent_clause() },
{ rows=>1, order_by => $self->position_column },
)->all())[0];
}
sub last_sibling {
my( $self ) = @_;
return ($self->search(
- {},
+ { $self->_parent_clause() },
{ rows=>1, order_by => $self->position_column.' DESC' },
)->all())[0];
}
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];
}
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);
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);
sub move_last {
my( $self ) = @_;
- my $count = $self->search()->count();
+ my $count = $self->search({$self->_parent_clause()})->count();
return $self->move_to( $count );
}
-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({
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( @_ );
}
$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