Flute/Pager.pm untested

filter: Statementstmt 0.0% 0 / 56 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 91 CC 7 SCAR 20.1
CC
7
Cov
0%
pages
20.0
next
12.0
position_last
6.0
CRAP 7.5
1 package Template::Flute::Pager;
2  
3 use strict;
4 use warnings;
5  
6 use Moo;
7 use Sub::Quote;
8 use Data::Page;
9  
10 =head1 NAME
11  
12 Template::Flute::Pager - Data::Page class for Template::Flute
13  
14 =head1 SYNOPSIS
15  
16 $pager = Template::Flute::Pager->new;
17  
18 # set page size
19 $pager->page_size(10);
20  
21 # retrieve number of pages
22 $pager->pages;
23  
24 # retrieve current page (numering starts at 1)
25 $pager->current_page;
26  
27 # retrieve global position numbers for current page
28 $pager->position_first;
29 $pager->position_last;
30  
31 # select a page (numbering starts at 1)
32 $pager->select_page(5);
33  
34 =head1 ATTRIBUTES
35  
36 =head2 iterator
37  
38 Pager iterator.
39  
40 =cut
41  
42 has iterator => (
43 is => 'rw',
44 lazy => 1,
45 default => quote_sub q{return Data::Page->new;},
46 );
47  
48 =head2 page_size
49  
50 Page size (defaults to 0).
51  
52 =cut
53  
54 has page_size => (
55 is => 'rw',
56 lazy => 1,
57 default => quote_sub q{return 0;},
58 );
59  
60 =head2 page_position
61  
62 Page position (defaults to 0).
63  
64 =cut
65  
66 has page_position => (
67 is => 'ro',
68 lazy => 1,
69 default => quote_sub q{return 0;},
70 );
71  
72 =head1 METHODS
73  
74 =head2 pages
75  
76 Returns number of pages.
77  
78 =cut
79  
80 sub pages {
81 my $self = shift;
82 my ($count, $pages);
83  
84 $count = $self->iterator->total_entries;
85  
86 if ($self->page_size > 0) {
87 $pages = int($count / $self->page_size);
88 if ($count % $self->page_size) {
89 $pages++;
90 }
91 }
92 elsif ($count > 0) {
93 $pages = 1;
94 }
95 else {
96 $pages = 0;
97 }
98  
99 return $pages;
100 }
101  
102 =head2 current_page
103  
104 Returns current page, starting from 1.
105  
106 =cut
107  
108 sub current_page {
109 my $self = shift;
110  
111 $self->iterator->current_page;
112 }
113  
114 =head2 select_page {
115  
116 Select page. Page numbering starts at 1.
117  
118 =cut
119  
120 sub select_page {
121 my ($self, $page) = @_;
122 my ($new_position, $distance);
123  
124 $self->iterator->current_page($page);
125 }
126  
127 =head2 position_first
128  
129 Returns global position number of first item on current page.
130  
131 =cut
132  
133 sub position_first {
134 my $self = shift;
135  
136 return ($self->current_page - 1) * $self->page_size + 1;
137 }
138  
139 =head2 position_last
140  
141 Returns global position number of last item on current page.
142  
143 =cut
144  
145 sub position_last {
146 my $self = shift;
147 my $position;
148  
149 $position = $self->current_page * $self->page_size;
150  
151 if ($position > $self->count) {
152 $position = $self->count;
153 }
154  
155 return $position;
156 }
157  
158 =head2 next
159  
160 Returns next record or undef.
161  
162 =cut
163  
164 sub next {
165 my $self = shift;
166  
167 if ($self->page_size > 0) {
168 if ($self->page_position < $self->page_size) {
169 $self->{page_position}++;
170 return $self->iterator->next_page;
171 }
172 else {
173 # advance current page
174 $self->{current_page}++;
175 $self->{page_position} = 0;
176 return;
177 }
178 }
179 else {
180 return $self->iterator->next;
181 }
182 }
183  
184 =head2 count
185  
186 Returns number of records.
187  
188 =cut
189  
190 sub count {
191 my $self = shift;
192  
193 $self->iterator->total_entries;
194 }
195  
196 =head2 reset
197  
198 Resets iterator.
199  
200 =cut
201  
202 sub reset {
203 my $self = shift;
204  
205 $self->iterator->current_page(1);
206 }
207  
208 =head2 seed
209  
210 Seeds the iterator.
211  
212 =cut
213  
214 sub seed {
215 my ($self, $data) = @_;
216  
217 $self->iterator->seed($data);
218 }
219  
220 =head1 AUTHOR
221  
222 Stefan Hornburg (Racke), <racke@linuxia.de>
223  
224 =head1 LICENSE AND COPYRIGHT
225  
226 Copyright 2010-2016 Stefan Hornburg (Racke) <racke@linuxia.de>.
227  
228 This program is free software; you can redistribute it and/or modify it
229 under the terms of either: the GNU General Public License as published
230 by the Free Software Foundation; or the Artistic License.
231  
232 See http://dev.perl.org/licenses/ for more information.
233  
234 =head1 SEE ALSO
235  
236 L<Template::Flute::Iterator>
237  
238 =cut
239  
240 1;