File Coverage

blib/lib/Rubric/WebApp/Entries.pm
Criterion Covered Total %
statement 70 94 74.4
branch 15 38 39.4
condition 3 6 50.0
subroutine 15 23 65.2
pod 13 13 100.0
total 116 174 66.6


line stmt bran cond sub pod time code
1 2     2   1151 use strict;
  2         4  
  2         48  
2 2     2   8 use warnings;
  2         3  
  2         81  
3             # ABSTRACT: process the /entries run method
4              
5             #pod =head1 DESCRIPTION
6             #pod
7             #pod Rubric::WebApp::Entries implements a URI parser that builds a query based
8             #pod on a query URI, passes it to Rubric::Entries, and returns the rendered report
9             #pod on the results.
10             #pod
11             #pod =cut
12              
13             use Date::Span 1.12;
14 2     2   363 use Digest::MD5 qw(md5_hex);
  2         719  
  2         254  
15 2     2   11  
  2         4  
  2         65  
16             use Rubric::Config;
17 2     2   11 use Rubric::Entry;
  2         4  
  2         29  
18 2     2   9 use Rubric::Renderer;
  2         2  
  2         107  
19 2     2   76 use Rubric::WebApp::URI;
  2         4  
  2         75  
20 2     2   8  
  2         64  
  2         1858  
21             #pod =head1 METHODS
22             #pod
23             #pod =head2 entries($webapp)
24             #pod
25             #pod This method is called by Rubric::WebApp. It returns the rendered template for
26             #pod return to the user's browser.
27             #pod
28             #pod =cut
29              
30             my ($self, $webapp) = @_;
31             my %arg;
32 30     30 1 82  
33 30         49 while (my $param = $webapp->next_path_part) {
34             my $value = $webapp->next_path_part;
35 30         95 $arg{$param} = $self->get_arg($param, $value);
36 22         497 }
37 22         292 if (my $uri = $webapp->query->param('uri')) {
38             $arg{urimd5} = md5_hex($uri) unless $arg{urimd5};
39 30 50       651 }
40 0 0       0  
41             for (qw(like desc_like body_like)) {
42             if (my $param = $webapp->query->param($_)) {
43 30         777 $arg{$_} = $self->get_arg($_, $param);
44 90 50       1091 }
45 0         0 }
46              
47             unless (%arg) {
48             $webapp->param(recent_tags => Rubric::Entry->recent_tags_counted);
49 30 100       493 $arg{first_only} = 1 unless %arg;
50 9         128 }
51 9 50       248  
52             my $user = $webapp->param('current_user');
53             my $order_by = $webapp->query->param('order_by');
54 30         80  
55 30         431 my $entries = Rubric::Entry->query(\%arg,
56             { user => $user, order_by => $order_by });
57 30         819 $webapp->param(query_description => $self->describe_query(\%arg));
58              
59 30         26963 $webapp->page_entries($entries)->render_entries(\%arg);
60             }
61 30         761  
62             #pod =head2 describe_query(\%arg)
63             #pod
64             #pod returns a human-readable description of the query described by C<%args>
65             #pod
66             #pod =cut
67              
68             my ($self, $arg) = @_;
69             my $desc;
70             $desc .= "$arg->{user}'s " if $arg->{user};
71 30     30 1 90 $desc .= "entries";
72 30         54 for (qw(body link)) {
73 30 100       99 if (defined $arg->{"has_$_"}) {
74 30         2276 $desc .= " with" . ($arg->{"has_$_"} ? "" : "out") . " a $_,";
75 30         76 }
76 60 50       500 }
77 0 0       0 if ($arg->{exact_tags}) {
78             if (%{ $arg->{exact_tags} }) {
79             $desc .= " filed under { "
80 30 50 66     169 . join(', ',
    100          
81 0 0       0 map { defined $arg->{exact_tags}{$_}
  0         0  
82             ? "$_:$arg->{exact_tags}{$_}"
83             : $_ }
84 0 0       0 keys %{$arg->{exact_tags}}) . " } exactly";
85             } else {
86             $desc .= " without tags"
87 0         0 }
  0         0  
88             } elsif ($arg->{tags} and %{ $arg->{tags} }) {
89 0         0 $desc .= " filed under { "
90             . join(', ',
91 13         58 map { defined $arg->{tags}{$_} ? "$_:$arg->{tags}{$_}" : $_ }
92             keys %{$arg->{tags}}) . " }";
93             }
94 13 50       79 $desc =~ s/,\Z//;
95 13         32 return $desc;
  13         41  
96             }
97 30         72  
98 30         134 #pod =head2 get_arg($param => $value)
99             #pod
100             #pod Given a name/value pair from the path, this method will attempt to
101             #pod generate part of hash to send to << Rubric::Entry->query >>. To do this, it
102             #pod looks for and calls a method called "arg_for_NAME" where NAME is the passed
103             #pod value of C<$param>. If no clause can be generated, it returns undef.
104             #pod
105             #pod =cut
106              
107             my ($self, $param, $value) = @_;
108              
109             return unless my $code = $self->can("arg_for_$param");
110             $code->($self, $value);
111 22     22 1 188 }
112              
113 22 50       234 #pod =head2 arg_for_NAME
114 22         109 #pod
115             #pod Each of these functions returns the proper value to put in the hash passed to
116             #pod C<< Rubric::Entries->query >>. If given an invalid argument, they will return
117             #pod undef.
118             #pod
119             #pod =head3 arg_for_user($username)
120             #pod
121             #pod Given a username, this method returns the associated Rubric::User object.
122             #pod
123             #pod =cut
124              
125             my ($self, $user) = @_;
126             return unless $user;
127             return Rubric::User->retrieve($user) || ();
128             }
129              
130 8     8 1 24 #pod =head3 arg_for_tags($tagstring)
131 8 50       24 #pod
132 8   33     99 #pod =head3 arg_for_exact_tags($tagstring)
133             #pod
134             #pod Given "happy fuzzy bunnies" this returns C< [ qw(happy fuzzy bunnies) ] >
135             #pod
136             #pod =cut
137              
138             my ($self, $tagstring) = @_;
139              
140             my $tags;
141             eval { $tags = Rubric::Entry->tags_from_string($tagstring) };
142             return $tags;
143             }
144 13     13 1 41  
145              
146 13         22 #pod =head3 arg_for_desc_like
147 13         23 #pod
  13         99  
148 13         1786 #pod =cut
149              
150             my ($self, $value) = @_;
151 0     0 1 0 return $value;
152             }
153              
154             #pod =head3 arg_for_body_like
155             #pod
156             #pod =cut
157              
158 0     0 1 0 my ($self, $value) = @_;
159 0         0 return $value;
160             }
161              
162             #pod =head3 arg_for_like
163             #pod
164             #pod =cut
165              
166             my ($self, $value) = @_;
167 0     0 1 0 return $value;
168 0         0 }
169              
170             #pod =head3 arg_for_has_body($bool)
171             #pod
172             #pod Returns the given boolean as 0 or 1.
173             #pod
174             #pod =cut
175              
176 0     0 1 0 my ($self, $bool) = @_;
177 0         0 return $bool ? 1 : 0;
178             }
179              
180             #pod =head3 arg_for_has_link($bool)
181             #pod
182             #pod Returns the given boolean as 0 or 1.
183             #pod
184             #pod =cut
185              
186             my ($self, $bool) = @_;
187 0     0 1 0 return $bool ? 1 : 0;
188 0 0       0 }
189              
190             #pod =head3 arg_for_first_only($bool)
191             #pod
192             #pod Returns the given boolean as 0 or 1.
193             #pod
194             #pod =cut
195              
196             my ($self, $bool) = @_;
197             return $bool ? 1 : 0;
198 0     0 1 0 }
199 0 0       0  
200             #pod =head3 arg_for_urimd5($md5sum)
201             #pod
202             #pod This method returns the passed value, if that value is a valid 32-character
203             #pod md5sum.
204             #pod
205             #pod =cut
206              
207             my ($self, $md5) = @_;
208             return unless $md5 =~ /\A[a-z0-9]{32}\Z/i;
209 0     0 1 0 return $md5;
210 0 0       0 }
211              
212             #pod =head3 arg_for_{timefield}_{preposition}($datetime)
213             #pod
214             #pod These methods correspond to those described in L<Rubric::Entry::Query>.
215             #pod
216             #pod They return the passed string unchanged.
217             #pod
218             #pod =cut
219              
220             ## more date-arg handling code
221 1     1 1 4 {
222 1 50       6 ## no critic (ProhibitNoStrict)
223 1         5 no strict 'refs';
224             for my $field (qw(created modified)) {
225             for my $prep (qw(after before on)) {
226             *{"arg_for_${field}_${prep}"} = sub {
227             my ($self, $datetime) = @_;
228             return $datetime;
229             }
230             }
231             }
232             }
233              
234             1;
235              
236              
237 2     2   14 =pod
  2         2  
  2         388  
238              
239             =encoding UTF-8
240              
241 0     0     =head1 NAME
242 0            
243             Rubric::WebApp::Entries - process the /entries run method
244              
245             =head1 VERSION
246              
247             version 0.157
248              
249             =head1 DESCRIPTION
250              
251             Rubric::WebApp::Entries implements a URI parser that builds a query based
252             on a query URI, passes it to Rubric::Entries, and returns the rendered report
253             on the results.
254              
255             =head1 PERL VERSION
256              
257             This code is effectively abandonware. Although releases will sometimes be made
258             to update contact info or to fix packaging flaws, bug reports will mostly be
259             ignored. Feature requests are even more likely to be ignored. (If someone
260             takes up maintenance of this code, they will presumably remove this notice.)
261             This means that whatever version of perl is currently required is unlikely to
262             change -- but also that it might change at any new maintainer's whim.
263              
264             =head1 METHODS
265              
266             =head2 entries($webapp)
267              
268             This method is called by Rubric::WebApp. It returns the rendered template for
269             return to the user's browser.
270              
271             =head2 describe_query(\%arg)
272              
273             returns a human-readable description of the query described by C<%args>
274              
275             =head2 get_arg($param => $value)
276              
277             Given a name/value pair from the path, this method will attempt to
278             generate part of hash to send to << Rubric::Entry->query >>. To do this, it
279             looks for and calls a method called "arg_for_NAME" where NAME is the passed
280             value of C<$param>. If no clause can be generated, it returns undef.
281              
282             =head2 arg_for_NAME
283              
284             Each of these functions returns the proper value to put in the hash passed to
285             C<< Rubric::Entries->query >>. If given an invalid argument, they will return
286             undef.
287              
288             =head3 arg_for_user($username)
289              
290             Given a username, this method returns the associated Rubric::User object.
291              
292             =head3 arg_for_tags($tagstring)
293              
294             =head3 arg_for_exact_tags($tagstring)
295              
296             Given "happy fuzzy bunnies" this returns C< [ qw(happy fuzzy bunnies) ] >
297              
298             =head3 arg_for_desc_like
299              
300             =head3 arg_for_body_like
301              
302             =head3 arg_for_like
303              
304             =head3 arg_for_has_body($bool)
305              
306             Returns the given boolean as 0 or 1.
307              
308             =head3 arg_for_has_link($bool)
309              
310             Returns the given boolean as 0 or 1.
311              
312             =head3 arg_for_first_only($bool)
313              
314             Returns the given boolean as 0 or 1.
315              
316             =head3 arg_for_urimd5($md5sum)
317              
318             This method returns the passed value, if that value is a valid 32-character
319             md5sum.
320              
321             =head3 arg_for_{timefield}_{preposition}($datetime)
322              
323             These methods correspond to those described in L<Rubric::Entry::Query>.
324              
325             They return the passed string unchanged.
326              
327             =head1 AUTHOR
328              
329             Ricardo SIGNES <rjbs@semiotic.systems>
330              
331             =head1 COPYRIGHT AND LICENSE
332              
333             This software is copyright (c) 2004 by Ricardo SIGNES.
334              
335             This is free software; you can redistribute it and/or modify it under
336             the same terms as the Perl 5 programming language system itself.
337              
338             =cut