line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mongol::Roles::Pagination; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
5927
|
use Moose::Role; |
|
2
|
|
|
|
|
22
|
|
|
2
|
|
|
|
|
17
|
|
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
7931
|
use Mongol::Models::Page; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
77
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use constant { |
8
|
2
|
|
|
|
|
372
|
PAGINATION_DEFAULT_START => 0, |
9
|
|
|
|
|
|
|
PAGINATION_DEFAULT_ROWS => 10, |
10
|
2
|
|
|
2
|
|
16
|
}; |
|
2
|
|
|
|
|
3
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
requires 'count'; |
13
|
|
|
|
|
|
|
requires 'find'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub paginate { |
16
|
0
|
|
|
0
|
1
|
|
my ( $class, $query, $start, $rows, $options ) = @_; |
17
|
|
|
|
|
|
|
|
18
|
0
|
|
0
|
|
|
|
$options ||= {}; |
19
|
0
|
|
0
|
|
|
|
$options->{skip} = $start || PAGINATION_DEFAULT_START; |
20
|
0
|
|
0
|
|
|
|
$options->{limit} = $rows || PAGINATION_DEFAULT_ROWS; |
21
|
|
|
|
|
|
|
|
22
|
0
|
|
|
|
|
|
my $total = $class->count( $query ); |
23
|
0
|
|
|
|
|
|
my @items = $class->find( $query, $options ) |
24
|
|
|
|
|
|
|
->all(); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
my $page = Mongol::Models::Page->new( |
27
|
|
|
|
|
|
|
{ |
28
|
|
|
|
|
|
|
items => \@items, |
29
|
|
|
|
|
|
|
total => $total, |
30
|
|
|
|
|
|
|
start => $options->{skip}, |
31
|
|
|
|
|
|
|
rows => $options->{limit}, |
32
|
|
|
|
|
|
|
} |
33
|
0
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
|
35
|
0
|
|
|
|
|
|
return $page; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
2
|
|
|
2
|
|
11
|
no Moose::Role; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
21
|
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
1; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
__END__ |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=pod |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=head1 NAME |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
Mongol::Roles::Pagination - Pagination for Mongol models |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=head1 SYNOPSIS |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
use POSIX qw( ceil ); |
53
|
|
|
|
|
|
|
use Data::Dumper; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
my $page = Models::Person->paginate( { age => { '$gt' => 25 } }, 0, 10 ); |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
my $total_pages = ceil( $page->total() / $page->rows() ); |
58
|
|
|
|
|
|
|
my $current_page = ( $page->start() / $page->rows() ) + 1; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
printf( "%s", Dumper( $page->serialize() ) ); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 DESCRIPTION |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head1 METHODS |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head2 paginate |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
my $page = Models::Person->paginate( { first_name => 'John' }, 0, 10, {} ); |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head1 SEE ALSO |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=over 4 |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=item * |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
L<Mongol::Models::Page> |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=back |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=cut |