| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Data::Page; # git description: v2.04-3-gaabb6e5 | 
| 2 |  |  |  |  |  |  | # ABSTRACT: Help when paging through sets of results | 
| 3 | 3 |  |  | 3 |  | 57123 | use Carp (); | 
|  | 3 |  |  |  |  | 13 |  | 
|  | 3 |  |  |  |  | 70 |  | 
| 4 | 3 |  |  | 3 |  | 11 | use strict; | 
|  | 3 |  |  |  |  | 4 |  | 
|  | 3 |  |  |  |  | 57 |  | 
| 5 | 3 |  |  | 3 |  | 11 | use warnings; | 
|  | 3 |  |  |  |  | 4 |  | 
|  | 3 |  |  |  |  | 66 |  | 
| 6 | 3 |  |  | 3 |  | 11 | use base 'Class::Accessor::Chained::Fast'; | 
|  | 3 |  |  |  |  | 12 |  | 
|  | 3 |  |  |  |  | 1522 |  | 
| 7 |  |  |  |  |  |  | __PACKAGE__->mk_accessors(qw(total_entries entries_per_page current_page)); | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | our $VERSION = '2.05'; # TRIAL | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | sub new { | 
| 12 | 76 |  |  | 76 | 1 | 46386 | my $class = shift; | 
| 13 | 76 |  |  |  |  | 159 | my $self  = {}; | 
| 14 | 76 |  |  |  |  | 113 | bless( $self, $class ); | 
| 15 |  |  |  |  |  |  |  | 
| 16 | 76 |  |  |  |  | 138 | my ( $total_entries, $entries_per_page, $current_page ) = @_; | 
| 17 | 76 |  | 100 |  |  | 283 | $self->total_entries( $total_entries       || 0 ); | 
| 18 | 76 |  | 100 |  |  | 890 | $self->entries_per_page( $entries_per_page || 10 ); | 
| 19 | 75 |  | 100 |  |  | 947 | $self->current_page( $current_page         || 1 ); | 
| 20 | 75 |  |  |  |  | 535 | return $self; | 
| 21 |  |  |  |  |  |  | } | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | sub entries_per_page { | 
| 24 | 183 |  |  | 183 | 1 | 590 | my $self             = shift; | 
| 25 | 183 | 100 |  |  |  | 297 | if (@_) { | 
| 26 | 182 | 100 |  |  |  | 571 | Carp::croak("Fewer than one entry per page!") if $_[0] < 1; | 
| 27 | 181 |  |  |  |  | 285 | return $self->_entries_per_page_accessor(@_); | 
| 28 |  |  |  |  |  |  | } | 
| 29 | 1 |  |  |  |  | 3 | return $self->_entries_per_page_accessor(); | 
| 30 |  |  |  |  |  |  | } | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | sub current_page { | 
| 33 | 333 |  |  | 333 | 1 | 3311 | my $self = shift; | 
| 34 | 333 | 100 |  |  |  | 603 | if (@_) { | 
| 35 | 187 |  |  |  |  | 226 | my $cp = $_[0]; | 
| 36 | 187 |  |  |  |  | 329 | my $fp = $self->first_page; | 
| 37 | 187 | 100 |  |  |  | 321 | return $self->_current_page_accessor($fp) if $cp < $fp; | 
| 38 | 183 |  |  |  |  | 242 | my $lp = $self->last_page; | 
| 39 | 183 | 100 |  |  |  | 338 | return $self->_current_page_accessor($lp) if $cp > $lp; | 
| 40 | 180 |  |  |  |  | 374 | return $self->_current_page_accessor(@_); | 
| 41 |  |  |  |  |  |  | } | 
| 42 | 146 |  |  |  |  | 267 | return $self->_current_page_accessor; | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | sub total_entries { | 
| 46 | 113 |  |  | 113 | 1 | 1000 | my $self = shift; | 
| 47 | 113 | 100 |  |  |  | 193 | if (@_) { | 
| 48 | 112 |  |  |  |  | 217 | return $self->_total_entries_accessor(@_); | 
| 49 |  |  |  |  |  |  | } | 
| 50 | 1 |  |  |  |  | 3 | return $self->_total_entries_accessor; | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | sub entries_on_this_page { | 
| 54 | 72 |  |  | 72 | 1 | 33977 | my $self = shift; | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 72 | 100 |  |  |  | 176 | if ( $self->_total_entries_accessor == 0 ) { | 
| 57 | 3 |  |  |  |  | 26 | return 0; | 
| 58 |  |  |  |  |  |  | } else { | 
| 59 | 69 |  |  |  |  | 517 | return $self->last - $self->first + 1; | 
| 60 |  |  |  |  |  |  | } | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | sub first_page { | 
| 64 | 260 |  |  | 260 | 1 | 28529 | my $self = shift; | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 260 |  |  |  |  | 635 | return 1; | 
| 67 |  |  |  |  |  |  | } | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | sub last_page { | 
| 70 | 568 |  |  | 568 | 1 | 2020 | my $self = shift; | 
| 71 |  |  |  |  |  |  |  | 
| 72 | 568 |  |  |  |  | 965 | my $pages = $self->_total_entries_accessor / $self->_entries_per_page_accessor; | 
| 73 | 568 |  |  |  |  | 5701 | my $last_page; | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 568 | 100 |  |  |  | 1343 | if ( $pages == int $pages ) { | 
| 76 | 168 |  |  |  |  | 210 | $last_page = $pages; | 
| 77 |  |  |  |  |  |  | } else { | 
| 78 | 400 |  |  |  |  | 571 | $last_page = 1 + int($pages); | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 568 | 100 |  |  |  | 1087 | $last_page = 1 if $last_page < 1; | 
| 82 | 568 |  |  |  |  | 1401 | return $last_page; | 
| 83 |  |  |  |  |  |  | } | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | sub first { | 
| 86 | 351 |  |  | 351 | 1 | 828 | my $self = shift; | 
| 87 |  |  |  |  |  |  |  | 
| 88 | 351 | 100 |  |  |  | 747 | if ( $self->_total_entries_accessor == 0 ) { | 
| 89 | 8 |  |  |  |  | 60 | return 0; | 
| 90 |  |  |  |  |  |  | } else { | 
| 91 | 343 |  |  |  |  | 2336 | return ( ( $self->_current_page_accessor - 1 ) * $self->_entries_per_page_accessor ) + 1; | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | sub last { | 
| 96 | 243 |  |  | 243 | 1 | 44062 | my $self = shift; | 
| 97 |  |  |  |  |  |  |  | 
| 98 | 243 | 100 |  |  |  | 453 | if ( $self->_current_page_accessor == $self->last_page ) { | 
| 99 | 123 |  |  |  |  | 222 | return $self->_total_entries_accessor; | 
| 100 |  |  |  |  |  |  | } else { | 
| 101 | 120 |  |  |  |  | 230 | return ( $self->_current_page_accessor * $self->_entries_per_page_accessor ); | 
| 102 |  |  |  |  |  |  | } | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | sub previous_page { | 
| 106 | 71 |  |  | 71 | 1 | 36483 | my $self = shift; | 
| 107 |  |  |  |  |  |  |  | 
| 108 | 71 | 100 |  |  |  | 263 | if ( $self->_current_page_accessor > 1 ) { | 
| 109 | 30 |  |  |  |  | 200 | return $self->_current_page_accessor - 1; | 
| 110 |  |  |  |  |  |  | } else { | 
| 111 | 41 |  |  |  |  | 358 | return undef; | 
| 112 |  |  |  |  |  |  | } | 
| 113 |  |  |  |  |  |  | } | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | sub next_page { | 
| 116 | 71 |  |  | 71 | 1 | 34716 | my $self = shift; | 
| 117 |  |  |  |  |  |  |  | 
| 118 | 71 | 100 |  |  |  | 154 | $self->_current_page_accessor < $self->last_page ? $self->_current_page_accessor + 1 : undef; | 
| 119 |  |  |  |  |  |  | } | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | # This method would probably be better named 'select' or 'slice' or | 
| 122 |  |  |  |  |  |  | # something, because it doesn't modify the array the way | 
| 123 |  |  |  |  |  |  | # CORE::splice() does. | 
| 124 |  |  |  |  |  |  | sub splice { | 
| 125 | 72 |  |  | 72 | 1 | 4049 | my ( $self, $array ) = @_; | 
| 126 | 72 | 100 |  |  |  | 438 | my $top = @$array > $self->last ? $self->last : @$array; | 
| 127 | 72 | 100 |  |  |  | 722 | return () if $top == 0;    # empty | 
| 128 | 68 |  |  |  |  | 104 | return @{$array}[ $self->first - 1 .. $top - 1 ]; | 
|  | 68 |  |  |  |  | 808 |  | 
| 129 |  |  |  |  |  |  | } | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | sub skipped { | 
| 132 | 71 |  |  | 71 | 1 | 38527 | my $self = shift; | 
| 133 |  |  |  |  |  |  |  | 
| 134 | 71 |  |  |  |  | 133 | my $skipped = $self->first - 1; | 
| 135 | 71 | 100 |  |  |  | 735 | return 0 if $skipped < 0; | 
| 136 | 68 |  |  |  |  | 306 | return $skipped; | 
| 137 |  |  |  |  |  |  | } | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | sub change_entries_per_page { | 
| 140 | 70 |  |  | 70 | 1 | 583 | my ( $self, $new_epp ) = @_; | 
| 141 |  |  |  |  |  |  |  | 
| 142 | 3 |  |  | 3 |  | 29178 | use integer; | 
|  | 3 |  |  |  |  | 37 |  | 
|  | 3 |  |  |  |  | 14 |  | 
| 143 | 70 | 50 |  |  |  | 197 | Carp::croak("Fewer than one entry per page!") if $new_epp < 1; | 
| 144 | 70 |  |  |  |  | 117 | my $new_page = 1 + ( $self->first / $new_epp ); | 
| 145 | 70 |  |  |  |  | 784 | $self->entries_per_page($new_epp); | 
| 146 | 70 |  |  |  |  | 1165 | $self->current_page($new_page); | 
| 147 |  |  |  |  |  |  | } | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | 1; | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | __END__ |