Lazy cursor instantiation for resultsets
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Pager.pm
1 package DBIx::Class::Pager;
2
3 use strict;
4 use warnings;
5
6 use NEXT;
7 use Data::Page;
8
9 =head1 NAME 
10
11 DBIx::Class::Pager -  Pagination of resultsets
12
13 =head1 SYNOPSIS
14
15 =head1 DESCRIPTION
16
17 This class lets you page through a resultset.
18
19 =head1 METHODS
20
21 =over 4
22
23 =item page
24
25 =item pager
26
27 =cut
28
29 *pager = \&page;
30
31 sub page {
32   my $self = shift;
33   my ($criteria, $attr) = @_;
34   
35   my $rows    = $attr->{rows} || 10;
36   my $current = $attr->{page} || 1;
37   
38   # count must not use LIMIT, so strip out rows/offset
39   delete $attr->{$_} for qw/rows offset/;
40   
41   my $total = $self->count( $criteria, $attr );
42   my $page = Data::Page->new( $total, $rows, $current );
43   
44   $attr->{rows}   = $page->entries_per_page;
45   $attr->{offset} = $page->skipped;
46
47   my $iterator = $self->search( $criteria, $attr );
48
49   return ( $page, $iterator );
50 }
51
52 1;
53
54 =back
55
56 =head1 AUTHORS
57
58 Andy Grundman <andy@hybridized.org>
59
60 =head1 LICENSE
61
62 You may distribute this code under the same terms as Perl itself.
63
64 =cut
65