File Coverage

blib/lib/Data/Pageset/Variable.pm
Criterion Covered Total %
statement 48 54 88.8
branch 13 22 59.0
condition 12 23 52.1
subroutine 11 11 100.0
pod 5 5 100.0
total 89 115 77.3


line stmt bran cond sub pod time code
1             package Data::Pageset::Variable;
2             $Data::Pageset::Variable::VERSION = '0.03';
3              
4 1     1   513 use strict;
  1         1  
  1         25  
5 1     1   3 use warnings;
  1         2  
  1         21  
6              
7 1     1   4 use base 'Data::Pageset';
  1         4  
  1         729  
8              
9 1     1   9356 use Carp;
  1         2  
  1         636  
10              
11             =head1 NAME
12              
13             Data::Pageset::Variable - Variable results on each page of results.
14              
15             =head1 SYNOPSIS
16              
17             use Data::Pageset::Variable;
18             # As Data::Pageset except...
19              
20             my $page_info = Data::Pageset->new(
21             {
22             total_entries => $total_entries,
23             variable_entries_per_page => { 1 => 30, 2 => 20, 3 => 10, }
24             entries_per_page => 10,
25             }
26             );
27              
28             =head1 DESCRIPTION
29              
30             Data::Pageset is A Great Module, and totally useful. This is a subclass that
31             extends its behaviour.
32              
33             Data::Pageset returns an object with a set number of pages per set. The point
34             of Data::Pageset::Variable is that you might not want this to be so. You might,
35             for reasons best known to yourself, want to have twice the number of results on
36             the first page as on the second, and so on.
37              
38             So now you can!
39              
40             =head1 HAIKU
41              
42             Different numbers
43             Of results on each page helps
44             Tabulate results
45              
46             This arose as Tony (http://www.tmtm.com/nothing/) suggested to me that if I can't
47             write the documentation of a module in haiku, then it is doing too many things.
48             As I (also) believe that modules should be responsible for one concept, and one only.
49              
50             Also, I have no poetical ability, so forgive my clumsy attempt.
51              
52             =head1 METHODS
53              
54             =head2 variable_entries_per_page
55              
56             # In the constructor hashref...
57             variable_entries_per_page => { 1 => 30, 2 => 20, 3 => 10, },
58              
59             The variable_entries_per_page argument takes a hashref.
60              
61             The key/value pairs of this hashref are the pages and the number of entries
62             on the page. If there is a page for which none is specified, then we use the
63             value of default_entries_per_page.
64              
65             If this isn't set, then we behave exactly like Data::Pageset.
66              
67             =head2 entries_per_page
68              
69             # In the constructor hashref...
70             entries_per_page => 10,
71              
72             This must be set. It is not optional. This is the number of entries per page
73             for all pages which aren't specified in the entries_per_page hashref.
74            
75             =cut
76              
77             sub new {
78 9     9 1 4650 my ($proto,$conf) = @_;
79 9   33     47 my $class = ref($proto) || $proto;
80 9         13 my $self = {};
81            
82 9 100 66     274 croak "total_entries and entries_per_page must be supplied"
83             unless defined $conf->{'total_entries'} && defined $conf->{'entries_per_page'};
84              
85 8 100       21 $conf->{'current_page'} = 1 unless defined $conf->{'current_page'};
86              
87 8 100 100     40 if (exists $conf->{'variable_entries_per_page'} && ref $conf->{'variable_entries_per_page'} ne 'HASH') {
88 1         328 croak "variable_entries_per_page must be a hashref";
89             }
90              
91 7   100     30 $self->{vari_pages} = $conf->{'variable_entries_per_page'} || {};
92              
93 7         15 $self->{TOTAL_ENTRIES} = $conf->{'total_entries'};
94 7         13 $self->{ENTRIES_PER_PAGE} = $conf->{'entries_per_page'};
95 7         14 $self->{CURRENT_PAGE} = $conf->{'current_page'};
96              
97 7         19 bless($self, $class);
98            
99 7 50       18 croak("Fewer than one entry per page!") if $self->entries_per_page < 1;
100 7 50       232 $self->{CURRENT_PAGE} = $self->first_page unless defined $self->current_page;
101 7 50       165 $self->{CURRENT_PAGE} = $self->first_page if $self->current_page < $self->first_page;
102 7 50       174 $self->{CURRENT_PAGE} = $self->last_page if $self->current_page > $self->last_page;
103              
104 7 50       22 $self->pages_per_set($conf->{'pages_per_set'}) if defined $conf->{'pages_per_set'};
105              
106 7         22 return $self;
107             }
108              
109 13     13   79 sub _vari_pages { shift->{vari_pages} }
110 2     2   38 sub _default_epp { shift->{ENTRIES_PER_PAGE} }
111              
112             sub entries_per_page {
113 7     7 1 9 my $self = shift;
114 7   66     14 return $self->_vari_pages->{$self->current_page} || $self->_default_epp;
115             }
116              
117             sub first {
118 6     6 1 22 my $self = shift;
119 6         8 my $sum = 0;
120 6 50       18 unless ($self->current_page == 1) {
121 0 0       0 my $last = $self->current_page > $self->last_page ? $self->last_page : $self->current_page;
122 0         0 for (1 .. $last - 1) {
123 0   0     0 $sum += $self->_vari_pages->{$_} || $self->_default_epp;
124             }
125             }
126 6         708 return $sum + 1;
127             }
128              
129             sub last {
130 6     6 1 11 my $self = shift;
131 6         9 my $sum = 0;
132 6 50       19 if ($self->current_page == $self->last_page) {
133 0         0 return $self->total_entries;
134             } else {
135 6         18 for (1 .. $self->current_page) {
136 6   66     135 $sum += $self->_vari_pages->{$_} || $self->_default_epp;
137             }
138 6         28 return $sum;
139             }
140             }
141              
142             sub last_page {
143 20     20 1 294 my $self = shift;
144 20         33 my ($count, $page) = (0, 0);
145 20         51 while ($count < $self->total_entries) {
146 0         0 $page++;
147 0   0     0 $count += $self->_vari_pages->{$page} || $self->_default_epp;
148             }
149 20         395 return $page;
150             }
151              
152             =head1 SHOWING YOU APPRECIATION
153              
154             There was a thread on london.pm mailing list about working in a vacumn - that
155             it was a bit depressing to keep writing modules but never get any feedback. So,
156             if you use and like this module then please send me an email and make my day.
157              
158             All it takes is a few little bytes.
159              
160             (Leon wrote that, not me!)
161              
162             =head1 AUTHOR
163              
164             Stray Toaster, Ecoder@stray-toaster.co.ukE
165              
166             =head2 With Thanks
167              
168             Leo for Data::Pageset. It rocks.
169             (And also for a code suggestion, and taking the time to even look at this!)
170              
171             =head1 COPYRIGHT AND LICENSE
172              
173             Copyright 2003 by Stray Toaster
174              
175             This library is free software; you can redistribute it and/or modify
176             it under the same terms as Perl itself.
177              
178             =cut
179              
180             return qw/I coded this to Queen Adreena and Black Rebel Motorcycle Club. Bizarre/;