File Coverage

blib/lib/Data/Page/Pagination.pm
Criterion Covered Total %
statement 71 71 100.0
branch 12 12 100.0
condition 14 18 77.7
subroutine 31 31 100.0
pod n/a
total 128 132 96.9


line stmt bran cond sub pod time code
1             package Data::Page::Pagination; ## no critic (TidyCode)
2              
3 1     1   87469 use Moose;
  1         601986  
  1         9  
4 1     1   9256 use Moose::Util::TypeConstraints;
  1         3  
  1         14  
5 1     1   3468 use MooseX::StrictConstructor;
  1         26429  
  1         7  
6 1     1   11624 use MooseX::Types::Moose qw(Int ArrayRef);
  1         68211  
  1         14  
7 1     1   5690 use List::Util qw(min max);
  1         2  
  1         69  
8 1     1   5 use namespace::autoclean;
  1         2  
  1         6  
9 1     1   866 use syntax qw(method);
  1         1175  
  1         5  
10              
11             our $VERSION = '0.005';
12              
13             subtype IntGreaterThan2 => (
14             as Int,
15             where { $_ > 2 },
16             );
17              
18             subtype IntGreaterThan0 => (
19             as Int,
20             where { $_ > 0 },
21             );
22              
23             class_type DataPage => {
24             class => 'Data::Page',
25             };
26              
27             has page => (
28             is => 'ro',
29             isa => 'DataPage',
30             required => 1,
31             );
32              
33             # methods that call page only
34 1     1   50546 method current_page { return $self->page->current_page }
  91     91   488  
  91         108  
  91         3520  
35 1     1   416 method first_page { return $self->page->first_page }
  40     40   712  
  40         42  
  40         1549  
36 1     1   675 method last_page { return $self->page->last_page }
  41     41   1378  
  41         47  
  41         1697  
37 1     1   408 method visible_previous_page { return defined $self->page->previous_page }
  26     26   33  
  26         29  
  26         987  
38 1     1   1151 method visible_next_page { return defined $self->page->next_page }
  26     26   269  
  26         30  
  26         990  
39              
40 1     1   487 method previous_page {
  8     8   913  
  8         11  
41             return
42 8   33     326 $self->page->previous_page
43             || $self->current_page;
44             }
45              
46 1     1   381 method next_page {
  9     9   1160  
  9         12  
47             return
48 9   33     357 $self->page->next_page
49             || $self->current_page;
50             }
51              
52             has page_numbers => (
53             is => 'ro',
54             isa => 'IntGreaterThan2',
55             required => 1,
56             );
57              
58             has max_list_length => (
59             is => 'rw',
60             isa => Int,
61             init_arg => undef,
62             lazy => 1,
63             default => method {
64             return int +( $self->page_numbers - 1 ) / 2
65             },
66             );
67              
68             has previous_pages => (
69             is => 'rw',
70             isa => ArrayRef['IntGreaterThan0'],
71             init_arg => undef,
72             lazy => 1,
73             default => method {
74             return [
75             max(
76             $self->first_page + 1,
77             $self->current_page - $self->max_list_length
78             ) .. $self->current_page - 1
79             ];
80             },
81             );
82              
83             has next_pages => (
84 1     1   1733 is => 'rw',
  13     13   1144  
  13         19  
85             isa => ArrayRef['IntGreaterThan0'],
86             init_arg => undef,
87 13   100     154 lazy => 1,
88             default => method {
89             return [
90 1     1   375 $self->current_page + 1
  13     13   445  
  13         15  
91             .. min(
92             $self->last_page - 1,
93 13   100     32 $self->current_page + $self->max_list_length
94             )
95             ];
96 1     1   914 },
  13     13   51  
  13         16  
97             );
98              
99 13   100     34 method visible_first_page {
100             return
101             $self->current_page == $self->first_page + 1
102             || @{ $self->previous_pages };
103 1     1   600 }
  13     13   23  
  13         16  
104              
105             method visible_last_page {
106 13   100     39 return
107             $self->current_page == $self->last_page - 1
108             || @{ $self->next_pages };
109             }
110 1     1   450  
  13     13   19  
  13         22  
111 13         850 method visible_hidden_previous {
112             return
113             $self->visible_previous_page
114             && @{ $self->previous_pages }
115 13         1155 && $self->previous_pages->[0] != $self->first_page + 1;
116             }
117 13 100       44  
    100          
    100          
    100          
    100          
    100          
118             method visible_hidden_next {
119             return
120             $self->visible_next_page
121             && @{ $self->next_pages }
122             && $self->next_pages->[-1] != $self->last_page - 1;
123             }
124              
125             method render_plaintext {
126             return join q{ },
127             $self->visible_previous_page ? $self->previous_page . q{<} : (),
128             $self->visible_first_page ? $self->first_page : (),
129             $self->visible_hidden_previous ? q{..} : (),
130             @{ $self->previous_pages },
131             q{[} . $self->current_page . q{]},
132             @{ $self->next_pages },
133             $self->visible_hidden_next ? q{..} : (),
134             $self->visible_last_page ? $self->last_page : (),
135             $self->visible_next_page ? q{>} . $self->next_page : ();
136             }
137              
138             __PACKAGE__->meta->make_immutable;
139              
140             # $Id$
141              
142             1;
143              
144             __END__
145              
146             =head1 NAME
147              
148             Data::Page::Pagination - calculates the pagination view
149              
150             =head1 VERSION
151              
152             0.005
153              
154             =head1 SYNOPSIS
155              
156             require Data::Page::Pagination;
157             require Data::Page;
158              
159             my $p = Data::Page::Pagination->new(
160             page => Data::Page->new(110, 10, 6),
161             page_numbers => 11,
162             );
163              
164             $p->visible_previous_page; # 5< ( $p->previous_page )
165             $p->visible_first_page; # 1 ( $p->first_page )
166             $p->visible_hidden_previous; # ..
167             @{ $p->previous_pages }; # 3 4 5 ( max_length = $p->max_list_length )
168             $p->page->current_page; # 6 ( $p->current_page )
169             @{ $p->next_pages }; # 7 8 9 ( max_length = $p->max_list_length )
170             $p->visible_hidden_next, # ..
171             $p->visible_last_page; # 11 ( $p->last_page )
172             $p->visible_next_page; # >7 ( $p->next_page )
173              
174             $p->render_plaintext eq '5< 1 .. 3 4 5 [6] 7 8 9 .. 11 >7';
175              
176             =head1 EXAMPLE
177              
178             Inside of this Distribution is a directory named example.
179             Run this *.pl files.
180              
181             =head1 DESCRIPTION
182              
183             This module calculates the pagination view using a Date::Page object.
184             The provided methods are simple enough to use them in a template system.
185              
186             =head1 SUBROUTINES/METHODS
187              
188             =head2 method new
189              
190             "page_numbers" is the count of pages for directly access.
191              
192             my $pagination = Data::Page::Pagination->new(
193             page => Data::Page->new(...),
194             page_numbers => $integer_greater_than_2,
195             );
196              
197             =head2 method current_page
198              
199             Returns the number of the current page
200              
201             $positive_integer = $pagination->current_page;
202              
203             =head2 method max_list_length
204              
205             Returns the maximal length of the list
206             that can be left or right of the current page.
207              
208             $positive_integer_or_zero = $pagination->max_list_length;
209              
210             =head2 method visible_previous_page, visible_last_page
211              
212             Returns boolean true if there is a previous/last page.
213              
214             $boolean = $pagination->visible_previous_page;
215             $boolean = $pagination->visible_last_page;
216              
217             =head2 method previous_page, last_page
218              
219             Returns the number of the previous/last page.
220              
221             $positive_integer = $pagination->previous_page;
222             $positive_integer = $pagination->last_page;
223              
224             =head2 method visible_first_page, visible_last_page
225              
226             Returns boolean true if the current page is not the fist/last page.
227              
228             $boolean = $pagination->visible_first_page;
229             $boolean = $pagination->visible_last_page;
230              
231             =head2 method first_page, last_page
232              
233             Returns the number of the first/last page.
234              
235             $positive_integer = $pagination->first_page;
236             $positive_integer = $pagination->last_page;
237              
238             =head2 method visible_hidden_previous, visible_hidden_next
239              
240             Returns boolean true if more pages then max_list_length pages
241             are between first/last page and current page.
242              
243             $boolean = $pagination->visible_hiddden_previous;
244             $boolean = $pagination->visible_hiddden_next;
245              
246             =head2 method previous_pages, next_pages
247              
248             Returns the page numbers before/after the current page,
249             not more then max_list_length.
250              
251             $array_ref = $pagination->previous_pages;
252             $array_ref = $pagination->next_pages;
253              
254             =head2 method render_plaintext
255              
256             Returns the test output.
257              
258             $string = $pagination->render_plaintext;
259              
260             =head1 DIAGNOSTICS
261              
262             Moose exceptions
263              
264             =head1 CONFIGURATION AND ENVIRONMENT
265              
266             nothing
267              
268             =head1 DEPENDENCIES
269              
270             L<Moose|Moose>
271              
272             L<Moose::Util::TypeConstraints|Moose::Util::TypeConstraints>
273              
274             L<MooseX::StrictConstructor|MooseX::StrictConstructor>
275              
276             L<MooseX::Types::Moose|MooseX::Types::Moose>
277              
278             L<List::Util|List::Util>
279              
280             L<namespace::autoclean|namespace::autoclean>
281              
282             L<syntax|syntax>
283              
284             L<Syntax::Feature::Method|Syntax::Feature::Method>
285              
286             =head1 INCOMPATIBILITIES
287              
288             none
289              
290             =head1 BUGS AND LIMITATIONS
291              
292             not known
293              
294             =head1 SEE ALSO
295              
296             L<Data::Page|Data::Page>
297              
298             =head1 AUTHOR
299              
300             Steffen Winkler
301              
302             =head1 LICENSE AND COPYRIGHT
303              
304             Copyright (c) 2012,
305             Steffen Winkler
306             C<< <steffenw at cpan.org> >>.
307             All rights reserved.
308              
309             This module is free software;
310             you can redistribute it and/or modify it
311             under the same terms as Perl itself.