| 1 | package DBIO::ResultSet::Pager; | ||||||||||||||||||||
| 2 | # ABSTRACT: help when paging through sets of results | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 7 | our $VERSION = '0.900007'; | |||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 7 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 7 | ▶ | use DBIO::Exception; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 91 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||
| 15 | 91 | my( $proto, $total_entries, $entries_per_page, $current_page ) = @_; | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 91 | my $self = {}; | |||||||||||||||||||
| 18 | 91 | ▶ | bless( $self, ( ref $proto || $proto ) ); | ||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 91 | ▶ | $self->total_entries( $total_entries || 0 ); | ||||||||||||||||||
|
Condition100%2/2
$total_entries || 0
MC/DC100%1/1
$total_entries || 0
$total_entries
Truth table100%2/2
A
A= $total_entries
| |||||||||||||||||||||
| 21 | 91 | ▶ | $self->entries_per_page( $entries_per_page || 10 ); | ||||||||||||||||||
|
Condition100%2/2
$entries_per_page || 10
MC/DC100%1/1
$entries_per_page || 10
$entries_per_page
Truth table100%2/2
A
A= $entries_per_page
| |||||||||||||||||||||
| 22 | 90 | ▶ | $self->current_page( $current_page || 1 ); | ||||||||||||||||||
|
Condition100%2/2
$current_page || 1
MC/DC100%1/1
$current_page || 1
$current_page
Truth table100%2/2
A
A= $current_page
| |||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 90 | return $self; | |||||||||||||||||||
| 25 | } | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 2115 | ▶ | sub entries_per_page { | ||||||||||||||||||
|
Subroutine
entries_per_page: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 29 | 2115 | my $self = shift; | |||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | return $self->{entries_per_page} | ||||||||||||||||||||
| 32 | 2115 | ▶ | unless @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 197 | ▶ | DBIO::Exception->throw( "Fewer than one entry per page!" ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 35 | if $_[0] < 1; | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 196 | $self->{entries_per_page} = $_[0]; | |||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 196 | $self; | |||||||||||||||||||
| 40 | } | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 1262 | ▶ | sub current_page { | ||||||||||||||||||
|
Subroutine
current_page: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||
| 44 | 1262 | my $self = shift; | |||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 1262 | ▶ | if( @_ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 47 | 200 | $self->{current_page} = $_[0]; | |||||||||||||||||||
| 48 | 200 | return $self; | |||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | return $self->first_page | ||||||||||||||||||||
| 52 | 1062 | ▶ | unless defined $self->{current_page}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | return $self->first_page | ||||||||||||||||||||
| 55 | 1061 | ▶ | if $self->{current_page} < $self->first_page; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | return $self->last_page | ||||||||||||||||||||
| 58 | 1060 | ▶ | if $self->{current_page} > $self->last_page; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 1059 | $self->{current_page}; | |||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 2145 | ▶ | sub total_entries { | ||||||||||||||||||
|
Subroutine
total_entries: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 65 | 2145 | my $self = shift; | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 2145 | ▶ | if( @_ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 68 | 127 | $self->{total_entries} = $_[0]; | |||||||||||||||||||
| 69 | 127 | return $self; | |||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | # lazification for DBIO's benefit | ||||||||||||||||||||
| 73 | 2018 | ▶ | if( ref $self->{total_entries} eq 'CODE' ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 74 | 9 | $self->{total_entries} = $self->{total_entries}->(); | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 2016 | $self->{total_entries}; | |||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 75 | ▶ | sub entries_on_this_page { | ||||||||||||||||||
|
Subroutine
entries_on_this_page: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 82 | 75 | my $self = shift; | |||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 75 | ▶ | if ( $self->total_entries == 0 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 85 | 3 | return 0; | |||||||||||||||||||
| 86 | } else { | ||||||||||||||||||||
| 87 | 72 | return $self->last - $self->first + 1; | |||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 1136 | ▶ | sub first_page { | ||||||||||||||||||
|
Subroutine
first_page: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 93 | 1136 | return 1; | |||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 1451 | ▶ | sub last_page { | ||||||||||||||||||
|
Subroutine
last_page: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 98 | 1451 | my $self = shift; | |||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 1451 | my $pages = $self->total_entries / $self->entries_per_page; | |||||||||||||||||||
| 101 | 1451 | my $last_page; | |||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 1451 | ▶ | if ( $pages == int $pages ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 104 | 377 | $last_page = $pages; | |||||||||||||||||||
| 105 | } else { | ||||||||||||||||||||
| 106 | 1074 | $last_page = 1 + int($pages); | |||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 1451 | ▶ | $last_page = 1 if $last_page < 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 110 | 1451 | return $last_page; | |||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 354 | ▶ | sub first { | ||||||||||||||||||
|
Subroutine
first: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 115 | 354 | my $self = shift; | |||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 354 | ▶ | if ( $self->total_entries == 0 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 118 | 8 | return 0; | |||||||||||||||||||
| 119 | } else { | ||||||||||||||||||||
| 120 | 346 | return ( ( $self->current_page - 1 ) * $self->entries_per_page ) + 1; | |||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 246 | ▶ | sub last { | ||||||||||||||||||
|
Subroutine
last: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 126 | 246 | my $self = shift; | |||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 246 | ▶ | if ( $self->current_page == $self->last_page ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 129 | 126 | return $self->total_entries; | |||||||||||||||||||
| 130 | } else { | ||||||||||||||||||||
| 131 | 120 | return ( $self->current_page * $self->entries_per_page ); | |||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 71 | ▶ | sub previous_page { | ||||||||||||||||||
|
Subroutine
previous_page: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 137 | 71 | my $self = shift; | |||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 71 | ▶ | if ( $self->current_page > 1 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 140 | 30 | return $self->current_page - 1; | |||||||||||||||||||
| 141 | } else { | ||||||||||||||||||||
| 142 | 41 | return undef; | |||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | } | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 71 | ▶ | sub next_page { | ||||||||||||||||||
|
Subroutine
next_page: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 148 | 71 | my $self = shift; | |||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 71 | ▶ | $self->current_page < $self->last_page ? $self->current_page + 1 : undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 151 | } | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | # This method would probably be better named 'select' or 'slice' or | ||||||||||||||||||||
| 154 | # something, because it doesn't modify the array the way | ||||||||||||||||||||
| 155 | # CORE::splice() does. | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | 72 | ▶ | sub splice { | ||||||||||||||||||
|
Subroutine
splice: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 158 | 72 | my ( $self, $array ) = @_; | |||||||||||||||||||
| 159 | 72 | ▶ | my $top = @$array > $self->last ? $self->last : @$array; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 160 | 72 | ▶ | return () if $top == 0; # empty | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 161 | 68 | return @{$array}[ $self->first - 1 .. $top - 1 ]; | |||||||||||||||||||
| 162 | } | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | 71 | ▶ | sub skipped { | ||||||||||||||||||
|
Subroutine
skipped: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 166 | 71 | my $self = shift; | |||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | 71 | my $skipped = $self->first - 1; | |||||||||||||||||||
| 169 | 71 | ▶ | return 0 if $skipped < 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 170 | 68 | return $skipped; | |||||||||||||||||||
| 171 | } | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | 70 | ▶ | sub change_entries_per_page { | ||||||||||||||||||
|
Subroutine
change_entries_per_page: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 175 | 70 | my ( $self, $new_epp ) = @_; | |||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | 7 | ▶ | use integer; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 178 | 70 | ▶ | croak("Fewer than one entry per page!") if $new_epp < 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 179 | 70 | my $new_page = 1 + ( $self->first / $new_epp ); | |||||||||||||||||||
| 180 | 70 | $self->entries_per_page($new_epp); | |||||||||||||||||||
| 181 | 70 | $self->current_page($new_page); | |||||||||||||||||||
| 182 | } | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | 7 | 1; | |||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | __END__ | ||||||||||||||||||||