HiD/Pager.pm

filter: Statementstmt 62.6% 47 / 75 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 65.0% 13 / 20 Podpod 100% 4 / 4 totaltotal 57.6% 64 / 111 CC 7 SCAR 16.5
CC
7
Cov
54%
_page_url
20.0
next
6.0
next_page_url
6.0
CRAP 5.2
« HiD/Generator/AtomFeed.pm ↑ Summary HiD/Role/IsPost.pm »
1 # ABSTRACT: Class for paging thru sets of entries
2  
3  
4 package HiD::Pager;
5 8 $HiD::Pager::VERSION = '1.5';
6 8 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 # note: we also do 'with HiD::Role::DoesLogging', just later on because reasons.
8  
9 8 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use warnings qw/ FATAL utf8 /;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use open qw/ :std :utf8 /;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶use charnames qw/ :full /;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use feature qw/ unicode_strings /;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 8 ▶use Data::Page;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 8 ▶use String::Errf qw/ errf /;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24  
25 8 has entries => (
26 is => 'ro',
27 isa => 'ArrayRef[HiD_Post]' ,
28 traits => [ qw/ Array / ] ,
29 handles => { total_entries => 'count' } ,
30 required => 1 ,
31 );
32  
33  
34 8 has entries_per_page => (
35 is => 'ro' ,
36 isa => 'HiD_PosInt' ,
37 default => 10 ,
38 );
39  
40  
41 8 has hid => (
42 is => 'ro' ,
43 isa => 'HiD' ,
44 required => 1 ,
45 handles => [ qw/ get_config / ],
46 );
47 8 with 'HiD::Role::DoesLogging'; # needs to see the get_config delegation
48  
49  
50 8 has page_pattern => (
51 is => 'ro',
52 isa => 'Str' ,
53 default => 'blog/%{page}' ,
54 );
55  
56  
57 8 has pager => (
58 is => 'ro' ,
59 isa => 'Data::Page' ,
60 lazy => 1,
61 init_arg => undef ,
62 builder => '_build_pager' ,
63 handles => {
64 page_number => 'current_page' ,
65 next_page => 'next_page' ,
66 prev_page => 'previous_page',
67 splice => 'splice',
68 total_pages => 'last_page' ,
69 }
70 );
71  
72 sub _build_pager {
73 0 ▶ my( $self ) = @_;
Subroutine _build_pager: not called CC=1 SCAR=6.9
74  
75 0 my $pager = Data::Page->new();
76 0 $pager->total_entries($self->total_entries);
77 0 $pager->entries_per_page($self->entries_per_page);
78 0 $pager->current_page(1);
79  
80 0 return $pager;
81 }
82  
83 has _pager_page => (
84 is => 'rw' ,
85 isa => 'Maybe[Int]' ,
86 lazy => 1 ,
87 0 ▶ default => sub { shift->pager->current_page },
Subroutine __ANON__: not called CC=1 SCAR=6.9
88 8 );
89  
90  
91 sub current_page_url {
92 0 ▶ my $self = shift;
Subroutine current_page_url: not called CC=1 SCAR=6.9
Pod documented
93 0 return $self->_page_url( $self->page_number );
94 }
95  
96  
97 sub next {
98 0 ▶ my( $self ) = @_;
Subroutine next: not called CC=2 SCAR=17.9
Pod documented
99  
100 0 ▶ return undef unless defined $self->_pager_page;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_pager_page 0 0 0
101  
102 0 $self->page_number( $self->_pager_page );
103 0 my @page_posts = $self->splice( reverse( $self->entries ));
104  
105 0 $self->_pager_page( $self->next_page );
106  
107 return {
108 0 current_page_url => $self->current_page_url ,
109 next_page => $self->next_page ,
110 next_page_url => $self->next_page_url ,
111 page_number => $self->page_number ,
112 posts => \@page_posts,
113 prev_page => $self->prev_page ,
114 prev_page_url => $self->prev_page_url ,
115 total_pages => $self->total_pages ,
116 };
117 }
118  
119  
120 sub next_page_url {
121 0 ▶ my $self = shift;
Subroutine next_page_url: not called CC=2 SCAR=17.9
Pod documented
122 0 ▶ if ( my $next = $self->next_page ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = $self->next_page) 0 0 0
123 0 return $self->_page_url( $next );
124 }
125 }
126  
127  
128 sub prev_page_url {
129 0 ▶ my $self = shift;
Subroutine prev_page_url: not called CC=2 SCAR=17.9
Pod documented
130 0 ▶ if ( my $prev = $self->prev_page ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $prev = $self->prev_page) 0 0 0
131 0 return $self->_page_url( $prev );
132 }
133 }
134  
135 sub _page_url {
136 0 ▶ my( $self , $number ) = @_;
Subroutine _page_url: not called CC=4 SCAR=30.0
137  
138 0 ▶ confess('wtf') unless defined $number;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $number 0 0 0
139  
140 0 ▶ $number = '' if $number == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $number == 1 0 0 0
141  
142 0 my $url = errf $self->page_pattern , { page => $number };
143 0 ▶ $url .= '/index.html' unless $url =~ /html$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ /html$/u 0 0 0
144 0 $url =~ s|//|/|g;
145  
146 0 return $url;
147 }
148  
149 8 __PACKAGE__->meta->make_immutable;
150  
151 __END__
152  
153 =pod
154  
155 =encoding utf-8
156  
157 =head1 NAME
158  
159 HiD::Pager - Class for paging thru sets of entries
160  
161 =head1 SYNOPSIS
162  
163 To use pagination with just the blog pages, set the following config
164 options:
165  
166 pagination:
167 entries: 10
168 page: 'blog/%{page}s'
169 template: 'blog/index.html'
170  
171 C<pagination.entries> sets the number of entries per
172 page. C<pagination.page> sets the pattern for pages. C<pagination.template>
173 is the template file that will be used for each file in turn. A
174 C<index.html> will be appended to each page. Note that no 'page1' entry will
175 be generated; in the example above, the first page would be at
176 'blog/index.html', the second at 'blog/page2/index.html', and so on.
177  
178 If you need more control, or want to use pagination inside a
179 L<HiD::Generator>, you can instatiate one like so:
180  
181 my $pager = HiD::Pager->new({
182 entries => $site->posts ,
183 entries_per_page => 5 ,
184 hid => $site ,
185 page_pattern => 'blog/%{page}s' ,
186 });
187  
188 while( my $page_data = $pager->next() ) {
189 my $page = HiD::Page->new(
190 metadata => { page_data => $page_data },
191 # other page data here
192 );
193 # inject page into site, etc.
194 }
195  
196 # in page template, assuming Kolon template syntax
197 : for $page_data.posts -> $post {
198 : ## render page here
199 : }
200  
201 : # other useful info for creating intra-page links and metadata
202 : $page_data.current_page_url = url of current page
203 : $page_data.page_number = number of current page
204 : $page_data.total_pages = total number of pages
205 : $page_data.prev_page = number of previous page (undef if no previous)
206 : $page_data.prev_page_url = url of previous page (undef if no previous)
207 : $page_data.next_page = number of next page (undef if no next)
208 : $page_data.next_page_url = url of next page (undef if no next)
209  
210 =head1 DESCRIPTION
211  
212 Class providing pagination services for sets of posts. Can be used for main
213 blog post pages by setting up the appropriate configuration, or used inside a
214 C<HiD::Generator> class to provide paged sets of a subset of the posts on a
215 site.
216  
217 =head1 ATTRIBUTES
218  
219 =head2 entries
220  
221 Array of L<HiD::Post> objects being worked with
222  
223 =head2 entries_per_page
224  
225 Number of entries per page.
226  
227 =head2 hid
228  
229 All hail the God Object.
230  
231 =head2 page_pattern
232  
233 Regex used to generate per-page URLs
234  
235 =head2 pager
236  
237 The L<Data::Page> object that does all the work.
238  
239 =head1 METHODS
240  
241 =head2 current_page_url
242  
243 Returns the URL for the current page in the set.
244  
245 =head2 next
246  
247 Returns the data structure for the pager information.
248  
249 =head2 next_page_url
250  
251 Returns the URL for the next page in the set.
252  
253 =head2 prev_page_url
254  
255 Returns the URL for the previous page in the set.
256  
257 =head1 VERSION
258  
259 version 1.5
260  
261 =head1 AUTHOR
262  
263 John SJ Anderson <genehack@genehack.org>
264  
265 =head1 COPYRIGHT AND LICENSE
266  
267 This software is copyright (c) 2014 by John SJ Anderson.
268  
269 This is free software; you can redistribute it and/or modify it under
270 the same terms as the Perl 5 programming language system itself.
271  
272 =cut
« HiD/Generator/AtomFeed.pm ↑ Summary HiD/Role/IsPost.pm »