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

filter: Statementstmt 98.9% 93 / 94 Branchbran 97.3% 37 / 38 Conditioncond 80.7% 21 / 26 MC/DCmcdc 60.0% 9 / 15 Subroutinesub 100% 15 / 15 Podpod 100% 3 / 3 totaltotal 93.1% 178 / 191 SCAR 33.7
CC
29
Cov
96%
pagination
16.0
pager
6.0
new
4.1
CRAP 29.1
↑ Summary Build »
1 package Template::Plugin::Bootstrap::Pagination;
2 {
3 2 $Template::Plugin::Bootstrap::Pagination::VERSION = '0.001002';
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 sub new {
20 9 ▶ my ($class, $context, $arg_ref) = @_;
Subroutine new: called CC=4 SCAR=14.2
Pod documented
21  
22 9 my $self = $class->next::method($context, $arg_ref);
23  
24 9 ▶ if (defined $arg_ref && ref $arg_ref ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg_ref and ref $arg_ref ne "HASH") 0 9 9
Condition66%2/3
defined $arg_ref and ref $arg_ref ne "HASH"
!ll&&!rl&&r
630
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 9 ▶ $arg_ref ||= {};
Condition100%2/2
$arg_ref ||= {}
l!l
36
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 9 %{$arg_ref},
37 };
38  
39 9 return $self;
40 }
41  
42  
43  
44 sub pagination {
45 7 ▶ my ($self, $arg_ref) = @_;
Subroutine pagination: called CC=16 SCAR=27.7
Pod documented
46  
47 $arg_ref = {
48 7 %{$self->{default}},
49 7 ▶ %{$arg_ref || {}},
Branch: 1 branch
BranchTrueFalseTotal
$arg_ref or {} 3 4 7
50 };
51  
52 7 my $pager = $arg_ref->{pager};
53 7 ▶ unless (blessed $pager && $pager->isa('Data::Page')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($pager) and $pager->isa("Data::Page")) 1 6 7
Condition66%2/3
builtin::blessed($pager) and $pager->isa("Data::Page")
!ll&&!rl&&r
106
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
54 1 $self->_throw("Required 'pager' parameter not passed or not a 'Data::Page' instance");
55 }
56  
57 6 my $pagination = '';
58 6 ▶ if ($pager->total_entries() > $pager->entries_per_page()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pager->total_entries > $pager->entries_per_page) 4 2 6
59 4 my $current_page = $pager->current_page();
60 4 my $first_page = $pager->first_page();
61 4 my $last_page = $pager->last_page();
62 4 my $page = $first_page;
63 4 ▶ PAGE: while ($page <= $last_page) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page <= $last_page) 18 3 21
64 18 ▶ if ($current_page == $page) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_page == $page) { } 4 14 18
65 4 $pagination .= '<li class="active">'
66 . '<span>'.$page.'</span>'
67 . '</li>';
68 }
69 else {
70 14 ▶ 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) { } 10 4 14
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
1004
$page == $first_page or $page == $last_page or abs $page - $current_page <= $arg_ref->{"siblings"}
l!l&&r!l&&!r
554
$page == $first_page or $page == $last_page
l!l&&r!l&&!r
329
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
71 || abs($page - $current_page) <= ($arg_ref->{siblings})
72 || $last_page <= (2 * $arg_ref->{siblings} + 1)) {
73 10 $pagination .= '<li>'
74 . '<a href="'.$self->_uri_for_page($page, $arg_ref).'">'.$page.'</a>'
75 . '</li>';
76 }
77 ▶ 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
78 2 $pagination .= '<li class="disabled">'
79 . '<span>&hellip;</span>'
80 . '</li>';
81 }
82 else {
83 $page = ($page < $current_page)
84 ? $current_page - $arg_ref->{siblings}
85 2 ▶ : $last_page - 1;
Branch: 1 branch
BranchTrueFalseTotal
$page < $current_page ? : 1 1 2
86 2 next PAGE;
87 }
88 }
89 15 $page++;
90 }
91 }
92  
93 my $alignment = $arg_ref->{centered}
94 ? ' pagination-centered'
95 5 ▶ : ($arg_ref->{right} ? ' pagination-right' : '');
Branch: 2 branches
BranchTrueFalseTotal
$arg_ref->{'centered'} ? : 1 4 5
$arg_ref->{'right'} ? : 1 3 4
96 5 my ($prev_uri, $next_uri) = $self->_prev_next_uri($arg_ref);
97 return '<div class="pagination'.$alignment.'">'
98 . '<ul>'
99 . $self->_pager_item($prev_uri, $arg_ref->{prev_text})
100 . $pagination
101 . $self->_pager_item($next_uri, $arg_ref->{next_text})
102 5 . '</ul>'
103 . '</div>';
104 }
105  
106  
107  
108 sub pager {
109 5 ▶ my ($self, $arg_ref) = @_;
Subroutine pager: called CC=6 SCAR=17.9
Pod documented
110  
111 $arg_ref = {
112 5 %{$self->{default}},
113 5 ▶ %{$arg_ref || {}},
Branch: 1 branch
BranchTrueFalseTotal
$arg_ref or {} 2 3 5
114 };
115  
116 5 my $pager = $arg_ref->{pager};
117 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
118 1 $self->_throw("Required 'pager' parameter not passed or not a 'Data::Page' instance");
119 }
120  
121 4 my ($prev_uri, $next_uri) = $self->_prev_next_uri($arg_ref);
122 my $prev_page = $self->_pager_item(
123 3 ▶ $prev_uri, $arg_ref->{prev_text}, $arg_ref->{align} ? 'previous' : ()
Branch: 1 branch
BranchTrueFalseTotal
$arg_ref->{'align'} ? : 2 1 3
124 );
125 my $next_page = $self->_pager_item(
126 3 ▶ $next_uri, $arg_ref->{next_text}, $arg_ref->{align} ? 'next' : ()
Branch: 1 branch
BranchTrueFalseTotal
$arg_ref->{'align'} ? : 2 1 3
127 );
128  
129 3 return '<ul class="pager">'
130 . $prev_page
131 . $next_page
132 . '</ul>';
133 }
134  
135  
136 sub _pager_item {
137 20 ▶ my ($self, $uri, $text, @item_classes) = @_;
Subroutine _pager_item: called CC=3 SCAR=11.0
138  
139 20 my $content;
140 20 ▶ if (defined $uri) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $uri) { } 14 6 20
141 14 $content = '<a href="'.$uri.'">'.$text.'</a>';
142 }
143 else {
144 6 push @item_classes, 'disabled';
145 6 $content = '<span>'.$text.'</span>';
146 }
147  
148 20 my $item = '<li';
149 20 ▶ if (scalar @item_classes) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @item_classes) 12 8 20
150 12 $item .= ' class="'.join(' ', @item_classes).'"';
151 }
152  
153 20 return $item.'>'.$content.'</li>';
154 }
155  
156  
157 sub _prev_next_uri {
158 13 ▶ my ($self, $arg_ref) = @_;
Subroutine _prev_next_uri: called CC=2 SCAR=6.9
159  
160 13 my $pager = $arg_ref->{pager};
161 return map {
162 13 ▶ $_ ? $self->_uri_for_page($_, $arg_ref) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 16 10 26
163 } ($pager->previous_page(), $pager->next_page());
164 }
165  
166  
167 sub _uri_for_page {
168 32 ▶ my ($self, $page, $arg_ref) = @_;
Subroutine _uri_for_page: called CC=3 SCAR=11.0
169  
170 32 my $uri = $arg_ref->{uri};
171 32 ▶ if (! defined $uri || $uri eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $uri or $uri eq "") 2 30 32
Condition66%2/3
not defined $uri or $uri eq ""
l!l&&r!l&&!r
2030
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
172 2 $self->_throw("Required 'uri' parameter not passed");
173 }
174 30 $uri =~ s/__PAGE__/( $page + $arg_ref->{offset} ) * $arg_ref->{factor}/eg;
175 30 return encode_entities($uri);
176 }
177  
178  
179 sub _throw {
180 4 ▶ my ($self, $error) = @_;
Subroutine _throw: called CC=1 SCAR=0.0
181 4 croak(Template::Exception->new('Bootstrap.Pagination', $error));
182 }
183  
184  
185 2 1;
186  
187  
188 __END__
↑ Summary Build »