blib/lib/Template/Plugin/Bootstrap/Pagination.pm

filter: Statementstmt 98.1% 104 / 106 Branchbran 95.6% 44 / 46 Conditioncond 79.4% 27 / 34 MC/DCmcdc 57.8% 11 / 19 Subroutinesub 100% 17 / 17 Podpod 100% 3 / 3 totaltotal 91.5% 206 / 225 CC 37 SCAR 20.6
CC
37
Cov
94%
pagination
17.1
pager
6.0
_pagination_3
5.0
CRAP 7.8
↑ Summary
1 package Template::Plugin::Bootstrap::Pagination;
2 {
3 2 $Template::Plugin::Bootstrap::Pagination::VERSION = '0.002000';
4 }
5 2 ▶use parent qw(Template::Plugin);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # ABSTRACT: Produce HTML suitable for the Bootstrap pagination component
8  
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use MRO::Compat;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Template::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 10 ▶sub new {
Subroutine new: called CC=4 SCAR=14.2
Pod documented
20 10 my ($class, $context, $arg_ref) = @_;
21  
22 10 my $self = $class->next::method($context, $arg_ref);
23  
24 10 ▶ if (defined $arg_ref && ref $arg_ref ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg_ref and ref $arg_ref ne "HASH") 0 10 10
Condition66%2/3
defined $arg_ref and ref $arg_ref ne "HASH"
!ll&&!rl&&r
640
MC/DC0%0/2
defined $arg_ref and ref $arg_ref ne "HASH"
defined $arg_ref ref $arg_ref ne "HASH"
Truth table66%2/3
A and B
A= defined $arg_refB= ref $arg_ref ne "HASH"
ABresult
0X0
100
111
25 0 $self->_throw('Hash reference required');
26 }
27 10 ▶ $arg_ref ||= {};
Condition100%2/2
$arg_ref ||= {}
l!l
46
MC/DC100%1/1
$arg_ref ||= {}
$arg_ref
Truth table100%2/2
A
A= $arg_ref
Aresult
11
00
28 $self->{default} = {
29 prev_text => '«',
30 next_text => '»',
31 centered => 0,
32 right => 0,
33 siblings => 3,
34 offset => 0,
35 factor => 1,
36 version => 2,
37 10 %{$arg_ref},
38 };
39  
40 10 return $self;
41 }
42  
43  
44  
45 12 ▶sub pagination {
Subroutine pagination: called CC=17 SCAR=28.4
Pod documented
46 12 my ($self, $arg_ref) = @_;
47  
48 $arg_ref = {
49 12 %{$self->{default}},
50 12 ▶ %{$arg_ref || {}},
Condition100%2/2
$arg_ref || {}
l!l
84
MC/DC100%1/1
$arg_ref || {}
$arg_ref
Truth table100%2/2
A
A= $arg_ref
Aresult
11
00
51 };
52  
53 12 my $pager = $arg_ref->{pager};
54 12 ▶ unless (blessed $pager && $pager->isa('Data::Page')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($pager) and $pager->isa("Data::Page")) 1 11 12
Condition66%2/3
builtin::blessed($pager) and $pager->isa("Data::Page")
!ll&&!rl&&r
1011
MC/DC50%1/2
builtin::blessed($pager) and $pager->isa("Data::Page")
builtin::blessed($pager) $pager->isa("Data::Page")
Truth table66%2/3
A and B
A= builtin::blessed($pager)B= $pager->isa("Data::Page")
ABresult
0X0
100
111
55 1 $self->_throw("Required 'pager' parameter not passed or not a 'Data::Page' instance");
56 }
57  
58 11 my $pagination = '';
59 11 ▶ if ($pager->total_entries() > $pager->entries_per_page()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pager->total_entries > $pager->entries_per_page) 5 6 11
60 5 my $current_page = $pager->current_page();
61 5 my $first_page = $pager->first_page();
62 5 my $last_page = $pager->last_page();
63 5 my $page = $first_page;
64 5 ▶ PAGE: while ($page <= $last_page) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page <= $last_page) 20 4 24
65 20 ▶ if ($current_page == $page) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_page == $page) { } 5 15 20
66 5 $pagination .= '<li class="active">'
67 . '<span>'.$page.'</span>'
68 . '</li>';
69 }
70 else {
71 15 ▶ if ($page == $first_page || $page == $last_page
Branch: 2 branches
BranchTrueFalseTotal
if ($page == $first_page or $page == $last_page or abs $page - $current_page <= $arg_ref->{'siblings'} or $last_page <= 2 * $arg_ref->{'siblings'} + 1) { } 11 4 15
elsif ($first_page + 1 == $page or $last_page - 1 == $page) { } 2 2 4
Condition88%8/9
$page == $first_page or $page == $last_page or abs $page - $current_page <= $arg_ref->{"siblings"} or $last_page <= 2 * $arg_ref->{"siblings"} + 1
l!l&&r!l&&!r
1104
$page == $first_page or $page == $last_page or abs $page - $current_page <= $arg_ref->{"siblings"}
l!l&&r!l&&!r
654
$page == $first_page or $page == $last_page
l!l&&r!l&&!r
429
MC/DC75%3/4
$page == $first_page or $page == $last_page or abs $page - $current_page <= $arg_ref->{"siblings"} or $last_page <= 2 * $arg_ref->{"siblings"} + 1
$page == $first_page $page == $last_page abs $page - $current_page <= $arg_ref->{"siblings"} $last_page <= 2 * $arg_ref->{"siblings"} + 1
Truth table80%4/5
A or B or C or D
A= $page == $first_pageB= $page == $last_pageC= abs $page - $current_page <= $arg_ref->{"siblings"}D= $last_page <= 2 * $arg_ref->{"siblings"} + 1
ABCDresult
1XXX1
01XX1
001X1
00011
00000
72 || abs($page - $current_page) <= ($arg_ref->{siblings})
73 || $last_page <= (2 * $arg_ref->{siblings} + 1)) {
74 11 $pagination .= '<li>'
75 . '<a href="'.$self->_uri_for_page($page, $arg_ref).'">'.$page.'</a>'
76 . '</li>';
77 }
78 ▶ elsif ($first_page + 1 == $page || $last_page - 1 == $page) {
Condition100%3/3
$first_page + 1 == $page or $last_page - 1 == $page
l!l&&r!l&&!r
112
MC/DC100%2/2
$first_page + 1 == $page or $last_page - 1 == $page
$first_page + 1 == $page $last_page - 1 == $page
Truth table100%3/3
A or B
A= $first_page + 1 == $pageB= $last_page - 1 == $page
ABresult
1X1
011
000
79 2 $pagination .= '<li class="disabled">'
80 . '<span>&hellip;</span>'
81 . '</li>';
82 }
83 else {
84 $page = ($page < $current_page)
85 ? $current_page - $arg_ref->{siblings}
86 2 ▶ : $last_page - 1;
Branch: 1 branch
BranchTrueFalseTotal
$page < $current_page ? : 1 1 2
87 2 next PAGE;
88 }
89 }
90 17 $page++;
91 }
92 }
93  
94 10 ▶ my $version = $arg_ref->{version} || 2;
Condition50%1/2
$arg_ref->{'version'} || 2
l!l
100
MC/DC0%0/1
$arg_ref->{'version'} || 2
$arg_ref->{'version'}
Truth table50%1/2
A
A= $arg_ref->{'version'}
Aresult
11
00
95 10 ▶ if ($version eq '2') {
Branch: 2 branches
BranchTrueFalseTotal
if ($version eq '2') { } 6 4 10
elsif ($version eq '3') { } 4 0 4
96 6 return $self->_pagination_2($pagination, $arg_ref);
97 } elsif ($version eq '3') {
98 4 return $self->_pagination_3($pagination, $arg_ref);
99 } else {
100 0 croak('Bootstrap version ' . $version . ' not (yet) supported');
101 }
102 }
103  
104 6 ▶sub _pagination_2 {
Subroutine _pagination_2: called CC=4 SCAR=13.9
105 6 my ($self, $pagination, $arg_ref) = @_;
106  
107 my $alignment = $arg_ref->{centered}
108 ? ' pagination-centered'
109 6 ▶ : ($arg_ref->{right} ? ' pagination-right' : '');
Branch: 2 branches
BranchTrueFalseTotal
$arg_ref->{'centered'} ? : 1 5 6
$arg_ref->{'right'} ? : 1 4 5
110 6 ▶ my $size = defined $arg_ref->{size} ? ' pagination-'.$arg_ref->{size} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $arg_ref->{'size'} ? : 1 5 6
111 6 my ($prev_uri, $next_uri) = $self->_prev_next_uri($arg_ref);
112 return '<div class="pagination'.$alignment.$size.'">'
113 . '<ul>'
114 . $self->_pager_item($prev_uri, $arg_ref->{prev_text})
115 . $pagination
116 . $self->_pager_item($next_uri, $arg_ref->{next_text})
117 6 . '</ul>'
118 . '</div>';
119 }
120  
121 4 ▶sub _pagination_3 {
Subroutine _pagination_3: called CC=5 SCAR=16.1
122 4 my ($self, $pagination, $arg_ref) = @_;
123  
124 my $alignment = $arg_ref->{centered}
125 ? 'text-center'
126 4 ▶ : ($arg_ref->{right} ? 'text-right' : 'text-left');
Branch: 2 branches
BranchTrueFalseTotal
$arg_ref->{'centered'} ? : 1 3 4
$arg_ref->{'right'} ? : 1 2 3
127 my $size = defined $arg_ref->{size} ? ({
128 'mini' => ' pagination-sm',
129 'small' => ' pagination-sm',
130 'large' => ' pagination-lg',
131 4 ▶ }->{$arg_ref->{size}} || '') : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $arg_ref->{'size'} ? : 1 3 4
Condition50%1/2
{"mini", " pagination-sm", "small", " pagination-sm", "large", " pagination-lg"}->{$arg_ref->{"size"}} or ""
l!l
10
MC/DC0%0/1
{"mini", " pagination-sm", "small", " pagination-sm", "large", " pagination-lg"}->{$arg_ref->{"size"}} or ""
{"mini", " pagination-sm", "small", " pagination-sm", "large", " pagination-lg"}->{$arg_ref->{"size"}}
Truth table50%1/2
A
A= {"mini", " pagination-sm", "small", " pagination-sm", "large", " pagination-lg"}->{$arg_ref->{"size"}}
Aresult
11
00
132 4 my ($prev_uri, $next_uri) = $self->_prev_next_uri($arg_ref);
133 return '<div class="'.$alignment.'">'
134 . '<ul class="pagination'.$size.'">'
135 . $self->_pager_item($prev_uri, $arg_ref->{prev_text})
136 . $pagination
137 . $self->_pager_item($next_uri, $arg_ref->{next_text})
138 4 . '</ul>'
139 . '</div>';
140 }
141  
142  
143  
144 5 ▶sub pager {
Subroutine pager: called CC=6 SCAR=17.9
Pod documented
145 5 my ($self, $arg_ref) = @_;
146  
147 $arg_ref = {
148 5 %{$self->{default}},
149 5 ▶ %{$arg_ref || {}},
Condition100%2/2
$arg_ref || {}
l!l
32
MC/DC100%1/1
$arg_ref || {}
$arg_ref
Truth table100%2/2
A
A= $arg_ref
Aresult
11
00
150 };
151  
152 5 my $pager = $arg_ref->{pager};
153 5 ▶ unless (blessed $pager && $pager->isa('Data::Page')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($pager) and $pager->isa("Data::Page")) 1 4 5
Condition66%2/3
builtin::blessed($pager) and $pager->isa("Data::Page")
!ll&&!rl&&r
104
MC/DC50%1/2
builtin::blessed($pager) and $pager->isa("Data::Page")
builtin::blessed($pager) $pager->isa("Data::Page")
Truth table66%2/3
A and B
A= builtin::blessed($pager)B= $pager->isa("Data::Page")
ABresult
0X0
100
111
154 1 $self->_throw("Required 'pager' parameter not passed or not a 'Data::Page' instance");
155 }
156  
157 4 my ($prev_uri, $next_uri) = $self->_prev_next_uri($arg_ref);
158 my $prev_page = $self->_pager_item(
159 3 ▶ $prev_uri, $arg_ref->{prev_text}, $arg_ref->{align} ? 'previous' : ()
Branch: 1 branch
BranchTrueFalseTotal
$arg_ref->{'align'} ? : 2 1 3
160 );
161 my $next_page = $self->_pager_item(
162 3 ▶ $next_uri, $arg_ref->{next_text}, $arg_ref->{align} ? 'next' : ()
Branch: 1 branch
BranchTrueFalseTotal
$arg_ref->{'align'} ? : 2 1 3
163 );
164  
165 3 return '<ul class="pager">'
166 . $prev_page
167 . $next_page
168 . '</ul>';
169 }
170  
171  
172 30 ▶sub _pager_item {
Subroutine _pager_item: called CC=3 SCAR=11.0
173 30 my ($self, $uri, $text, @item_classes) = @_;
174  
175 30 my $content;
176 30 ▶ if (defined $uri) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $uri) { } 15 15 30
177 15 $content = '<a href="'.$uri.'">'.$text.'</a>';
178 }
179 else {
180 15 push @item_classes, 'disabled';
181 15 $content = '<span>'.$text.'</span>';
182 }
183  
184 30 my $item = '<li';
185 30 ▶ if (scalar @item_classes) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @item_classes) 21 9 30
186 21 $item .= ' class="'.join(' ', @item_classes).'"';
187 }
188  
189 30 return $item.'>'.$content.'</li>';
190 }
191  
192  
193 18 ▶sub _prev_next_uri {
Subroutine _prev_next_uri: called CC=2 SCAR=6.9
194 18 my ($self, $arg_ref) = @_;
195  
196 18 my $pager = $arg_ref->{pager};
197 return map {
198 18 ▶ $_ ? $self->_uri_for_page($_, $arg_ref) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 17 19 36
199 } ($pager->previous_page(), $pager->next_page());
200 }
201  
202  
203 34 ▶sub _uri_for_page {
Subroutine _uri_for_page: called CC=3 SCAR=11.0
204 34 my ($self, $page, $arg_ref) = @_;
205  
206 34 my $uri = $arg_ref->{uri};
207 34 ▶ if (! defined $uri || $uri eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $uri or $uri eq "") 2 32 34
Condition66%2/3
not defined $uri or $uri eq ""
l!l&&r!l&&!r
2032
MC/DC50%1/2
not defined $uri or $uri eq ""
not defined $uri $uri eq ""
Truth table66%2/3
not(A) or B
A= not defined $uriB= $uri eq ""
ABresult
1X1
011
000
208 2 $self->_throw("Required 'uri' parameter not passed");
209 }
210 32 $uri =~ s/__PAGE__/( $page + $arg_ref->{offset} ) * $arg_ref->{factor}/eg;
211 32 return encode_entities($uri);
212 }
213  
214  
215 4 ▶sub _throw {
Subroutine _throw: called CC=1 SCAR=0.0
216 4 my ($self, $error) = @_;
217 4 croak(Template::Exception->new('Bootstrap.Pagination', $error));
218 }
219  
220  
221 2 1;
222  
223  
224 __END__
↑ Summary