blib/lib/Data/Page/Set.pm

filter: Statementstmt 18.1% 12 / 66 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 12.0% 3 / 25 Podpod 16.6% 1 / 6 totaltotal 12.3% 16 / 130 CC 15 SCAR 29.3
CC
15
Cov
13%
show
56.0
new
30.0
pages_in_set
30.0
CRAP 18.8
↑ Summary
1 #!/usr/bin/perl
2  
3 package Data::Page::Set;
4  
5 =head1 NAME
6  
7 Data::Page::Set - Print page indexes
8  
9 =head1 SYNOPSIS
10  
11 use Data::Page;
12 use Data::Page::Set;
13  
14 my @data = 0 .. 300;
15 my $page = Data::Page->new( scalar @data, 5, shift );
16 my $pageset = Data::Page::Set->new( $page, 6, {} );
17  
18 print $pageset->show;
19  
20 =head1 DESCRIPTION
21  
22 =head2 Data::Page::Set->new( $page, $setsize, $showhash );
23  
24 =head4 Arguments
25  
26 =over 4
27  
28 =item C<$page> [Required]
29  
30 A Data::Page object.
31  
32 =item C<$setsize> [Required]
33  
34 The size of the pageset:
35 If you have a page object with 20 pages,
36 but you only want to show
37  
38 B<E<lt>E<lt>> B<E<lt>> B<4> B<5> 6 B<7> B<8> B<E<gt>> B<E<gt>E<gt>>
39  
40 Then setsize should be 5 because we're only
41 showing 5 page indexes.
42  
43 =item C<$showhash>
44  
45 A hash with zero or more of the following keys,
46 with a coderef as value wich is executed when we are about to print:
47  
48 =over 4
49  
50 =item show_first
51  
52 link to the first page
53  
54 =item show_no_first
55  
56 no link to the first page
57  
58 =item show_prev
59  
60 previous page link
61  
62 =item show_no_prev
63  
64 no link to the previous
65  
66 =item show_next
67  
68 next page link
69  
70 =item show_no_next
71  
72 No next page link
73  
74 =item show_last
75  
76 Last page link
77  
78 =item show_no_last
79  
80 No last page link
81  
82 =item show_page
83  
84 A link to another page
85  
86 =item show_current_page
87  
88 The current page
89  
90 =item grepper
91  
92 Executed in grep { $code->($_) } before the result is joined
93  
94 =item joiner
95  
96 Executed and used as the first argument to join
97  
98 =back
99  
100 =cut
101  
102 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
103 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
104 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
105  
106 1 $VERSION = '0.04';
107  
108 my $code = {
109 0 ▶ show_first => sub { qq(<a href="?page=$_[0]">&lt;&lt;First</a>) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
110 0 ▶ show_no_first => sub { qq() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
111 0 ▶ show_prev => sub { qq(<a href="?page=$_[0]">&lt;Previous</a>) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
112 0 ▶ show_no_prev => sub { qq() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
113 0 ▶ show_next => sub { qq(<a href="?page=$_[0]">&gt;Next</a>) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
114 0 ▶ show_no_next => sub { qq() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
115 0 ▶ show_last => sub { qq(<a href="?page=$_[0]">&gt;&gt;Last</a>) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
116 0 ▶ show_no_last => sub { qq() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
117 0 ▶ show_page => sub { qq(<a href="?page=$_[0]">$_[0]</a>) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
118 0 ▶ show_current_page => sub { qq($_[0]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
119 0 ▶ joiner => sub { qq(&nbsp;\n) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
120 0 ▶ grepper => sub { length $_[0] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
121 1 };
122  
123 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
124 0 my $class = shift;
125 0 my $pager = shift;
126 0 ▶ my $setsize = shift || 10;
Condition0%0/2
shift() || 10
l!l
00
MC/DC0%0/1
shift() || 10
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
127 0 my $show = shift;
128  
129 0 for my $key ( keys %$code ) {
130 $show->{$key} = $code->{$key}
131 unless exists $show->{$key}
132 0 ▶ and ref $show->{$key} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless exists $show->{$key} and ref $show->{$key} eq "CODE" 0 0 0
Condition0%0/3
exists $show->{$key} and ref $show->{$key} eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $show->{$key} and ref $show->{$key} eq "CODE"
exists $show->{$key} ref $show->{$key} eq "CODE"
Truth table0%0/3
A and B
A= exists $show->{$key}B= ref $show->{$key} eq "CODE"
ABresult
0X0
100
111
133 }
134  
135 0 my $self = bless {
136 pager => $pager,
137 show => $show,
138 setsize => $setsize,
139 }, $class;
140  
141 0 return $self;
142 }
143  
144 0 ▶sub show {
Subroutine show: not called CC=7 SCAR=40.3
Pod undocumented
145 0 my $self = shift;
146 0 ▶ my $show = shift || $self->{show};
Condition0%0/3
shift() || $self->{'show'}
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->{'show'}
shift() $self->{'show'}
Truth table0%0/3
A || B
A= shift()B= $self->{'show'}
ABresult
1X1
011
000
147 0 my $pager = $self->{pager};
148  
149 return join $show->{joiner}->(),
150 0 ▶ grep( { $show->{grepper}->($_) } (
Branch: 2 branches
BranchTrueFalseTotal
$self->page_in_set($pager->first_page) ? : 0 0 0
$pager->current_page == $pager->first_page ? : 0 0 0
151 $self->page_in_set($pager->first_page)
152 ? $show->{show_no_first}->($pager->first_page, $pager)
153 : $show->{show_first}->($pager->first_page, $pager),
154 $pager->current_page == $pager->first_page
155 ? $show->{show_no_prev}-> ($pager->previous_page, $pager)
156 : $show->{show_prev}->($pager->previous_page, $pager),
157 (map
158 {
159 $_ == $pager->current_page
160 ? $show->{show_current_page}->( $_, $pager )
161 0 ▶ : $show->{show_page}->( $_, $pager )
Branch: 3 branches
BranchTrueFalseTotal
$_ == $pager->current_page ? : 0 0 0
$pager->current_page == $pager->last_page ? : 0 0 0
$self->page_in_set($pager->last_page) ? : 0 0 0
162 } $self->pages_in_set()
163 ),
164 $pager->current_page == $pager->last_page
165 ? $show->{show_no_next}->($pager->next_page, $pager)
166 : $show->{show_next}->($pager->next_page, $pager),
167 $self->page_in_set($pager->last_page)
168 ? $show->{show_no_last}->($pager->last_page, $pager)
169 0 : $show->{show_last}->($pager->last_page, $pager),
170 ));
171 }
172  
173 0 ▶sub pages_in_set {
Subroutine pages_in_set: not called CC=5 SCAR=34.0
Pod undocumented
174 0 my $self = shift;
175  
176 0 my $cur = $self->{pager}->current_page;
177 0 my $len = $self->{setsize};
178 0 my $first = $self->{pager}->first_page;
179 0 my $last = $self->{pager}->last_page;
180  
181 0 my $pre;
182 0 ▶ my $post = sub { $len - $pre - 1 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
183 0 ▶ my $size = sub { $last - $first };
Subroutine __ANON__: not called CC=1 SCAR=6.9
184 0 ▶ my $first_show = sub { $cur - $pre };
Subroutine __ANON__: not called CC=1 SCAR=6.9
185 0 ▶ my $last_show = sub { $cur + $post->() };
Subroutine __ANON__: not called CC=1 SCAR=6.9
186  
187 0 ▶ return $first .. $last if $len > $size->();
Branch: 1 branch
BranchTrueFalseTotal
if $len > &$size() 0 0 0
188  
189 0 ▶ $pre = $len % 2 ? int $len / 2 : int( ($len - 1) / 2 );
Branch: 1 branch
BranchTrueFalseTotal
$len % 2 ? : 0 0 0
190  
191 0 ▶ $pre = $last_show->() > $last ? -$last + $cur + $len - 1
Branch: 2 branches
BranchTrueFalseTotal
&$last_show() > $last ? : 0 0 0
&$first_show() < $first ? : 0 0 0
192 : $first_show->() < $first ? -$first + $cur
193 : $pre;
194  
195 0 return $first_show->() .. $last_show->();
196 }
197  
198 0 ▶sub page_in_set {
Subroutine page_in_set: not called CC=1 SCAR=6.9
Pod undocumented
199 0 my $self = shift;
200 0 my $page = shift;
201  
202 return scalar grep
203 0 { $_ == $page }
204 $self->pages_in_set;
205 }
206  
207 0 ▶sub page_before_view {
Subroutine page_before_view: not called CC=1 SCAR=6.9
Pod undocumented
208 0 my $self = shift;
209 0 my @in_view = $self->pages_in_set;
210  
211 0 return $in_view[0] - 1;
212 }
213  
214 0 ▶sub page_after_view {
Subroutine page_after_view: not called CC=1 SCAR=6.9
Pod undocumented
215 0 my $self = shift;
216 0 my @in_view = $self->pages_in_set;
217  
218 0 return $in_view[-1] + 1;
219 }
220  
221 =head1 HISTORY
222  
223 0.02: Previous and next are show when current page
224 not is first and last resp.
225  
226 0.04: perl-5.6.1 compatible, tests added
227  
228 =head1 TODO
229  
230 =over 4
231  
232 =item * Improve joiner/grepper
233  
234 Joiner and grepper could be replaced with one routine that
235 constructs the return value.
236  
237 =item * Generic backend
238  
239 Data::Page is atm the only pager supported, but we could
240 do better than that.
241  
242 =back
243  
244 =head1 AUTHOR
245  
246 Berik Visschers <berikv@xs4all.nl>
247  
248 =head1 COPYRIGHT
249  
250 Copyright 2005 by Berik Visschers E<lt>berikv@xs4all.nlE<gt>.
251  
252 This program is free software; you can redistribute it and/or
253 modify it under the same terms as Perl itself.
254  
255 See F<http://www.perl.com/perl/misc/Artistic.html>
256  
257 =cut
258  
259 1 1
↑ Summary