line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Aniki::Plugin::PagerInjector; |
2
|
4
|
|
|
4
|
|
82282
|
use 5.014002; |
|
4
|
|
|
|
|
17
|
|
3
|
|
|
|
|
|
|
|
4
|
4
|
|
|
4
|
|
370
|
use namespace::autoclean; |
|
4
|
|
|
|
|
14631
|
|
|
4
|
|
|
|
|
27
|
|
5
|
4
|
|
|
4
|
|
707
|
use Mouse::Role; |
|
4
|
|
|
|
|
17365
|
|
|
4
|
|
|
|
|
29
|
|
6
|
4
|
|
|
4
|
|
3343
|
use Data::Page::NoTotalEntries; |
|
4
|
|
|
|
|
8413
|
|
|
4
|
|
|
|
|
150
|
|
7
|
4
|
|
|
4
|
|
1934
|
use Aniki::Result::Role::Pager; |
|
4
|
|
|
|
|
29
|
|
|
4
|
|
|
|
|
1336
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
requires qw/guess_result_class/; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub inject_pager_to_result { |
12
|
14
|
|
|
14
|
0
|
65
|
my ($self, $result, $opt) = @_; |
13
|
14
|
|
|
|
|
67
|
my $table_name = $result->table_name; |
14
|
|
|
|
|
|
|
|
15
|
14
|
|
|
|
|
80
|
my $has_next = $opt->{rows} < $result->count; |
16
|
14
|
100
|
|
|
|
59
|
if ($has_next) { |
17
|
5
|
|
|
|
|
16
|
my $result_class = ref $result; |
18
|
|
|
|
|
|
|
$result = $result_class->new( |
19
|
|
|
|
|
|
|
table_name => $table_name, |
20
|
|
|
|
|
|
|
handler => $self, |
21
|
5
|
|
|
|
|
54
|
row_datas => [@{$result->row_datas}[0..$result->count-2]], |
22
|
|
|
|
|
|
|
!$result->suppress_row_objects ? ( |
23
|
5
|
50
|
|
|
|
29
|
inflated_rows => [@{$result->inflated_rows}[0..$result->count-2]], |
|
5
|
|
|
|
|
93
|
|
24
|
|
|
|
|
|
|
) : (), |
25
|
|
|
|
|
|
|
suppress_row_objects => $result->suppress_row_objects, |
26
|
|
|
|
|
|
|
row_class => $result->row_class, |
27
|
|
|
|
|
|
|
); |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
my $pager = Data::Page::NoTotalEntries->new( |
31
|
|
|
|
|
|
|
entries_per_page => $opt->{rows}, |
32
|
|
|
|
|
|
|
current_page => $opt->{page}, |
33
|
14
|
|
|
|
|
390
|
has_next => $has_next, |
34
|
|
|
|
|
|
|
entries_on_this_page => $result->count, |
35
|
|
|
|
|
|
|
); |
36
|
14
|
50
|
|
|
|
215
|
$result->meta->does_role('Aniki::Result::Role::Pager') |
37
|
|
|
|
|
|
|
or Mouse::Util::apply_all_roles($result, 'Aniki::Result::Role::Pager'); |
38
|
14
|
|
|
|
|
12572
|
$result->pager($pager); |
39
|
|
|
|
|
|
|
|
40
|
14
|
|
|
|
|
108
|
return $result; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
1; |
44
|
|
|
|
|
|
|
__END__ |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=pod |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=encoding utf-8 |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=head1 NAME |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
Aniki::Plugin::PagerInjector - plus one pager injector |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=head1 SYNOPSIS |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
package MyDB; |
57
|
|
|
|
|
|
|
use Mouse v2.4.5; |
58
|
|
|
|
|
|
|
extends qw/Aniki/; |
59
|
|
|
|
|
|
|
with qw/Aniki::Plugin::PagerInjector/; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
package main; |
62
|
|
|
|
|
|
|
my $db = MyDB->new(...); |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
my ($page, $rows) = (1, 10); |
65
|
|
|
|
|
|
|
my ($limit, $offset) = ($rows + 1, ($page - 1) * $rows); |
66
|
|
|
|
|
|
|
my $result = $db->select('user', { type => 2 }, { limit => $limit, offset => $offset }); # => Aniki::Result::Collection |
67
|
|
|
|
|
|
|
$result = $db->inject_pager_to_result($result => { # => inject Aniki::Result::Role::Pager |
68
|
|
|
|
|
|
|
table_name => 'user', |
69
|
|
|
|
|
|
|
rows => $rows, |
70
|
|
|
|
|
|
|
page => $page, |
71
|
|
|
|
|
|
|
}) |
72
|
|
|
|
|
|
|
$result->pager; # => Data::Page::NoTotalEntries |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head1 SEE ALSO |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
L<perl> |
77
|
|
|
|
|
|
|
L<Data::Page::NoTotalEntries> |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head1 LICENSE |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
Copyright (C) karupanerura. |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
84
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head1 AUTHOR |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
karupanerura E<lt>karupa@cpan.orgE<gt> |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=cut |