HiD/Pager.pm

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