DBIx/Class/ResultSet/Pager.pm

filter: Statementstmt 100% 81 / 81 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% 160 / 165 CC 24 SCAR 10.7
CC
24
Cov
98%
new
5.0
current_page
5.0
entries_per_page
3.0
CRAP 2.9
« DBIx/Class/Storage/DBI/AutoCast.pm ↑ Summary DBIx/Class/Storage/DBI/ODBC/Firebird.pm »
1 package DBIx::Class::ResultSet::Pager;
2  
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use DBIx::Class::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 91 ▶sub new {
Subroutine new: called CC=5 SCAR=16.2
Pod documented
9 91 my( $proto, $total_entries, $entries_per_page, $current_page ) = @_;
10  
11 91 my $self = {};
12 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
13  
14 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
15 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
16 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
17  
18 90 return $self;
19 }
20  
21  
22 2142 ▶sub entries_per_page {
Subroutine entries_per_page: called CC=3 SCAR=11.0
Pod documented
23 2142 my $self = shift;
24  
25 return $self->{entries_per_page}
26 2142 ▶ unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1945 197 2142
27  
28 197 ▶ DBIx::Class::Exception->throw( "Fewer than one entry per page!" )
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] < 1 1 196 197
29 if $_[0] < 1;
30  
31 196 $self->{entries_per_page} = $_[0];
32  
33 196 $self;
34 }
35  
36 1281 ▶sub current_page {
Subroutine current_page: called CC=5 SCAR=16.1
Pod documented
37 1281 my $self = shift;
38  
39 1281 ▶ if( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 202 1079 1281
40 202 $self->{current_page} = $_[0];
41 202 return $self;
42 }
43  
44 return $self->first_page
45 1079 ▶ unless defined $self->{current_page};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"current_page"} 1 1078 1079
46  
47 return $self->first_page
48 1078 ▶ if $self->{current_page} < $self->first_page;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"current_page"} < $self->first_page 2 1076 1078
49  
50 return $self->last_page
51 1076 ▶ if $self->{current_page} > $self->last_page;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"current_page"} > $self->last_page 1 1075 1076
52  
53 1075 $self->{current_page};
54 }
55  
56 2169 ▶sub total_entries {
Subroutine total_entries: called CC=3 SCAR=11.0
Pod documented
57 2169 my $self = shift;
58  
59 2169 ▶ if( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 129 2040 2169
60 129 $self->{total_entries} = $_[0];
61 129 return $self;
62 }
63  
64 # lazification for DBIC's benefit
65 2040 ▶ if( ref $self->{total_entries} eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"total_entries"} eq "CODE") 10 2030 2040
66 10 $self->{total_entries} = $self->{total_entries}->();
67 }
68  
69 2040 $self->{total_entries};
70 }
71  
72  
73 77 ▶sub entries_on_this_page {
Subroutine entries_on_this_page: called CC=2 SCAR=6.9
Pod documented
74 77 my $self = shift;
75  
76 77 ▶ if ( $self->total_entries == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->total_entries == 0) { } 3 74 77
77 3 return 0;
78 } else {
79 74 return $self->last - $self->first + 1;
80 }
81 }
82  
83 1154 ▶sub first_page {
Subroutine first_page: called CC=1 SCAR=0.0
Pod documented
84 1154 return 1;
85 }
86  
87 1472 ▶sub last_page {
Subroutine last_page: called CC=3 SCAR=11.0
Pod documented
88 1472 my $self = shift;
89  
90 1472 my $pages = $self->total_entries / $self->entries_per_page;
91 1472 my $last_page;
92  
93 1472 ▶ if ( $pages == int $pages ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pages == int $pages) { } 380 1092 1472
94 380 $last_page = $pages;
95 } else {
96 1092 $last_page = 1 + int($pages);
97 }
98  
99 1472 ▶ $last_page = 1 if $last_page < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $last_page < 1 35 1437 1472
100 1472 return $last_page;
101 }
102  
103 356 ▶sub first {
Subroutine first: called CC=2 SCAR=6.9
Pod documented
104 356 my $self = shift;
105  
106 356 ▶ if ( $self->total_entries == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->total_entries == 0) { } 8 348 356
107 8 return 0;
108 } else {
109 348 return ( ( $self->current_page - 1 ) * $self->entries_per_page ) + 1;
110 }
111 }
112  
113 248 ▶sub last {
Subroutine last: called CC=2 SCAR=6.9
Pod documented
114 248 my $self = shift;
115  
116 248 ▶ if ( $self->current_page == $self->last_page ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->current_page == $self->last_page) { } 127 121 248
117 127 return $self->total_entries;
118 } else {
119 121 return ( $self->current_page * $self->entries_per_page );
120 }
121 }
122  
123 74 ▶sub previous_page {
Subroutine previous_page: called CC=2 SCAR=6.9
Pod documented
124 74 my $self = shift;
125  
126 74 ▶ if ( $self->current_page > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->current_page > 1) { } 33 41 74
127 33 return $self->current_page - 1;
128 } else {
129 41 return undef;
130 }
131 }
132  
133 74 ▶sub next_page {
Subroutine next_page: called CC=2 SCAR=6.9
Pod documented
134 74 my $self = shift;
135  
136 74 ▶ $self->current_page < $self->last_page ? $self->current_page + 1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->current_page < $self->last_page ? : 32 42 74
137 }
138  
139 # This method would probably be better named 'select' or 'slice' or
140 # something, because it doesn't modify the array the way
141 # CORE::splice() does.
142 72 ▶sub splice {
Subroutine splice: called CC=3 SCAR=11.0
Pod documented
143 72 my ( $self, $array ) = @_;
144 72 ▶ my $top = @$array > $self->last ? $self->last : @$array;
Branch: 1 branch
BranchTrueFalseTotal
@$array > $self->last ? : 31 41 72
145 72 ▶ return () if $top == 0; # empty
Branch: 1 branch
BranchTrueFalseTotal
if $top == 0 4 68 72
146 68 return @{$array}[ $self->first - 1 .. $top - 1 ];
147 }
148  
149 71 ▶sub skipped {
Subroutine skipped: called CC=2 SCAR=6.9
Pod documented
150 71 my $self = shift;
151  
152 71 my $skipped = $self->first - 1;
153 71 ▶ return 0 if $skipped < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $skipped < 0 3 68 71
154 68 return $skipped;
155 }
156  
157 70 ▶sub change_entries_per_page {
Subroutine change_entries_per_page: called CC=2 SCAR=7.0
Pod documented
158 70 my ( $self, $new_epp ) = @_;
159  
160 8 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
161 70 ▶ croak("Fewer than one entry per page!") if $new_epp < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $new_epp < 1 0 70 70
162 70 my $new_page = 1 + ( $self->first / $new_epp );
163 70 $self->entries_per_page($new_epp);
164 70 $self->current_page($new_page);
165 }
166  
167 8 1;
168  
169 __END__
« DBIx/Class/Storage/DBI/AutoCast.pm ↑ Summary DBIx/Class/Storage/DBI/ODBC/Firebird.pm »