Paginator.pm

filter: Statementstmt 92.7% 89 / 96 Branchbran 84.0% 37 / 44 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 94.7% 18 / 19 Podpod 90.9% 10 / 11 totaltotal 90.8% 159 / 175 CC 24 SCAR 10.4
CC
24
Cov
90%
first_set
6.0
__ANON__
4.1
page_for
4.0
CRAP 2.8
↑ Summary Paginator/Types.pm »
1 package Data::Paginator;
2 6 $Data::Paginator::VERSION = '0.08';
3 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: Pagination with Moose
6  
7 6 ▶use Data::Paginator::Types qw(PositiveInt);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use MooseX::Types::Moose qw(Maybe);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 6 has current_page => (
12 is => 'ro',
13 isa => 'Num',
14 default => 1
15 );
16  
17 6 has current_set => (
18 is => 'ro',
19 isa => Maybe[PositiveInt],
20 lazy_build => 1
21 );
22  
23  
24 6 has entries_per_page => (
25 is => 'ro',
26 isa => PositiveInt,
27 required => 1
28 );
29  
30  
31 6 has last_page => (
32 is => 'ro',
33 isa => PositiveInt,
34 lazy_build => 1
35 );
36  
37  
38 6 has next_set => (
39 is => 'ro',
40 isa => Maybe[PositiveInt],
41 lazy_build => 1
42 );
43  
44  
45 6 has pages_per_set => (
46 is => 'ro',
47 isa => Maybe[PositiveInt],
48 predicate => 'has_pages_per_set'
49 );
50  
51  
52 6 has previous_set => (
53 is => 'ro',
54 isa => Maybe[PositiveInt],
55 lazy_build => 1
56 );
57  
58  
59 6 has total_entries => (
60 is => 'ro',
61 isa => PositiveInt,
62 required => 1
63 );
64  
65 # This facilitates incorrect page numbers.
66 459 ▶around 'current_page' => sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
67 459 my ($orig, $self) = @_;
68  
69 459 my $attr = $self->meta->find_attribute_by_name('current_page');
70 459 my $val = $attr->get_value($self);
71 459 ▶ if(!defined($val)) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $val) { } 0 459 459
elsif ($val < 1) { } 2 457 459
elsif ($val > $self->last_page) { } 1 456 457
72 0 $attr->set_value($self, 1);
73 0 return 1
74 } elsif($val < 1) {
75 2 $attr->set_value($self, 1);
76 2 return 1;
77 } elsif($val > $self->last_page) {
78 1 $attr->set_value($self, $self->last_page);
79 1 return $self->last_page;
80 }
81  
82 456 return $val;
83 6 };
84  
85 3 ▶sub _build_current_set {
Subroutine _build_current_set: called CC=1 SCAR=0.0
86 3 my ($self) = @_;
87  
88 3 return $self->set_for($self->current_page);
89 }
90  
91 41 ▶sub _build_last_page {
Subroutine _build_last_page: called CC=3 SCAR=11.0
92 41 my ($self) = @_;
93  
94 41 my $pages = $self->total_entries / $self->entries_per_page;
95 41 my $last_page;
96  
97 41 ▶ if ($pages == int $pages) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pages == int $pages) { } 15 26 41
98 15 $last_page = $pages;
99 } else {
100 26 $last_page = 1 + int($pages);
101 }
102  
103 41 ▶ $last_page = 1 if $last_page < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $last_page < 1 2 39 41
104 41 return $last_page;
105 }
106  
107 3 ▶sub _build_next_set {
Subroutine _build_next_set: called CC=3 SCAR=11.0
108 3 my ($self) = @_;
109  
110 3 ▶ return undef unless $self->pages_per_set;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->pages_per_set 0 3 3
111  
112 3 my $next = $self->current_set * ($self->pages_per_set * $self->entries_per_page) + 1;
113 3 ▶ return undef if $next > $self->total_entries;
Branch: 1 branch
BranchTrueFalseTotal
if $next > $self->total_entries 1 2 3
114 2 return $next;
115 }
116  
117 3 ▶sub _build_previous_set {
Subroutine _build_previous_set: called CC=3 SCAR=11.0
118 3 my ($self) = @_;
119  
120 3 ▶ return undef unless $self->pages_per_set;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->pages_per_set 0 3 3
121 3 my $cset = $self->current_set;
122 3 ▶ return undef if $cset == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $cset == 1 1 2 3
123  
124 2 return ($cset - 2) * $self->pages_per_set * $self->entries_per_page + 1;
125 }
126  
127  
128 35 ▶sub entries_on_this_page {
Subroutine entries_on_this_page: called CC=2 SCAR=6.9
Pod documented
129 35 my ($self) = @_;
130  
131 35 ▶ if ($self->total_entries == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->total_entries == 0) { } 1 34 35
132 1 return 0;
133 } else {
134 34 return $self->last - $self->first + 1;
135 }
136 }
137  
138  
139 138 ▶sub first {
Subroutine first: called CC=2 SCAR=6.9
Pod documented
140 138 my ($self) = @_;
141  
142 138 ▶ if ($self->total_entries == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->total_entries == 0) { } 2 136 138
143 2 return 0;
144 } else {
145 136 return (($self->current_page - 1) * $self->entries_per_page) + 1;
146 }
147 }
148  
149  
150 35 ▶sub first_page {
Subroutine first_page: called CC=1 SCAR=0.0
Pod documented
151 35 my ($self) = @_;
152 35 return 1;
153 }
154  
155  
156 0 ▶sub first_set {
Subroutine first_set: not called CC=2 SCAR=17.9
Pod documented
157 0 my ($self) = @_;
158  
159 0 ▶ if($self->has_pages_per_set) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_pages_per_set) 0 0 0
160 0 return 1;
161 }
162  
163 0 return undef;
164 }
165  
166  
167 120 ▶sub last {
Subroutine last: called CC=2 SCAR=6.9
Pod documented
168 120 my $self = shift;
169  
170 120 ▶ if ($self->current_page == $self->last_page) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->current_page == $self->last_page) { } 60 60 120
171 60 return $self->total_entries;
172 } else {
173 60 return ($self->current_page * $self->entries_per_page);
174 }
175 }
176  
177  
178 35 ▶sub next_page {
Subroutine next_page: called CC=2 SCAR=6.9
Pod documented
179 35 my $self = shift;
180  
181 35 ▶ $self->current_page < $self->last_page ? $self->current_page + 1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->current_page < $self->last_page ? : 15 20 35
182 }
183  
184  
185 7 ▶sub page_for {
Subroutine page_for: called CC=4 SCAR=13.9
Pod documented
186 7 my ($self, $num) = @_;
187  
188 7 ▶ return undef if $num > $self->total_entries || $num < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $num > $self->total_entries or $num < 1 2 5 7
Condition100%3/3
$num > $self->total_entries or $num < 1
l!l&&r!l&&!r
115
MC/DC100%2/2
$num > $self->total_entries or $num < 1
$num > $self->total_entries $num < 1
Truth table100%3/3
A or B
A= $num > $self->total_entriesB= $num < 1
ABresult
1X1
011
000
189  
190 5 my $page = $num / $self->entries_per_page;
191 5 ▶ if($page > int($page)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page > int $page) 3 2 5
192 3 return int($page) + 1;
193 }
194  
195 2 return $page;
196 }
197  
198  
199 35 ▶sub previous_page {
Subroutine previous_page: called CC=2 SCAR=6.9
Pod documented
200 35 my ($self) = @_;
201  
202 35 ▶ if ($self->current_page > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->current_page > 1) { } 15 20 35
203 15 return $self->current_page - 1;
204 } else {
205 20 return undef;
206 }
207 }
208  
209  
210 5 ▶sub set_for {
Subroutine set_for: called CC=3 SCAR=11.8
Pod documented
211 5 my ($self, $num) = @_;
212  
213 5 ▶ return undef unless $self->has_pages_per_set;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_pages_per_set 0 5 5
214  
215 5 my $set = $num / $self->pages_per_set;
216 5 ▶ if(int($set) != $set) {
Branch: 1 branch
BranchTrueFalseTotal
if (int $set != $set) 5 0 5
217 5 return int($set) + 1;
218 }
219  
220 0 return $set;
221 }
222  
223 35 ▶sub skipped {
Subroutine skipped: called CC=2 SCAR=6.9
Pod undocumented
224 35 my $self = shift;
225  
226 35 my $skipped = $self->first - 1;
227 35 ▶ return 0 if $skipped < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $skipped < 0 1 34 35
228 34 return $skipped;
229 }
230  
231  
232 36 ▶sub splice {
Subroutine splice: called CC=3 SCAR=11.0
Pod documented
233 36 my ($self, $array) = @_;
234  
235 36 ▶ my $top = @$array > $self->last ? $self->last : @$array;
Branch: 1 branch
BranchTrueFalseTotal
@$array > $self->last ? : 15 21 36
236 36 ▶ return () if $top == 0; # empty
Branch: 1 branch
BranchTrueFalseTotal
if $top == 0 2 34 36
237 34 return @{$array}[ $self->first - 1 .. $top - 1 ];
238 }
239  
240  
241 6 1;
242  
243 __END__
↑ Summary Paginator/Types.pm »