Documentation.pm

filter: Statementstmt 100% 111 / 111 Branchbran 94.4% 34 / 36 Conditioncond 82.1% 23 / 28 MC/DCmcdc 56.2% 9 / 16 Subroutinesub 100% 23 / 23 Podpod 100% 6 / 6 totaltotal 93.6% 206 / 220 CC 35 SCAR 15.2
CC
35
Cov
96%
get_route_documentation
9.0
__ANON__
6.5
__ANON__
6.0
CRAP 4.6
↑ Summary Documentation/Route.pm »
1 package Dancer::Plugin::Documentation;
2  
3 =head1 NAME
4  
5 Dancer::Plugin::Documentation - register documentation for routes
6  
7 =cut
8  
9 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use Carp qw{croak};
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Scalar::Util (qw{blessed});
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Set::Functional (qw{setify_by});
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 ▶use Dancer::App;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Dancer::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Dancer::Plugin::Documentation::Route;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use Dancer::Plugin::Documentation::Section;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23  
24 =head1 VERSION
25  
26 Version 0.02
27  
28 =cut
29  
30 3 our $VERSION = '0.02';
31  
32 =head1 SYNOPSIS
33  
34 Dancer::Plugin::Documentation provides a few keywords to associate documentation
35 with a fully pathed route. This is especially useful when the route path is
36 externally modified by the prefix command. Documentation my be anything from a
37 string to a complex data structure.
38  
39 Example usage:
40  
41 package Foo;
42  
43 use Dancer;
44 use Dancer::Plugin::Documentation qw{:all};
45  
46 document_section 'Helpers';
47  
48 document_route 'Display documentation'
49 get '/resources' => sub {
50 status 200;
51 return join "\n\n",
52 map {
53 $_->isa('Dancer::Plugin::Documentation::Section') ? ($_->section, $_->documentation || ()) :
54 $_->isa('Dancer::Plugin::Documentation::Route') ? ($_->method . ' ' . $_->path, $_->documentation) :
55 $_->documentation
56 } documentation;
57 };
58  
59 prefix '/v1';
60  
61 document_section 'Foo', 'Manage your foo';
62  
63 document_route 'A route to retrieve foo',
64 get '/foo' => sub { status 200; return 'foo' };
65  
66 package main;
67  
68 dance;
69  
70 =cut
71  
72 3 my %APP_TO_ACTIVE_SECTION;
73 my %APP_TO_ROUTE_DOCUMENTATION;
74 3 my %APP_TO_SECTION_DOCUMENTATION;
75  
76 =head1 KEYWORDS
77  
78 =cut
79  
80 =head2 document_route
81  
82 Given a documentation argument and a list of routes, associate the
83 documentation with all of the routes.
84  
85 =cut
86  
87 7 ▶register document_route => sub {
Subroutine __ANON__: called CC=6 SCAR=18.7
88 7 my ($documentation, @routes) = @_;
89  
90 7 my $app = Dancer::App->current->name;
91  
92 7 ▶ croak "Documentation missing, Dancer::Route found instead"
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($documentation) and $documentation->isa("Dancer::Route") 0 7 7
Condition33%1/3
builtin::blessed($documentation) and $documentation->isa("Dancer::Route")
!ll&&!rl&&r
700
MC/DC0%0/2
builtin::blessed($documentation) and $documentation->isa("Dancer::Route")
builtin::blessed($documentation) $documentation->isa("Dancer::Route")
Truth table33%1/3
A and B
A= builtin::blessed($documentation)B= $documentation->isa("Dancer::Route")
ABresult
0X0
100
111
93 if blessed $documentation && $documentation->isa('Dancer::Route');
94  
95 croak "Invalid argument where Dancer::Route expected"
96 7 ▶ if grep { ! blessed $_ || ! $_->isa('Dancer::Route') } @routes;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not builtin::blessed($_) && $_->isa("Dancer::Route");} @routes 1 6 7
Condition66%2/3
builtin::blessed($_) && $_->isa('Dancer::Route')
!ll&&!rl&&r
1010
MC/DC50%1/2
builtin::blessed($_) && $_->isa('Dancer::Route')
builtin::blessed($_) $_->isa('Dancer::Route')
Truth table66%2/3
A && B
A= builtin::blessed($_)B= $_->isa('Dancer::Route')
ABresult
0X0
100
111
97  
98 Dancer::Plugin::Documentation->set_route_documentation(
99 app => $app,
100 path => $_->pattern,
101 method => $_->method,
102 documentation => $documentation,
103 section => Dancer::Plugin::Documentation->get_active_section(app => $app),
104 6 ) for @routes;
105  
106 6 return @routes;
107 3 };
108  
109 =head2 document_section
110  
111 Given a label, set the section grouping for all subsequent document_route calls.
112 Optionally, supply documentation to associate with the section. Disable the
113 current section by passing undef or the empty string for the label.
114  
115 =cut
116  
117 4 ▶register document_section => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
118 4 my ($section, $documentation) = @_;
119  
120 4 my $app = Dancer::App->current->name;
121 4 ▶ $section = '' unless defined $section;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $section 0 4 4
122  
123 4 ▶ Dancer::Plugin::Documentation->set_section_documentation(
Branch: 1 branch
BranchTrueFalseTotal
unless $section eq "" 3 1 4
124 app => $app,
125 section => $section,
126 documentation => $documentation,
127 ) unless $section eq '';
128 4 Dancer::Plugin::Documentation->set_active_section(
129 app => $app,
130 section => $section,
131 );
132  
133 4 return;
134 3 };
135  
136 =head2 documentation
137  
138 Retrieve all documentation for the current app with sections interweaved
139 with routes. Supports all arguments for documentation_for_routes and
140 documentation_for_sections.
141  
142 =cut
143  
144 4 ▶register documentation => sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
145 4 my %args = @_;
146 4 my @route_documentation = documentation_for_routes(%args);
147 my @section_documentation =
148 ! keys %args || exists $args{section}
149 4 ▶ ? documentation_for_sections(%args)
Branch: 1 branch
BranchTrueFalseTotal
!keys(%args) || exists $args{'section'} ? : 2 2 4
Condition100%3/3
not keys %args or exists $args{"section"}
l!l&&r!l&&!r
112
MC/DC100%2/2
not keys %args or exists $args{"section"}
not keys %args exists $args{"section"}
Truth table100%3/3
not(A) or B
A= not keys %argsB= exists $args{"section"}
ABresult
1X1
011
000
150 : ();
151  
152 4 my @documentation;
153 4 ▶ while (@section_documentation && @route_documentation) {
Branch: 1 branch
BranchTrueFalseTotal
if (@section_documentation and @route_documentation) 7 4 11
Condition66%2/3
@section_documentation and @route_documentation
!ll&&!rl&&r
407
MC/DC50%1/2
@section_documentation and @route_documentation
@section_documentation @route_documentation
Truth table66%2/3
A and B
A= @section_documentationB= @route_documentation
ABresult
0X0
100
111
154 7 ▶ push @documentation, $section_documentation[0]->section le $route_documentation[0]->section
Branch: 1 branch
BranchTrueFalseTotal
$section_documentation[0]->section le $route_documentation[0]->section ? : 4 3 7
155 ? shift @section_documentation
156 : shift @route_documentation
157 ;
158 }
159 4 push @documentation, @section_documentation;
160 4 push @documentation, @route_documentation;
161  
162 4 return @documentation;
163 3 };
164  
165 =head2 documentation_for_routes
166  
167 Retrieve all route documentation for the current app. Supports all the same
168 arguments as get_route_documentation besides app.
169  
170 =cut
171  
172 4 ▶register documentation_for_routes => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
173 4 return Dancer::Plugin::Documentation->get_route_documentation(
174 @_,
175 app => Dancer::App->current->name,
176 );
177 3 };
178  
179 =head2 documentation_for_sections
180  
181 Retrieve all section documentation for the current app. Supports all the same
182 arguments as get_section_documentation besides app.
183  
184 =cut
185  
186 2 ▶register documentation_for_sections => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
187 2 my %args = @_;
188  
189 2 return Dancer::Plugin::Documentation->get_section_documentation(
190 @_,
191 app => Dancer::App->current->name,
192 );
193 3 };
194  
195 =head1 DOCUMENTATION METHODS
196  
197 =head2 get_route_documentation
198  
199 Retrieve the route documentation for an app in lexicographical order by
200 section, route, then method. Any/all of the following may be supplied to
201 filter the documentation: method, path, section
202  
203 =cut
204  
205 17 ▶sub get_route_documentation {
Subroutine get_route_documentation: called CC=9 SCAR=22.0
Pod documented
206 17 my ($class, %args) = @_;
207  
208 defined $args{$_} || croak "Argument [$_] is required"
209 17 ▶ for qw{ app };
Branch: 1 branch
BranchTrueFalseTotal
defined $args{$_} or croak("Argument [$_] is required") 15 2 17
210  
211 15 my ($app, $method, $path, $section) = @args{qw{app method path section}};
212 15 ▶ $method = lc $method if $method;
Branch: 1 branch
BranchTrueFalseTotal
if $method 6 9 15
213 15 ▶ $section = lc $section if $section;
Branch: 1 branch
BranchTrueFalseTotal
if $section 4 11 15
214  
215 15 ▶ my @docs = @{$APP_TO_ROUTE_DOCUMENTATION{$app} || []};
Condition100%2/2
$APP_TO_ROUTE_DOCUMENTATION{$app} || []
l!l
132
MC/DC100%1/1
$APP_TO_ROUTE_DOCUMENTATION{$app} || []
$APP_TO_ROUTE_DOCUMENTATION{$app}
Truth table100%2/2
A
A= $APP_TO_ROUTE_DOCUMENTATION{$app}
Aresult
11
00
216  
217 15 ▶ @docs = grep { $_->section eq $section } @docs if defined $section;
Branch: 1 branch
BranchTrueFalseTotal
if defined $section 4 11 15
218 15 ▶ @docs = grep { $_->path eq $path } @docs if defined $path;
Branch: 1 branch
BranchTrueFalseTotal
if defined $path 5 10 15
219 15 ▶ @docs = grep { $_->method eq $method } @docs if defined $method;
Branch: 1 branch
BranchTrueFalseTotal
if defined $method 6 9 15
220  
221 15 return @docs;
222 }
223  
224 =head2 get_section_documentation
225  
226 Retrieve the section documentation for an app in lexicographical order.
227 Any/all of the following may be supplied to filter the documentation: section
228  
229 =cut
230  
231 11 ▶sub get_section_documentation {
Subroutine get_section_documentation: called CC=6 SCAR=17.9
Pod documented
232 11 my ($class, %args) = @_;
233  
234 defined $args{$_} || croak "Argument [$_] is required"
235 11 ▶ for qw{ app };
Branch: 1 branch
BranchTrueFalseTotal
defined $args{$_} or croak("Argument [$_] is required") 9 2 11
236  
237 9 my ($app, $section) = @args{qw{app section}};
238 9 ▶ $section = lc $section if $section;
Branch: 1 branch
BranchTrueFalseTotal
if $section 5 4 9
239  
240 9 ▶ my @docs = @{$APP_TO_SECTION_DOCUMENTATION{$app} || []};
Condition100%2/2
$APP_TO_SECTION_DOCUMENTATION{$app} || []
l!l
72
MC/DC100%1/1
$APP_TO_SECTION_DOCUMENTATION{$app} || []
$APP_TO_SECTION_DOCUMENTATION{$app}
Truth table100%2/2
A
A= $APP_TO_SECTION_DOCUMENTATION{$app}
Aresult
11
00
241  
242 9 ▶ @docs = grep { $_->section eq $section } @docs if defined $section;
Branch: 1 branch
BranchTrueFalseTotal
if defined $section 5 4 9
243  
244 9 return @docs;
245 }
246  
247 =head2 set_route_documentation
248  
249 Register documentation for the method and route of a particular app.
250  
251 =cut
252  
253 28 ▶sub set_route_documentation {
Subroutine set_route_documentation: called CC=4 SCAR=13.9
Pod documented
254 28 my $class = shift;
255  
256 28 my $route_documentation = Dancer::Plugin::Documentation::Route->new(@_);
257  
258 #We take the hit to keep all documentation in a sorted unique list on insertion
259 #so that any retrieval requests are highly optimized.
260 $APP_TO_ROUTE_DOCUMENTATION{$route_documentation->app} = [
261 123 ▶ sort { 0
Condition83%5/6
$a->section cmp $b->section || $a->path cmp $b->path || $a->method cmp $b->method
l!l&&r!l&&!r
87360
$a->section cmp $b->section or $a->path cmp $b->path
l!l&&r!l&&!r
592836
MC/DC0%0/3
$a->section cmp $b->section || $a->path cmp $b->path || $a->method cmp $b->method
$a->section cmp $b->section $a->path cmp $b->path $a->method cmp $b->method
Truth table75%3/4
A or B || C
A= $a->section cmp $b->sectionB= $a->path cmp $b->pathC= $a->method cmp $b->method
ABCresult
1XX1
01X1
0011
0000
262 || $a->section cmp $b->section
263 || $a->path cmp $b->path
264 || $a->method cmp $b->method
265 }
266 88 ▶ setify_by { $_->method . ':' . $_->path }
Subroutine __ANON__: called CC=1 SCAR=0.0
267 (
268 22 ▶ @{$APP_TO_ROUTE_DOCUMENTATION{$route_documentation->app} || []},
Condition100%2/2
$APP_TO_ROUTE_DOCUMENTATION{$route_documentation->app} || []
l!l
175
MC/DC100%1/1
$APP_TO_ROUTE_DOCUMENTATION{$route_documentation->app} || []
$APP_TO_ROUTE_DOCUMENTATION{$route_documentation->app}
Truth table100%2/2
A
A= $APP_TO_ROUTE_DOCUMENTATION{$route_documentation->app}
Aresult
11
00
269 $route_documentation,
270 )
271 ];
272  
273 22 return $class;
274 }
275  
276 =head2 set_section_documentation
277  
278 Register documentation for the section of a particular app.
279  
280 =cut
281  
282 13 ▶sub set_section_documentation {
Subroutine set_section_documentation: called CC=2 SCAR=6.9
Pod documented
283 13 my $class = shift;
284  
285 13 my $section_documentation = Dancer::Plugin::Documentation::Section->new(@_);
286  
287 #We take the hit to keep all documentation in a sorted unique list on insertion
288 #so that any retrieval requests are highly optimized.
289 $APP_TO_SECTION_DOCUMENTATION{$section_documentation->app} = [
290 8 sort { $a->section cmp $b->section }
291 16 ▶ setify_by { $_->section }
Subroutine __ANON__: called CC=1 SCAR=0.0
292 (
293 9 ▶ @{$APP_TO_SECTION_DOCUMENTATION{$section_documentation->app} || []},
Condition100%2/2
$APP_TO_SECTION_DOCUMENTATION{$section_documentation->app} || []
l!l
54
MC/DC100%1/1
$APP_TO_SECTION_DOCUMENTATION{$section_documentation->app} || []
$APP_TO_SECTION_DOCUMENTATION{$section_documentation->app}
Truth table100%2/2
A
A= $APP_TO_SECTION_DOCUMENTATION{$section_documentation->app}
Aresult
11
00
294 $section_documentation,
295 )
296 ];
297  
298 9 return $class;
299 }
300  
301 =head1 APPLICATION STATE METHODS
302  
303 =cut
304  
305 =head2 get_active_section
306  
307 Get the name of the active section for the application.
308  
309 =cut
310  
311 15 ▶sub get_active_section {
Subroutine get_active_section: called CC=4 SCAR=13.9
Pod documented
312 15 my ($class, %args) = @_;
313  
314 defined $args{$_} || croak "Argument [$_] is required"
315 15 ▶ for qw{ app };
Branch: 1 branch
BranchTrueFalseTotal
defined $args{$_} or croak("Argument [$_] is required") 13 2 15
316  
317 13 my ($app) = @args{qw{ app }};
318  
319 13 ▶ return $APP_TO_ACTIVE_SECTION{$app} || '';
Condition100%2/2
$APP_TO_ACTIVE_SECTION{$app} || ''
l!l
85
MC/DC100%1/1
$APP_TO_ACTIVE_SECTION{$app} || ''
$APP_TO_ACTIVE_SECTION{$app}
Truth table100%2/2
A
A= $APP_TO_ACTIVE_SECTION{$app}
Aresult
11
00
320 }
321  
322 =head2 set_active_section
323  
324 Set the name of the active section for the application.
325  
326 =cut
327  
328 11 ▶sub set_active_section {
Subroutine set_active_section: called CC=3 SCAR=11.0
Pod documented
329 11 my ($class, %args) = @_;
330  
331 defined $args{$_} || croak "Argument [$_] is required"
332 11 ▶ for qw{ app section };
Branch: 1 branch
BranchTrueFalseTotal
defined $args{$_} or croak("Argument [$_] is required") 16 4 20
333  
334 7 my ($app, $section) = @args{qw{ app section }};
335 7 $APP_TO_ACTIVE_SECTION{$app} = $section;
336  
337 7 return $class;
338 }
339  
340 =head1 CAVEATS
341  
342 =over 4
343  
344 =item any
345  
346 The documentation keyword does not work with the I<any> keyword as it does not
347 return the list of registered routes, but rather the number of routes
348 registered. Fixing this behavior will require a patch to Dancer.
349  
350 =item get
351  
352 The I<get> keyword generates both get and head routes. Documentation will be
353 attached to both.
354  
355 =back
356  
357 =head1 AUTHOR
358  
359 Aaron Cohen, C<< <aarondcohen at gmail.com> >>
360  
361 =head1 ACKNOWLEDGEMENTS
362  
363 This module was made possible by L<Shutterstock|http://www.shutterstock.com/>
364 (L<@ShutterTech|https://twitter.com/ShutterTech>). Additional open source
365 projects from Shutterstock can be found at
366 L<code.shutterstock.com|http://code.shutterstock.com/>.
367  
368 =head1 BUGS
369  
370 Please report any bugs or feature requests to C<bug-Dancer-Plugin-Documentation at rt.cpan.org>, or through
371 the web interface at L<https://github.com/aarondcohen/perl-dancer-plugin-documentation/issues>. I will
372 be notified, and then you'll automatically be notified of progress on your bug as I make changes.
373  
374 =head1 SUPPORT
375  
376 You can find documentation for this module with the perldoc command.
377  
378 perldoc Dancer::Plugin::Documentation
379  
380 You can also look for information at:
381  
382 =over 4
383  
384 =item * Official GitHub Repo
385  
386 L<https://github.com/aarondcohen/perl-dancer-plugin-documentation>
387  
388 =item * GitHub's Issue Tracker (report bugs here)
389  
390 L<https://github.com/aarondcohen/perl-dancer-plugin-documentation/issues>
391  
392 =item * CPAN Ratings
393  
394 L<http://cpanratings.perl.org/d/Dancer-Plugin-Documentation>
395  
396 =item * Official CPAN Page
397  
398 L<http://search.cpan.org/dist/Dancer-Plugin-Documentation/>
399  
400 =back
401  
402 =head1 LICENSE AND COPYRIGHT
403  
404 Copyright 2014 Aaron Cohen.
405  
406 This program is free software; you can redistribute it and/or modify it
407 under the terms of either: the GNU General Public License as published
408 by the Free Software Foundation; or the Artistic License.
409  
410 See http://dev.perl.org/licenses/ for more information.
411  
412 =cut
413  
414 3 register_plugin;
415 3 1; # End of Dancer::Plugin::Documentation
↑ Summary Documentation/Route.pm »