| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Aniki::Plugin::PagerInjector; | 
| 2 | 4 |  |  | 4 |  | 71643 | use 5.014002; | 
|  | 4 |  |  |  |  | 18 |  | 
| 3 |  |  |  |  |  |  |  | 
| 4 | 4 |  |  | 4 |  | 566 | use namespace::autoclean; | 
|  | 4 |  |  |  |  | 20017 |  | 
|  | 4 |  |  |  |  | 24 |  | 
| 5 | 4 |  |  | 4 |  | 750 | use Mouse::Role; | 
|  | 4 |  |  |  |  | 13374 |  | 
|  | 4 |  |  |  |  | 24 |  | 
| 6 | 4 |  |  | 4 |  | 2895 | use Data::Page::NoTotalEntries; | 
|  | 4 |  |  |  |  | 6571 |  | 
|  | 4 |  |  |  |  | 98 |  | 
| 7 | 4 |  |  | 4 |  | 1508 | use Aniki::Result::Role::Pager; | 
|  | 4 |  |  |  |  | 19 |  | 
|  | 4 |  |  |  |  | 925 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | requires qw/guess_result_class/; | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | sub inject_pager_to_result { | 
| 12 | 14 |  |  | 14 | 0 | 59 | my ($self, $result, $opt) = @_; | 
| 13 | 14 |  |  |  |  | 67 | my $table_name = $result->table_name; | 
| 14 |  |  |  |  |  |  |  | 
| 15 | 14 |  |  |  |  | 79 | my $has_next = $opt->{rows} < $result->count; | 
| 16 | 14 | 100 |  |  |  | 57 | if ($has_next) { | 
| 17 | 5 |  |  |  |  | 14 | my $result_class = ref $result; | 
| 18 |  |  |  |  |  |  | $result = $result_class->new( | 
| 19 |  |  |  |  |  |  | table_name           => $table_name, | 
| 20 |  |  |  |  |  |  | handler              => $self, | 
| 21 | 5 |  |  |  |  | 48 | row_datas            => [@{$result->row_datas}[0..$result->count-2]], | 
| 22 |  |  |  |  |  |  | !$result->suppress_row_objects ? ( | 
| 23 | 5 | 50 |  |  |  | 27 | inflated_rows    => [@{$result->inflated_rows}[0..$result->count-2]], | 
|  | 5 |  |  |  |  | 84 |  | 
| 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 |  |  |  |  | 75 | has_next             => $has_next, | 
| 34 |  |  |  |  |  |  | entries_on_this_page => $result->count, | 
| 35 |  |  |  |  |  |  | ); | 
| 36 | 14 | 50 |  |  |  | 209 | $result->meta->does_role('Aniki::Result::Role::Pager') | 
| 37 |  |  |  |  |  |  | or Mouse::Util::apply_all_roles($result, 'Aniki::Result::Role::Pager'); | 
| 38 | 14 |  |  |  |  | 13660 | $result->pager($pager); | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 14 |  |  |  |  | 114 | 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 |