blib/lib/Data/Pager.pm

filter: Statementstmt 97.8% 92 / 94 Branchbran 71.8% 23 / 32 Conditioncond 76.4% 13 / 17 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 100% 15 / 15 Podpod 83.3% 5 / 6 totaltotal 88.4% 153 / 173 CC 28 SCAR 26.3
CC
28
Cov
90%
__init
23.8
__ANON__
4.1
list
2.0
CRAP 13.9
↑ Summary
1 package Data::Pager;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use POSIX qw(ceil);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 $VERSION = '0.01';
8  
9 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
10 1 my $class = shift;
11 1 my $data = shift;
12 1 my $self = {};
13  
14 1 ▶ $class ne __PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
$class ne __PACKAGE__ and croak("class Data::Pager constructor is not static") 0 1 1
15 and croak "class " . __PACKAGE__ . " constructor is not static";
16  
17 1 $self->{data}{fields} = $data;
18  
19 1 bless $self, $class;
20  
21 1 $self->__init($data);
22  
23 1 return $self;
24 }
25  
26 2192 ▶sub __init {
Subroutine __init: called CC=23 SCAR=31.7
27 2192 my $self = shift;
28 2192 ▶ my $data = shift || {};
Condition50%1/2
shift() || {}
l!l
21920
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
29  
30 2192 ▶ my $current = $data->{'current'} || 1;
Condition100%2/2
$data->{'current'} || 1
l!l
21911
MC/DC100%1/1
$data->{'current'} || 1
$data->{'current'}
Truth table100%2/2
A
A= $data->{'current'}
Aresult
11
00
31 2192 ▶ my $offset = $data->{'offset'} || 10;
Condition100%2/2
$data->{'offset'} || 10
l!l
21911
MC/DC100%1/1
$data->{'offset'} || 10
$data->{'offset'}
Truth table100%2/2
A
A= $data->{'offset'}
Aresult
11
00
32 2192 ▶ my $links = $data->{'perpage'} || 10;
Condition50%1/2
$data->{'perpage'} || 10
l!l
02192
MC/DC0%0/1
$data->{'perpage'} || 10
$data->{'perpage'}
Truth table50%1/2
A
A= $data->{'perpage'}
Aresult
11
00
33 2192 ▶ my $limit = $data->{'limit'} || 0;
Condition50%1/2
$data->{'limit'} || 0
l!l
21920
MC/DC0%0/1
$data->{'limit'} || 0
$data->{'limit'}
Truth table50%1/2
A
A= $data->{'limit'}
Aresult
11
00
34  
35 2192 $self->{'tmp'} = { links => $links };
36  
37 2192 ▶ $links % 2 or $links += 1;
Branch: 1 branch
BranchTrueFalseTotal
$links % 2 or $links += 1 0 2192 2192
38  
39 2192 my $middle = int( $links / 2 );
40 2192 my %pager = ();
41 2192 my %tmp = ();
42 2192 my @pager = ();
43  
44 return undef
45 2192 ▶ if $current * $offset - $offset + 1 > $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $current * $offset - $offset + 1 > $limit 0 2192 2192
46  
47 2192 for my $i ( $current .. $current + $middle ) {
48 13152 ▶ $pager{$i}++ if $i * $offset - $offset < $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $i * $offset - $offset < $limit 13152 0 13152
49 }
50 2192 for my $i ( $current - $middle .. $current ) {
51 13152 ▶ $pager{$i}++ if $i > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i > 0 13117 35 13152
52 }
53  
54 2192 ▶ if ( scalar keys %pager < $links and $current < 5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %pager < $links and $current < 5) 9 2183 2192
Condition100%3/3
scalar keys %pager < $links and $current < 5
!ll&&!rl&&r
218129
MC/DC100%2/2
scalar keys %pager < $links and $current < 5
scalar keys %pager < $links $current < 5
Truth table100%3/3
A and B
A= scalar keys %pager < $linksB= $current < 5
ABresult
0X0
100
111
55 9 ▶ while ( $middle++ < $links - 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($middle++ < $links - 1) 45 9 54
56 45 ▶ $middle > $limit
Branch: 1 branch
BranchTrueFalseTotal
$middle > $limit and last 0 45 45
57 and last;
58 exists( $pager{$middle} )
59 45 ▶ or $pager{$middle}++;
Branch: 1 branch
BranchTrueFalseTotal
exists $pager{$middle} or ++$pager{$middle} 21 24 45
60 }
61 }
62  
63 2192 %tmp = %pager;
64 2192 %pager = ();
65  
66 2192 for my $pos ( sort { $a <=> $b } keys %tmp ) {
67 $pos == $current
68 24101 ▶ and $pager{'current'} = $pos;
Branch: 1 branch
BranchTrueFalseTotal
$pos == $current and $pager{"current"} = $pos 2192 21909 24101
69 24101 ▶ $pager{$pos} = {
Branch: 2 branches
BranchTrueFalseTotal
$pos - 1 ? : 24088 13 24101
$pos * $offset >= $limit ? : 0 24101 24101
70 'prev' => $pos - 1 ? $pos - 1 : undef,
71 'next' => $pos * $offset >= $limit ? undef: $pos + 1,
72 'from' => $offset * $pos - $offset,
73 'to' => $offset * $pos,
74 };
75 24101 push @pager, $pos;
76 }
77  
78 2192 $pager{'limit'} = $limit;
79 2192 $pager{'list'} = \@pager;
80 2192 $pager{'last'} = $pager[-1];
81 2192 $pager{'first'} = $pager[0];
82 2192 $pager{'start'} = 1;
83 2192 $pager{'end'} = $self->final;
84 2192 $pager{'offset'} = $offset;
85 2192 $pager{'links'} = $self->{'tmp'}->{'links'};
86 2192 ▶ $pager{'prev'} = $pager{ $pager{'current'} }->{'prev'} || undef;
Condition100%2/2
$pager{$pager{'current'}}{'prev'} || undef
l!l
21893
MC/DC100%1/1
$pager{$pager{'current'}}{'prev'} || undef
$pager{$pager{'current'}}{'prev'}
Truth table100%2/2
A
A= $pager{$pager{'current'}}{'prev'}
Aresult
11
00
87 2192 ▶ $pager{'next'} = $pager{ $pager{'current'} }->{'next'} || undef;
Condition50%1/2
$pager{$pager{'current'}}{'next'} || undef
l!l
21920
MC/DC0%0/1
$pager{$pager{'current'}}{'next'} || undef
$pager{$pager{'current'}}{'next'}
Truth table50%1/2
A
A= $pager{$pager{'current'}}{'next'}
Aresult
11
00
88  
89 2192 $self->{'data'} = \%pager;
90  
91 }
92  
93 1101 ▶sub list {
Subroutine list: called CC=2 SCAR=7.1
Pod documented
94 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
95 1101 my $self = shift;
96 wantarray
97 0 ? @{ $self->{data}{list} }
98 1101 ▶ : \@{ $self->{data}{list} };
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1101 1101
99 }
100  
101 1 for my $method (qw(start first last set_current current prev next offset limit links))
102 {
103 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
104 15145 ▶ *$method = sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
105 15145 my $self = shift;
106 15145 my $value = shift;
107 15145 ▶ if ( $method eq 'set_current' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "set_current") 2191 12954 15145
108 2191 ▶ if ( $value > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value > 0) 2191 0 2191
109 2191 $self->{data}{current} = $value;
110 2191 $self->__init( $self->{data} );
111 2191 ▶ if ( $self->current eq $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->current eq $value) { } 2191 0 2191
112 2191 return $self;
113 }
114 else {
115 0 return undef;
116 }
117 }
118 }
119 12954 return $self->{data}{$method};
120 10 };
121 }
122  
123 1091 ▶sub from {
Subroutine from: called CC=1 SCAR=0.0
Pod documented
124 1091 my $self = shift;
125 1091 $self->{'data'}->{ $self->current }{'from'};
126 }
127  
128 1091 ▶sub to {
Subroutine to: called CC=1 SCAR=0.0
Pod documented
129 1091 my $self = shift;
130 1091 $self->{'data'}->{ $self->current }{'to'};
131 }
132  
133 2194 ▶sub final {
Subroutine final: called CC=1 SCAR=0.0
Pod undocumented
134 2194 my $self = shift;
135 1 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
136 2194 return eval { ceil( $self->limit / $self->offset ) };
137 }
138  
139 1 ▶sub end { shift->final }
Subroutine end: called CC=1 SCAR=0.0
Pod documented
140  
141 1 1;
142  
143 __END__
↑ Summary