blib/lib/Data/Pageset/Variable.pm

filter: Statementstmt 89.2% 50 / 56 Branchbran 58.3% 14 / 24 Conditioncond 52.1% 12 / 23 MC/DCmcdc 26.6% 4 / 15 Subroutinesub 100% 11 / 11 Podpod 100% 5 / 5 totaltotal 71.6% 96 / 134 CC 23 SCAR 21.4
CC
23
Cov
74%
new
13.9
first
11.6
last_page
4.5
CRAP 8.5
↑ Summary
1 package Data::Pageset::Variable;
2 1 $Data::Pageset::Variable::VERSION = '0.03';
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use base 'Data::Pageset';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 9 ▶sub new {
Subroutine new: called CC=13 SCAR=26.4
Pod documented
78 9 my ($proto,$conf) = @_;
79 9 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
090
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
80 9 my $self = {};
81
82 croak "total_entries and entries_per_page must be supplied"
83 9 ▶ unless defined $conf->{'total_entries'} && defined $conf->{'entries_per_page'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conf->{"total_entries"} and defined $conf->{"entries_per_page"} 1 8 9
Condition66%2/3
defined $conf->{"total_entries"} and defined $conf->{"entries_per_page"}
!ll&&!rl&&r
018
MC/DC50%1/2
defined $conf->{"total_entries"} and defined $conf->{"entries_per_page"}
defined $conf->{"total_entries"} defined $conf->{"entries_per_page"}
Truth table66%2/3
A and B
A= defined $conf->{"total_entries"}B= defined $conf->{"entries_per_page"}
ABresult
0X0
100
111
84  
85 8 ▶ $conf->{'current_page'} = 1 unless defined $conf->{'current_page'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conf->{"current_page"} 2 6 8
86  
87 8 ▶ if (exists $conf->{'variable_entries_per_page'} && ref $conf->{'variable_entries_per_page'} ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $conf->{"variable_entries_per_page"} and ref $conf->{"variable_entries_per_page"} ne "HASH") 1 7 8
Condition100%3/3
exists $conf->{"variable_entries_per_page"} and ref $conf->{"variable_entries_per_page"} ne "HASH"
!ll&&!rl&&r
161
MC/DC100%2/2
exists $conf->{"variable_entries_per_page"} and ref $conf->{"variable_entries_per_page"} ne "HASH"
exists $conf->{"variable_entries_per_page"} ref $conf->{"variable_entries_per_page"} ne "HASH"
Truth table100%3/3
A and B
A= exists $conf->{"variable_entries_per_page"}B= ref $conf->{"variable_entries_per_page"} ne "HASH"
ABresult
0X0
100
111
88 1 croak "variable_entries_per_page must be a hashref";
89 }
90  
91 7 ▶ $self->{vari_pages} = $conf->{'variable_entries_per_page'} || {};
Condition100%2/2
$conf->{'variable_entries_per_page'} || {}
l!l
61
MC/DC100%1/1
$conf->{'variable_entries_per_page'} || {}
$conf->{'variable_entries_per_page'}
Truth table100%2/2
A
A= $conf->{'variable_entries_per_page'}
Aresult
11
00
92  
93 7 $self->{TOTAL_ENTRIES} = $conf->{'total_entries'};
94 7 $self->{ENTRIES_PER_PAGE} = $conf->{'entries_per_page'};
95 7 $self->{CURRENT_PAGE} = $conf->{'current_page'};
96  
97 7 bless($self, $class);
98
99 7 ▶ croak("Fewer than one entry per page!") if $self->entries_per_page < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->entries_per_page < 1 0 7 7
100 7 ▶ $self->{CURRENT_PAGE} = $self->first_page unless defined $self->current_page;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->current_page 0 7 7
101 7 ▶ $self->{CURRENT_PAGE} = $self->first_page if $self->current_page < $self->first_page;
Branch: 1 branch
BranchTrueFalseTotal
if $self->current_page < $self->first_page 0 7 7
102 7 ▶ $self->{CURRENT_PAGE} = $self->last_page if $self->current_page > $self->last_page;
Branch: 1 branch
BranchTrueFalseTotal
if $self->current_page > $self->last_page 7 0 7
103  
104 7 ▶ $self->pages_per_set($conf->{'pages_per_set'}) if defined $conf->{'pages_per_set'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $conf->{"pages_per_set"} 0 7 7
105  
106 7 return $self;
107 }
108  
109 13 ▶sub _vari_pages { shift->{vari_pages} }
Subroutine _vari_pages: called CC=1 SCAR=0.0
110 2 ▶sub _default_epp { shift->{ENTRIES_PER_PAGE} }
Subroutine _default_epp: called CC=1 SCAR=0.0
111  
112 7 ▶sub entries_per_page {
Subroutine entries_per_page: called CC=2 SCAR=7.1
Pod documented
113 7 my $self = shift;
114 7 ▶ return $self->_vari_pages->{$self->current_page} || $self->_default_epp;
Condition66%2/3
$self->_vari_pages->{$self->current_page} || $self->_default_epp
l!l&&r!l&&!r
610
MC/DC0%0/2
$self->_vari_pages->{$self->current_page} || $self->_default_epp
$self->_vari_pages->{$self->current_page} $self->_default_epp
Truth table66%2/3
A || B
A= $self->_vari_pages->{$self->current_page}B= $self->_default_epp
ABresult
1X1
011
000
115 }
116  
117 6 ▶sub first {
Subroutine first: called CC=5 SCAR=24.5
Pod documented
118 6 my $self = shift;
119 6 my $sum = 0;
120 6 ▶ unless ($self->current_page == 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->current_page == 1) 0 6 6
121 0 ▶ my $last = $self->current_page > $self->last_page ? $self->last_page : $self->current_page;
Branch: 1 branch
BranchTrueFalseTotal
$self->current_page > $self->last_page ? : 0 0 0
122 0 for (1 .. $last - 1) {
123 0 ▶ $sum += $self->_vari_pages->{$_} || $self->_default_epp;
Condition0%0/3
$self->_vari_pages->{$_} || $self->_default_epp
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_vari_pages->{$_} || $self->_default_epp
$self->_vari_pages->{$_} $self->_default_epp
Truth table0%0/3
A || B
A= $self->_vari_pages->{$_}B= $self->_default_epp
ABresult
1X1
011
000
124 }
125 }
126 6 return $sum + 1;
127 }
128  
129 6 ▶sub last {
Subroutine last: called CC=4 SCAR=14.5
Pod documented
130 6 my $self = shift;
131 6 my $sum = 0;
132 6 ▶ if ($self->current_page == $self->last_page) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->current_page == $self->last_page) { } 0 6 6
133 0 return $self->total_entries;
134 } else {
135 6 for (1 .. $self->current_page) {
136 6 ▶ $sum += $self->_vari_pages->{$_} || $self->_default_epp;
Condition66%2/3
$self->_vari_pages->{$_} || $self->_default_epp
l!l&&r!l&&!r
510
MC/DC0%0/2
$self->_vari_pages->{$_} || $self->_default_epp
$self->_vari_pages->{$_} $self->_default_epp
Truth table66%2/3
A || B
A= $self->_vari_pages->{$_}B= $self->_default_epp
ABresult
1X1
011
000
137 }
138 6 return $sum;
139 }
140 }
141  
142 20 ▶sub last_page {
Subroutine last_page: called CC=3 SCAR=15.0
Pod documented
143 20 my $self = shift;
144 20 my ($count, $page) = (0, 0);
145 20 ▶ while ($count < $self->total_entries) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < $self->total_entries) 0 20 20
146 0 $page++;
147 0 ▶ $count += $self->_vari_pages->{$page} || $self->_default_epp;
Condition0%0/3
$self->_vari_pages->{$page} || $self->_default_epp
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_vari_pages->{$page} || $self->_default_epp
$self->_vari_pages->{$page} $self->_default_epp
Truth table0%0/3
A || B
A= $self->_vari_pages->{$page}B= $self->_default_epp
ABresult
1X1
011
000
148 }
149 20 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, E<lt>coder@stray-toaster.co.ukE<gt>
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 1 return qw/I coded this to Queen Adreena and Black Rebel Motorcycle Club. Bizarre/;
↑ Summary