blib/lib/DBIO/ResultSet/Pager.pm

filter: Statementstmt 100% 82 / 82 Branchbran 97.3% 37 / 38 Conditioncond 77.7% 7 / 9 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 18 / 18 Podpod 100% 14 / 14 totaltotal 96.9% 161 / 166 CC 24 SCAR 10.7
CC
24
Cov
98%
new
5.0
current_page
5.0
entries_per_page
3.0
CRAP 2.9
« blib/lib/DBIO/Storage/DBI/AutoCast.pm ↑ Summary blib/lib/DBIO/Test/Future.pm »
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
l!l&&r!l&&!r
0910
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
19  
20 91 ▶ $self->total_entries( $total_entries || 0 );
Condition100%2/2
$total_entries || 0
l!l
5239
MC/DC100%1/1
$total_entries || 0
$total_entries
Truth table100%2/2
A
A= $total_entries
Aresult
11
00
21 91 ▶ $self->entries_per_page( $entries_per_page || 10 );
Condition100%2/2
$entries_per_page || 10
l!l
5536
MC/DC100%1/1
$entries_per_page || 10
$entries_per_page
Truth table100%2/2
A
A= $entries_per_page
Aresult
11
00
22 90 ▶ $self->current_page( $current_page || 1 );
Condition100%2/2
$current_page || 1
l!l
5337
MC/DC100%1/1
$current_page || 1
$current_page
Truth table100%2/2
A
A= $current_page
Aresult
11
00
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
BranchTrueFalseTotal
unless @_ 1918 197 2115
33  
34 197 ▶ DBIO::Exception->throw( "Fewer than one entry per page!" )
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] < 1 1 196 197
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
BranchTrueFalseTotal
if (@_) 200 1062 1262
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
BranchTrueFalseTotal
unless defined $self->{"current_page"} 1 1061 1062
53  
54 return $self->first_page
55 1061 ▶ if $self->{current_page} < $self->first_page;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"current_page"} < $self->first_page 1 1060 1061
56  
57 return $self->last_page
58 1060 ▶ if $self->{current_page} > $self->last_page;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"current_page"} > $self->last_page 1 1059 1060
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
BranchTrueFalseTotal
if (@_) 127 2018 2145
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
BranchTrueFalseTotal
if (ref $self->{"total_entries"} eq "CODE") 9 2009 2018
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
BranchTrueFalseTotal
if ($self->total_entries == 0) { } 3 72 75
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
BranchTrueFalseTotal
if ($pages == int $pages) { } 377 1074 1451
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
BranchTrueFalseTotal
if $last_page < 1 35 1416 1451
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
BranchTrueFalseTotal
if ($self->total_entries == 0) { } 8 346 354
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
BranchTrueFalseTotal
if ($self->current_page == $self->last_page) { } 126 120 246
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
BranchTrueFalseTotal
if ($self->current_page > 1) { } 30 41 71
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
BranchTrueFalseTotal
$self->current_page < $self->last_page ? : 30 41 71
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
BranchTrueFalseTotal
@$array > $self->last ? : 31 41 72
160 72 ▶ return () if $top == 0; # empty
Branch: 1 branch
BranchTrueFalseTotal
if $top == 0 4 68 72
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
BranchTrueFalseTotal
if $skipped < 0 3 68 71
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
BranchTrueFalseTotal
if $new_epp < 1 0 70 70
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__
« blib/lib/DBIO/Storage/DBI/AutoCast.pm ↑ Summary blib/lib/DBIO/Test/Future.pm »