| 1 | =head1 NAME | ||
| 2 | |||
| 3 | DBIx::SQLEngine::RecordSet::NextPrev - A RecordSet with a current index | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | use DBIx::SQLEngine::RecordSet; | ||
| 8 | |||
| 9 | $rs = DBIx::SQLEngine::RecordSet->class( 'NextPrev' )->new(@records); | ||
| 10 | |||
| 11 | while ( $rs->current_record ) { | ||
| 12 | print $rs->get_values( 'name' ); | ||
| 13 | $rs->move_next(); | ||
| 14 | } | ||
| 15 | |||
| 16 | $rs->move_first; | ||
| 17 | |||
| 18 | while ( $rs->current_record ) { | ||
| 19 | $rs->set_values( 'updated' => time() ); | ||
| 20 | $rs->save_record(); | ||
| 21 | $rs->move_next(); | ||
| 22 | } | ||
| 23 | |||
| 24 | =head1 DESCRIPTION | ||
| 25 | |||
| 26 | Provides additional methods for a RecordSet to add a current index with previous and next methods. | ||
| 27 | |||
| 28 | B<This package is INCOMPLETE!> | ||
| 29 | |||
| 30 | =cut | ||
| 31 | |||
| 32 | ######################################################################## | ||
| 33 | |||
| 34 | package DBIx::SQLEngine::RecordSet::NextPrev; | ||
| 35 | |||
| 36 | use strict; | ||
| 37 | use Carp; | ||
| 38 | |||
| 39 | ######################################################################## | ||
| 40 | |||
| 41 | use Class::MakeMethods ( | ||
| 42 | 'Standard::Inheritable:scalar' => 'index_current', | ||
| 43 | ); | ||
| 44 | |||
| 45 | sub reset { | ||
| 46 | (shift)->index_current( undef ) | ||
| 47 | } | ||
| 48 | |||
| 49 | ######################################################################## | ||
| 50 | |||
| 51 | sub index { | ||
| 52 | my $self = shift; | ||
| 53 | my $offset = ( scalar @_ ) ? shift : 0; | ||
| 54 | my $index = $self->index_current + $offset | ||
| 55 | ( $index < 0 ) ? undef : $index | ||
| 56 | } | ||
| 57 | |||
| 58 | sub look { | ||
| 59 | my $self = shift; | ||
| 60 | $self->record( $self->index( @_ ) ) | ||
| 61 | } | ||
| 62 | |||
| 63 | sub move { | ||
| 64 | my $self = shift; | ||
| 65 | $self->index_current( $self->index( @_ ) ) | ||
| 66 | } | ||
| 67 | |||
| 68 | ######################################################################## | ||
| 69 | |||
| 70 | sub current_record { | ||
| 71 | my $self = shift; | ||
| 72 | $self->record( $self->index_current || 0 ) | ||
| 73 | } | ||
| 74 | |||
| 75 | sub get_current { | ||
| 76 | ( $_[0] )->current_record | ||
| 77 | or croak("No current record at position '" . $self->index_current . "'" ) | ||
| 78 | } | ||
| 79 | |||
| 80 | ######################################################################## | ||
| 81 | |||
| 82 | sub index_next { | ||
| 83 | (shift)->index( ( scalar @_ ) ? shift : 1 ) | ||
| 84 | } | ||
| 85 | |||
| 86 | sub next_record { | ||
| 87 | my $self = shift; | ||
| 88 | $self->record( $self->index_next( @_ ) ) | ||
| 89 | } | ||
| 90 | |||
| 91 | sub move_next { | ||
| 92 | my $self = shift; | ||
| 93 | $self->index_current( $self->index_next( @_ ) ); | ||
| 94 | $self->get_current; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub get_next { | ||
| 98 | ( $_[0] )->move_next | ||
| 99 | or croak("No next record at position '" . $self->index_current . "'" ) | ||
| 100 | } | ||
| 101 | |||
| 102 | ######################################################################## | ||
| 103 | |||
| 104 | sub index_prev { | ||
| 105 | (shift)->index( 0 - ( ( scalar @_ ) ? shift : 1 ) ) | ||
| 106 | } | ||
| 107 | |||
| 108 | sub prev_record { | ||
| 109 | my $self = shift; | ||
| 110 | $self->record( $self->index_prev( @_ ) ) | ||
| 111 | } | ||
| 112 | |||
| 113 | sub move_prev { | ||
| 114 | my $self = shift; | ||
| 115 | $self->index_current( $self->index_prev( @_ ) ); | ||
| 116 | $self->get_current; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub get_prev { | ||
| 120 | ( $_[0] )->move_prev | ||
| 121 | or croak("No prev record at position '" . $self->index_current . "'" ) | ||
| 122 | } | ||
| 123 | |||
| 124 | ######################################################################## | ||
| 125 | |||
| 126 | sub index_first { | ||
| 127 | (shift)->record( 0 ) ? 0 : undef | ||
| 128 | } | ||
| 129 | |||
| 130 | sub look_first { | ||
| 131 | my $self = shift; | ||
| 132 | $self->record( ( scalar @_ ) ? shift : 0 ) | ||
| 133 | } | ||
| 134 | |||
| 135 | sub move_first { | ||
| 136 | my $self = shift; | ||
| 137 | $self->index_current( 0 ); | ||
| 138 | $self->get_current; | ||
| 139 | } | ||
| 140 | |||
| 141 | sub get_first { | ||
| 142 | (shift)->move_first( @_ ) | ||
| 143 | or croak("No first record, record set is empty" ) | ||
| 144 | } | ||
| 145 | |||
| 146 | ######################################################################## | ||
| 147 | |||
| 148 | sub index_last { | ||
| 149 | my $self = shift; | ||
| 150 | my $count = $self->count; | ||
| 151 | $count ? $count - 1 : undef | ||
| 152 | } | ||
| 153 | |||
| 154 | sub look_last { | ||
| 155 | my $self = shift; | ||
| 156 | $self->record( 0 - ( ( scalar @_ ) ? shift : 1 ) ) | ||
| 157 | } | ||
| 158 | |||
| 159 | sub move_last { | ||
| 160 | my $self = shift; | ||
| 161 | $self->index_current( $self->index_last ); | ||
| 162 | $self->get_current; | ||
| 163 | } | ||
| 164 | |||
| 165 | sub get_last { | ||
| 166 | (shift)->move_last( @_ ) | ||
| 167 | or croak("No last record, record set is empty" ) | ||
| 168 | } | ||
| 169 | |||
| 170 | ######################################################################## | ||
| 171 | |||
| 172 | sub before_first { | ||
| 173 | ! defined (shift)->index_current | ||
| 174 | } | ||
| 175 | |||
| 176 | sub at_first { | ||
| 177 | (shift)->index_current < 1 | ||
| 178 | } | ||
| 179 | |||
| 180 | sub at_either_end { | ||
| 181 | my $self = shift; | ||
| 182 | $self->at_first or $self->at_last | ||
| 183 | } | ||
| 184 | |||
| 185 | sub at_index { | ||
| 186 | my $self = shift; | ||
| 187 | $self->index_current == $self->index( @_ ) | ||
| 188 | } | ||
| 189 | |||
| 190 | sub at_last { | ||
| 191 | my $self = shift; | ||
| 192 | ( ! $self->get_current ) and ( $self->index_current >= $self->index_last ) | ||
| 193 | } | ||
| 194 | |||
| 195 | sub after_last { | ||
| 196 | my $self = shift; | ||
| 197 | ( ! $self->get_current ) and ( $self->index_current > $self->index_last ) | ||
| 198 | } | ||
| 199 | |||
| 200 | ######################################################################## | ||
| 201 | |||
| 202 | use Class::MakeMethods ( | ||
| 203 | 'Standard::Universal:delegate' => [ | ||
| 204 | [ qw( get_values change_values save_record ) ] => { target=>'get_current' }, | ||
| 205 | ], | ||
| 206 | ); | ||
| 207 | |||
| 208 | ######################################################################## | ||
| 209 | |||
| 210 | ######################################################################## | ||
| 211 | |||
| 212 | =head1 SEE ALSO | ||
| 213 | |||
| 214 | See L<DBIx::SQLEngine> for the overall interface and developer documentation. | ||
| 215 | |||
| 216 | See L<DBIx::SQLEngine::Docs::ReadMe> for general information about | ||
| 217 | this distribution, including installation and license information. | ||
| 218 | |||
| 219 | =cut | ||
| 220 | |||
| 221 | ######################################################################## | ||
| 222 | |||
| 223 | 1; |