blib/lib/Data/SimplePaginator.pm

filter: Statementstmt 35.7% 15 / 42 Branchbran 11.1% 2 / 18 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 44.4% 4 / 9 Podpod 100% 5 / 5 totaltotal 30.9% 26 / 84 CC 12 SCAR 29.0
CC
12
Cov
26%
page
42.0
new
20.0
data
6.0
CRAP 18.2
↑ Summary
1 package Data::SimplePaginator;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ($VERSION) = '0.5';
6  
7 # Private method so we do not depend on presence of POSIX module:
8 1640 ▶sub _ceil {
Subroutine _ceil: called CC=2 SCAR=6.9
9 1640 my ($i) = @_;
10 1640 my $r = sprintf("%d",$i);
11 1640 ▶ $r += 1 if( $i > $r );
Branch: 1 branch
BranchTrueFalseTotal
if $i > $r 668 972 1640
12 1640 return $r;
13 }
14  
15 =pod
16  
17 =head1 NAME
18  
19 Data::SimplePaginator - data pagination without assumptions (I think)
20  
21 =head1 SYNOPSIS
22  
23 =for test begin
24  
25 my $paginator;
26  
27 =for test end
28  
29 # paginate the alphabet into groups of 10 letters each
30 $paginator = Data::SimplePaginator->new(10);
31 $paginator->data(A..Z);
32
33 # print the second page (K..T)
34 foreach( $paginator->page(2) ) {
35 print $_;
36 }
37  
38 =head1 DESCRIPTION
39  
40 This module helps me create pagination without a lot of fuss. I looked
41 at other pagination modules (see also, below) and their interfaces were
42 cumbersome for me.
43  
44 This module will NOT...
45  
46 =over
47  
48 =item Keep track of the current page for you
49  
50 =item Screw with your CGI or other environment
51  
52 =item Generate any HTML
53  
54 =item Print to stdout
55  
56 =back
57  
58 If you're using Template Toolkit you probably want to use Data::Pageset
59 or Data::SpreadPagination because they have lots of subs that work great
60 with the way TT is set up.
61  
62 =head1 METHODS
63  
64  
65  
66  
67 =head2 new
68  
69 =for test begin
70  
71 my $number_per_page = 10;
72
73 =for test end
74  
75 $paginator = Data::SimplePaginator->new();
76
77 $paginator = Data::SimplePaginator->new($number_per_page);
78
79 $paginator = Data::SimplePaginator->new($number_per_page, A..Z);
80  
81 Creates a new pagination object to split up data into sets of $number_per_page.
82 Default items per page is 10, and default data is the empty list.
83  
84 =cut
85  
86 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
87 0 my $type = shift;
88 0 ▶ my $class = ref($type) || $type;
Condition0%0/3
ref $type || $type
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $type || $type
ref $type $type
Truth table0%0/3
A || B
A= ref $typeB= $type
ABresult
1X1
011
000
89 0 my $self = {
90 'size' => 10,
91 'data' => [],
92 };
93 0 bless $self, $class;
94 0 ▶ if( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
95 0 $self->size( shift );
96 }
97 0 ▶ if( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
98 0 $self->data( @_ );
99 }
100 0 return $self;
101 }
102  
103  
104  
105  
106 =head2 data
107  
108 =for test begin
109  
110 my @items = ('orange','apple','banana','...');
111  
112 =for test end
113  
114 $paginator->data( @items );
115
116 my @all = $paginator->data;
117  
118 This method lets you set new data items for the paginator. It stores
119 a shallow copy of the array, not a reference to it.
120  
121 Return value is the current data array.
122  
123 =cut
124  
125 0 ▶sub data {
Subroutine data: not called CC=2 SCAR=17.9
Pod documented
126 0 my ($self,@items) = @_;
127 0 ▶ if( @items ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@items) 0 0 0
128 0 $self->{data} = [ @items ];
129 }
130 0 return @{$self->{data}};
131 }
132  
133  
134  
135  
136 =head2 size
137  
138 $paginator->size(15);
139
140 my $items_per_page = $paginator->size;
141
142 This method lets you set the size of the page, a.k.a. the number of items per page.
143  
144 Return value is the size of the page.
145  
146 =cut
147  
148 0 ▶sub size {
Subroutine size: not called CC=2 SCAR=17.9
Pod documented
149 0 my ($self,@p) = @_;
150 0 ▶ if( @p ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@p) 0 0 0
151 0 $self->{size} = shift @p;
152 }
153 0 return $self->{size};
154 }
155  
156  
157  
158  
159 =head2 pages
160  
161 my $number_of_pages = $paginator->pages;
162
163 Returns the number of pages based on the data you provide and the
164 number of items per page that you set.
165  
166 =cut
167  
168 0 ▶sub pages {
Subroutine pages: not called CC=1 SCAR=6.9
Pod documented
169 0 my ($self) = @_;
170 0 my @all = $self->data;
171 0 return _ceil(scalar(@all) / $self->size);
172 }
173  
174  
175 =head2 page
176  
177 my @contents = $paginator->page($number);
178  
179 my @first_page = $paginator->page(1);
180
181 my @last_page = $paginator->page( $paginator->pages );
182
183 The first page is page 1, the last page is number of pages.
184  
185 Returns items from @list that are on the specified page.
186 If you give an invalid/undefined page number or one that's out of
187 range for your data set, you get an empty list.
188  
189 =cut
190  
191 0 ▶sub page {
Subroutine page: not called CC=6 SCAR=37.4
Pod documented
192 0 my ($self,$num) = @_;
193 0 my @all = $self->data;
194 0 ▶ return (wantarray ? () : 0) unless defined $num;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $num 0 0 0
wantarray ? : 0 0 0
195 0 ▶ return (wantarray ? () : 0) if ($num < 1 || $num > $self->pages);
Branch: 2 branches
BranchTrueFalseTotal
if $num < 1 or $num > $self->pages 0 0 0
wantarray ? : 0 0 0
Condition0%0/3
$num < 1 or $num > $self->pages
l!l&&r!l&&!r
000
MC/DC0%0/2
$num < 1 or $num > $self->pages
$num < 1 $num > $self->pages
Truth table0%0/3
A or B
A= $num < 1B= $num > $self->pages
ABresult
1X1
011
000
196 0 my @data = splice(@all,($num - 1)*$self->size, $self->size);
197 0 return @data;
198 }
199  
200  
201  
202  
203 =head1 EXAMPLES
204  
205 use Data::SimplePaginator;
206  
207 =head2 paginate the alphabet into groups of 10 letters each
208  
209 $paginator = Data::SimplePaginator->new(10,A..Z);
210  
211 =head2 print just the first page, A .. J
212  
213 print "first page: ". join(" ", $paginator->page(1)) . "\n";
214  
215 =head2 print every page
216  
217 foreach my $page ( 1..$paginator->pages ) {
218 print "page $page: ". join(" ", $paginator->page($page)) . "\n";
219 }
220  
221 =head2 print just the last page, U .. Z
222  
223 print "last page: ". join(" ", $paginator->page($paginator->pages)) . "\n";
224  
225 =head2 add more elements to the paginator
226  
227 $paginator->data( $paginator->data, 1..4, map { lc } A..Z, 5..8 );
228
229 =head2 create a pageset paginator to group pages in sets of 3
230  
231 my $pageset = Data::SimplePaginator->new(3, 1..$paginator->pages);
232
233 =head2 print every page, grouping into pagesets
234  
235 foreach my $setnum ( 1..$pageset->pages ) {
236 print "pageset $setnum\n";
237 foreach my $page ( $pageset->page($setnum) ) {
238 print " page $page: ". join(" ", $paginator->page($page)) . "\n";
239 }
240 }
241  
242 =head2 print every page, grouping into pagesets, resetting page numbers
243  
244 foreach my $setnum ( 1..$pageset->pages ) {
245 print "pageset $setnum\n";
246 foreach my $page ( 1..$pageset->page($setnum) ) {
247 print " page $page: ". join(" ", $paginator->page( ($pageset->page($setnum))[$page-1])) . "\n";
248 }
249 }
250
251  
252 =head1 SEE ALSO
253  
254 The other paginators I looked at before deciding to write this one:
255  
256 =over
257  
258 =item HTML::Paginator
259  
260 =item Data::Paginated
261  
262 =item Data::Page (also: Data::Pageset, Data::SpreadPagination)
263  
264 =back
265  
266 =head1 AUTHOR
267  
268 Jonathan Buhacoff <jonathan@buhacoff.net>
269  
270 =head1 COPYRIGHT
271  
272 Copyright (C) 2004-2008 Jonathan Buhacoff. All rights reserved.
273  
274 =head1 LICENSE
275  
276 This library is free software and can be modified and distributed under the same
277 terms as Perl itself.
278  
279 =cut
280  
281 3 1;
↑ Summary