File Coverage

blib/lib/WWW/Mixi/OO/ListPage.pm
Criterion Covered Total %
statement 18 57 31.5
branch 0 14 0.0
condition 0 6 0.0
subroutine 6 14 42.8
pod 8 8 100.0
total 32 99 32.3


line stmt bran cond sub pod time code
1             # -*- cperl -*-
2             # copyright (C) 2005 Topia . all rights reserved.
3             # This is free software; you can redistribute it and/or modify it
4             # under the same terms as Perl itself.
5             # $Id: ListPage.pm 106 2005-02-05 10:35:38Z topia $
6             # $URL: file:///usr/minetools/svnroot/mixi/trunk/WWW-Mixi-OO/lib/WWW/Mixi/OO/ListPage.pm $
7             package WWW::Mixi::OO::ListPage;
8 2     2   65953 use strict;
  2         15  
  2         71  
9 2     2   11 use warnings;
  2         4  
  2         62  
10 2     2   11 use Carp;
  2         5  
  2         148  
11 2     2   12 use base qw(WWW::Mixi::OO::Page);
  2         4  
  2         1188  
12              
13             =head1 NAME
14              
15             WWW::Mixi::OO::ListPage - WWW::Mixi::OO's List Pages base class
16              
17             =head1 SYNOPSIS
18              
19             package WWW::Mixi::OO::Foo;
20             use base qw(WWW::Mixi::OO::ListPage);
21             # some implementations...
22              
23             =head1 DESCRIPTION
24              
25             list pages base class.
26              
27             =head1 METHODS
28              
29             =over 4
30              
31             =cut
32              
33             =item uri
34              
35             see super class (L).
36              
37             this module handle following params:
38              
39             =over 4
40              
41             =item page
42              
43             page number, maybe 1 origin.
44              
45             =back
46              
47             =cut
48              
49             sub uri {
50 0     0 1   my $this = shift;
51 0           my $options = $this->_init_uri(@_);
52              
53 0           $this->copy_hash_val($options, $options->{_params}, 'page');
54 0           $this->SUPER::uri($options);
55             }
56              
57             =item parse_uri
58              
59             see super class (L).
60              
61             this module handle following params:
62              
63             =over 4
64              
65             =item page
66              
67             page number, maybe 1 origin.
68              
69             =back
70              
71             =cut
72              
73             sub parse_uri {
74 0     0 1   my ($this, $data, %options) = @_;
75              
76 0           $this->copy_hash_val($data->{params}, \%options, 'page');
77 0           $this->SUPER::parse_uri($data, %options);
78             }
79              
80             =item parse_navi_next
81              
82             # subclass
83             sub parse_navi_next {
84             my ($this, %options) = @_;
85             # parse...
86             $this->content =~ /.../;
87             # return
88             return { a => b, c => d };
89             }
90              
91             # call
92             my $next = $pkg->parse_navi_next;
93              
94             page "next" navi parser. please return hashref.
95             page didn't have next page navigation, no need to implement this.
96              
97             =cut
98              
99 0     0 1   sub parse_navi_next { shift->please_override_this }
100              
101             =item parse_navi_prev
102              
103             # subclass
104             sub parse_navi_prev {
105             my ($this, %options) = @_;
106             # parse...
107             $this->content =~ /.../;
108             # return
109             return { a => b, c => d };
110             }
111              
112             # call
113             my $prev = $pkg->parse_navi_prev;
114              
115             page "prev" navi parser. please return hashref.
116             page didn't have previous page navigation, no need to implement this.
117              
118             =cut
119              
120 0     0 1   sub parse_navi_prev { shift->please_override_this }
121              
122             =item fetch
123              
124             # call
125             $pkg->fetch(
126             limit => $limit,
127             other_options...
128             );
129              
130             fetch all items from some pages.
131             need ->get and ->parse_navi_next()->{link}.
132              
133             =cut
134              
135             sub fetch {
136 0     0 1   my ($this, %options) = @_;
137              
138 0           my $limit = delete $options{limit};
139              
140 0           my (@items, $next);
141 0           push @items, $this->get(%options);
142 0           while (defined ($next = $this->parse_navi_next)) {
143 0 0 0       last if defined $limit && @items > $limit;
144 0           push @items, $this->get($next->{link}, %options);
145             }
146 0 0 0       if (defined $limit && @items > $limit) {
147 0           splice @items, $limit;
148             }
149 0           return @items;
150             }
151              
152             =item gen_sort_proc
153              
154             # call
155             $pkg->gen_sort_proc($spec, [$pkg]);
156              
157             generate sort closure(anonsub).
158              
159             spec is "$field" or "!$field"(reverse order).
160              
161             =cut
162              
163             sub gen_sort_proc {
164 0     0 1   my ($this, $spec, $pkg) = @_;
165 0 0         $pkg = caller unless defined $pkg;
166              
167 0           my @order = qw($a $b);
168 0 0         @order = reverse @order if $spec =~ s/^\!//;
169              
170 0           my $op;
171 0           my $type = $this->sort_type($spec);
172 0 0         if ($type eq 'num') {
    0          
173 0           $op = '<=>';
174             } elsif ($type eq 'str') {
175 0           $op = 'cmp';
176             } else {
177 0           croak "unknown sort_type: $type";
178             }
179              
180 2     2   17 no warnings;
  2         3  
  2         100  
181 2     2   11 no strict 'refs';
  2         5  
  2         760  
182 0           return eval "package $pkg;(sub {" .
183             join(" $op ",
184 0           map { "\$$_\{$spec\}" } @order) . "})";
185             }
186              
187             =item sort
188              
189             # call
190             $pkg->sort($spec, @items...);
191              
192             handy sort function.
193             (but maybe need unnecessarily array copy...)
194              
195             =cut
196              
197             sub sort {
198 0     0 1   my ($this, $spec, @items) = @_;
199 0           my $sort_proc = $this->gen_sort_proc($spec);
200 0           sort $sort_proc @items;
201             }
202              
203             =item sort_type
204              
205             # subclass
206             sub sort_type {
207             my ($this, $field) = @_;
208              
209             if (grep { $_ eq $field } qw(more nums...)) {
210             return 'num';
211             } else {
212             return $this->SUPER::sort_type($field);
213             }
214             }
215              
216             sort type probe function.
217              
218             =cut
219              
220             sub sort_type {
221 0     0 1   my ($this, $field) = @_;
222              
223 0 0         if (grep { $_ eq $field } qw(id count)) {
  0            
224 0           return 'num';
225             } else {
226 0           return 'str';
227             }
228             }
229              
230             =item get_navi_next
231              
232             # call
233             $pkg->get_navi_next([opt => val], ...);
234              
235             handy method. call ->set_content and ->parse_navi_next.
236              
237             =item get_navi_prev
238              
239             # call
240             $pkg->get_navi_prev([opt => val], ...);
241              
242             handy method. call ->set_content and ->parse_navi_prev.
243              
244             =cut
245              
246             __PACKAGE__->mk_get_method(qw(navi_next navi_prev));
247              
248             1;
249              
250             __END__