| 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
| |||||||||||||||||||||||||||||
| 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() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 2192 | ▶ | my $current = $data->{'current'} || 1; | ||||||||||||||||||||||||||
|
Condition100%2/2
$data->{'current'} || 1
MC/DC100%1/1
$data->{'current'} || 1
$data->{'current'}
Truth table100%2/2
A
A= $data->{'current'}
| |||||||||||||||||||||||||||||
| 31 | 2192 | ▶ | my $offset = $data->{'offset'} || 10; | ||||||||||||||||||||||||||
|
Condition100%2/2
$data->{'offset'} || 10
MC/DC100%1/1
$data->{'offset'} || 10
$data->{'offset'}
Truth table100%2/2
A
A= $data->{'offset'}
| |||||||||||||||||||||||||||||
| 32 | 2192 | ▶ | my $links = $data->{'perpage'} || 10; | ||||||||||||||||||||||||||
|
Condition50%1/2
$data->{'perpage'} || 10
MC/DC0%0/1
$data->{'perpage'} || 10
$data->{'perpage'}
Truth table50%1/2
A
A= $data->{'perpage'}
| |||||||||||||||||||||||||||||
| 33 | 2192 | ▶ | my $limit = $data->{'limit'} || 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$data->{'limit'} || 0
MC/DC0%0/1
$data->{'limit'} || 0
$data->{'limit'}
Truth table50%1/2
A
A= $data->{'limit'}
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 2192 | $self->{'tmp'} = { links => $links }; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 2192 | ▶ | $links % 2 or $links += 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 2192 | for my $i ( $current .. $current + $middle ) { | |||||||||||||||||||||||||||
| 48 | 13152 | ▶ | $pager{$i}++ if $i * $offset - $offset < $limit; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | 2192 | for my $i ( $current - $middle .. $current ) { | |||||||||||||||||||||||||||
| 51 | 13152 | ▶ | $pager{$i}++ if $i > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 2192 | ▶ | if ( scalar keys %pager < $links and $current < 5 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
scalar keys %pager < $links and $current < 5
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
| |||||||||||||||||||||||||||||
| 55 | 9 | ▶ | while ( $middle++ < $links - 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 45 | ▶ | $middle > $limit | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | and last; | ||||||||||||||||||||||||||||
| 58 | exists( $pager{$middle} ) | ||||||||||||||||||||||||||||
| 59 | 45 | ▶ | or $pager{$middle}++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 69 | 24101 | ▶ | $pager{$pos} = { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$pager{$pager{'current'}}{'prev'} || undef
$pager{$pager{'current'}}{'prev'}
Truth table100%2/2
A
A= $pager{$pager{'current'}}{'prev'}
| |||||||||||||||||||||||||||||
| 87 | 2192 | ▶ | $pager{'next'} = $pager{ $pager{'current'} }->{'next'} || undef; | ||||||||||||||||||||||||||
|
Condition50%1/2
$pager{$pager{'current'}}{'next'} || undef
MC/DC0%0/1
$pager{$pager{'current'}}{'next'} || undef
$pager{$pager{'current'}}{'next'}
Truth table50%1/2
A
A= $pager{$pager{'current'}}{'next'}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 108 | 2191 | ▶ | if ( $value > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 2191 | $self->{data}{current} = $value; | |||||||||||||||||||||||||||
| 110 | 2191 | $self->__init( $self->{data} ); | |||||||||||||||||||||||||||
| 111 | 2191 | ▶ | if ( $self->current eq $value ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||