blib/lib/CatalystX/Routes.pm

filter: Statementstmt 98.8% 82 / 83 Branchbran 88.8% 16 / 18 Conditioncond 30.0% 6 / 20 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 96.3% 26 / 27 Podpod 100% 11 / 11 totaltotal 83.9% 141 / 168 CC 17 SCAR 16.6
CC
17
Cov
86%
_STRINGLIKE0
17.7
_process_args
9.3
_add_route
3.0
CRAP 5.3
↑ Summary
1 package CatalystX::Routes;
2 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 $CatalystX::Routes::VERSION = '0.02';
4 }
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Moose::Util qw( apply_all_roles );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Params::Util qw( _CODELIKE _REGEX _STRING );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Moose::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 Moose::Exporter->setup_import_methods(
16 with_meta => [qw( get get_html post put del chain_point )],
17 as_is => [qw( chained args capture_args path_part action_class_name )],
18 );
19  
20 7 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod documented
21 7 _add_route( 'GET', @_ );
22 }
23  
24 2 ▶sub get_html {
Subroutine get_html: called CC=1 SCAR=0.0
Pod documented
25 2 _add_route( 'GET_html', @_ );
26 }
27  
28 2 ▶sub post {
Subroutine post: called CC=1 SCAR=0.0
Pod documented
29 2 _add_route( 'POST', @_ );
30 }
31  
32 2 ▶sub put {
Subroutine put: called CC=1 SCAR=0.0
Pod documented
33 2 _add_route( 'PUT', @_ );
34 }
35  
36 2 ▶sub del {
Subroutine del: called CC=1 SCAR=0.0
Pod documented
37 2 _add_route( 'DELETE', @_ );
38 }
39  
40 15 ▶sub _add_route {
Subroutine _add_route: called CC=3 SCAR=11.0
41 15 my $rest = shift;
42 15 my $meta = shift;
43 15 my ( $attrs, $sub ) = _process_args( $meta, @_ );
44  
45 15 ▶ unless ( exists $attrs->{Chained} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $attrs->{"Chained"}) 12 3 15
46 12 $attrs->{Chained} = q{/};
47 }
48  
49 15 my $name = $_[0];
50 15 $name =~ s{^/}{};
51  
52 # We need to turn the full chain name into a path, since two end points
53 # from two different chains could have the same end point name.
54 15 ▶ $name = ( $attrs->{Chained} eq '/' ? q{} : $attrs->{Chained} ) . q{/}
Branch: 1 branch
BranchTrueFalseTotal
$attrs->{'Chained'} eq '/' ? : 12 3 15
55 . $name;
56  
57 15 $name =~ s{/}{|}g;
58  
59 15 my $meth_base = '__route__' . $name;
60  
61 15 _maybe_add_rest_route( $meta, $meth_base, $attrs );
62  
63 15 my $meth_name = $meth_base . q{_} . $rest;
64  
65 15 ▶ $meta->add_method( $meth_name => sub { goto &$sub } );
Subroutine __route__|foo_GET_html: called CC=1 SCAR=0.0
66  
67 15 return;
68 }
69  
70 4 ▶sub chain_point {
Subroutine chain_point: called CC=1 SCAR=0.0
Pod documented
71 4 my $meta = shift;
72 4 my $name = shift;
73 4 _add_chain_point( $meta, $name, chain_point => 1, @_ );
74 }
75  
76 4 ▶sub _add_chain_point {
Subroutine _add_chain_point: called CC=1 SCAR=0.0
77 4 my $meta = shift;
78 4 my ( $attrs, $sub ) = _process_args( $meta, @_ );
79  
80 4 my $name = $_[0];
81 4 $name =~ s{/}{|}g;
82  
83 4 $meta->add_method( $name => $sub );
84  
85 4 $meta->name()->config()->{actions}{$name} = $attrs;
86 }
87  
88 19 ▶sub _process_args {
Subroutine _process_args: called CC=9 SCAR=22.3
89 19 my $meta = shift;
90 19 my $path = shift;
91 19 my $sub = pop;
92  
93 19 my $caller = ( caller(2) )[3];
94  
95 19 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
unless &_STRINGLIKE0($path) or &_REGEX($path) 0 19 19
Condition33%1/3
&_STRINGLIKE0($path) or &_REGEX($path)
l!l&&r!l&&!r
1900
MC/DC0%0/2
&_STRINGLIKE0($path) or &_REGEX($path)
&_STRINGLIKE0($path) &_REGEX($path)
Truth table33%1/3
A or B
A= &_STRINGLIKE0($path)B= &_REGEX($path)
ABresult
1X1
011
000
96 "The $caller keyword expects a path string or regex as its first argument"
97 unless _STRINGLIKE0($path) || _REGEX($path);
98  
99 19 ▶ die "The $caller keyword expects a sub ref as its final argument"
Branch: 1 branch
BranchTrueFalseTotal
unless &_CODELIKE($sub) 0 19 19
100 unless _CODELIKE($sub);
101  
102 19 my %p = @_;
103  
104 19 ▶ unless ( delete $p{chain_point} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (delete $p{"chain_point"}) 15 4 19
105 15 ▶ $p{ActionClass} ||= 'REST::ForBrowsers';
Condition50%1/2
$p{"ActionClass"} ||= "REST::ForBrowsers"
l!l
015
MC/DC0%0/1
$p{"ActionClass"} ||= "REST::ForBrowsers"
$p{"ActionClass"}
Truth table50%1/2
A
A= $p{"ActionClass"}
Aresult
11
00
106 }
107  
108 19 ▶ unless ( $p{PathPart} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p{"PathPart"}) 15 4 19
109 15 my $part = $path;
110  
111 15 ▶ unless ( exists $p{Chained} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $p{"Chained"}) 12 3 15
112 12 ▶ unless ( $part =~ s{^/}{} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($part =~ s[^/][]) 7 5 12
113 7 $part = join q{/},
114 $meta->name()->action_namespace('FakeConfig'), $part;
115 7 $part =~ s{^/}{};
116 }
117 }
118  
119 15 $p{PathPart} = [$part];
120 }
121  
122 19 return \%p, $sub;
123 }
124  
125 15 ▶sub _maybe_add_rest_route {
Subroutine _maybe_add_rest_route: called CC=2 SCAR=6.9
126 15 my $meta = shift;
127 15 my $name = shift;
128 15 my $attrs = shift;
129  
130 15 ▶ return if $meta->has_method($name);
Branch: 1 branch
BranchTrueFalseTotal
if $meta->has_method($name) 8 7 15
131  
132 7 ▶ $meta->add_method( $name => sub { } );
Subroutine __route__|: called CC=1 SCAR=0.0
133  
134 7 $meta->name()->config()->{actions}{$name} = $attrs;
135  
136 7 return;
137 }
138  
139 7 ▶sub chained ($) {
Subroutine chained: called CC=1 SCAR=0.0
Pod documented
140 7 return ( Chained => $_[0] );
141 }
142  
143 5 ▶sub args ($) {
Subroutine args: called CC=1 SCAR=0.0
Pod documented
144 5 return ( Args => [ $_[0] ] );
145 }
146  
147 4 ▶sub capture_args ($) {
Subroutine capture_args: called CC=1 SCAR=0.0
Pod documented
148 4 return ( CaptureArgs => [ $_[0] ] );
149 }
150  
151 4 ▶sub path_part ($) {
Subroutine path_part: called CC=1 SCAR=0.0
Pod documented
152 4 return ( PathPart => [ $_[0] ] );
153 }
154  
155 0 ▶sub action_class_name ($) {
Subroutine action_class_name: not called CC=1 SCAR=6.9
Pod documented
156 0 return ( ActionClass => [ $_[0] ] );
157 }
158  
159 # XXX - this should be added to Params::Util
160 19 ▶sub _STRINGLIKE0 ($) {
Subroutine _STRINGLIKE0: called CC=6 SCAR=28.7
161 19 ▶ return _STRING( $_[0] )
Condition26%4/15
&_STRING($_[0]) || defined $_[0] && $_[0] eq '' || builtin::blessed($_[0]) && overload::Method($_[0], '""') && length "$_[0]"
l!l&&r!l&&!r
1900
&_STRING($_[0]) or defined $_[0] and $_[0] eq ""
l!l&&r!l&&!r
1630
defined $_[0] and $_[0] eq ""
!ll&&!rl&&r
003
builtin::blessed($_[0]) and overload::Method($_[0], "\"\"") and length "$_[0]"
!ll&&!rl&&r
000
builtin::blessed($_[0]) and overload::Method($_[0], "\"\"")
!ll&&!rl&&r
000
MC/DC0%0/6
&_STRING($_[0]) || defined $_[0] && $_[0] eq '' || builtin::blessed($_[0]) && overload::Method($_[0], '""') && length "$_[0]"
&_STRING($_[0]) defined $_[0] $_[0] eq "" builtin::blessed($_[0]) overload::Method($_[0], "\"\"") length "$_[0]"
Truth table20%2/10
A or B and C || D and E and F
A= &_STRING($_[0])B= defined $_[0]C= $_[0] eq ""D= builtin::blessed($_[0])E= overload::Method($_[0], "\"\"")F= length "$_[0]"
ABCDEFresult
1XXXXX1
011XXX1
00X1111
0101111
00X0XX0
00X10X0
00X1100
0100XX0
01010X0
0101100
162 || ( defined $_[0]
163 && $_[0] eq q{} )
164 || ( blessed $_[0]
165 && overload::Method( $_[0], q{""} )
166 && length "$_[0]" );
167 }
168  
169 {
170  
171 # This is a nasty hack around some weird back compat code in
172 # Catalyst::Controller->action_namespace
173 package FakeConfig;
174 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
175 1 $FakeConfig::VERSION = '0.02';
176 }
177  
178 5 ▶ sub config {
Subroutine config: called CC=1 SCAR=0.0
179 5 return { case_sensitive => 0 };
180 }
181 }
182  
183 1 1;
184  
185 # ABSTRACT: Sugar for declaring RESTful chained actions in Catalyst
186  
187  
188  
189 =pod
190  
191 =head1 NAME
192  
193 CatalystX::Routes - Sugar for declaring RESTful chained actions in Catalyst
194  
195 =head1 VERSION
196  
197 version 0.02
198  
199 =head1 SYNOPSIS
200  
201 package MyApp::Controller::User;
202  
203 use Moose;
204 use CatalystX::Routes;
205  
206 BEGIN { extends 'Catalyst::Controller'; }
207  
208 # /user/:user_id
209  
210 chain_point '_set_user'
211 => chained '/'
212 => path_part 'user'
213 => capture_args 1
214 => sub {
215 my $self = shift;
216 my $c = shift;
217 my $user_id = shift;
218  
219 $c->stash()->{user} = ...;
220 };
221  
222 # GET /user/:user_Id
223 get ''
224 => chained('_set_user')
225 => args 0
226 => sub { ... };
227  
228 # GET /user/foo
229 get 'foo' => sub { ... };
230  
231 sub _post { ... }
232  
233 # POST /user/foo
234 post 'foo' => \&_post;
235  
236 # PUT /root
237 put '/root' => sub { ... };
238  
239 # /user/plain_old_catalyst
240 sub plain_old_catalyst : Local { ... }
241  
242 =head1 DESCRIPTION
243  
244 B<WARNING>: This module is still experimental. It works well, but the APIs may
245 change without warning.
246  
247 This module provides a sugar layer that allows controllers to declare chained
248 RESTful actions.
249  
250 Under the hood, all the sugar declarations are turned into Chained subs. All
251 chain end points are declared using one of C<get>, C<get_html>, C<post>,
252 C<put>, or C<del>. These will declare actions using the
253 L<Catalyst::Action::REST::ForBrowsers> action class from the
254 L<Catalyst::Action::REST> distribution.
255  
256 =head1 PUTTING IT ALL TOGETHER
257  
258 This module is merely sugar over Catalyst's built-in L<Chained
259 dispatching|Catalyst::DispatchType::Chained> and L<Catalyst::Action::REST>. It
260 helps to know how those two things work.
261  
262 =head1 SUGAR FUNCTIONS
263  
264 All of these functions will be exported into your controller class when you
265 use C<CatalystX::Routes>.
266  
267 =head2 get ...
268  
269 This declares a C<GET> handler.
270  
271 =head2 get_html
272  
273 This declares a C<GET> handler for browsers. Use this to generate a standard
274 HTML page for browsers while still being able to generate some sort of RESTful
275 data response for other clients.
276  
277 If a browser makes a C<GET> request and no C<get_html> action has been
278 declared, a C<get> action is used as a fallback. See
279 C<Catalyst::TraitFor::Request::REST::ForBrowsers> for details on how
280 "browser-ness" is determined.
281  
282 =head2 post ...
283  
284 This declares a C<POST> handler.
285  
286 =head2 put
287  
288 This declares a C<PUT> handler.
289  
290 =head2 del
291  
292 This declares a C<DELETE> handler.
293  
294 =head2 chain_point
295  
296 This declares an intermediate chain point that should not be exposed as a
297 public URI.
298  
299 =head2 chained $path
300  
301 This function takes a single argument, the previous chain point from which the
302 action is chained.
303  
304 =head2 args $number
305  
306 This declares the number of arguments that this action expects. This should
307 only be used for the end of a chain.
308  
309 =head2 capture_args $number
310  
311 The number of arguments to capture at this point in the chain. This should
312 only be used for the beginning or middle parts of a chain.
313  
314 =head2 path_part $path
315  
316 The path part for this part of the chain. If you are declaring a chain end
317 point with C<get>, etc., then this isn't necessary. By default, the name
318 passed to the initial sugar function will be converted to a path part. See
319 below for details.
320  
321 =head2 action_class_name $class
322  
323 Use this to declare an action class. By default, this will be
324 L<Catalyst::Action::REST::ForBrowsers> for end points. For other parts of a
325 chain, it simply won't be set.
326  
327 =head1 PATH GENERATION
328  
329 All of the end point function (C<get>, C<post>, etc.) take a path as the first
330 argument. By default, this will be used as the C<path_part> for the chain. You
331 can override this by explicitly calling C<path_part>, in which case the name
332 is essentially ignored (but still required).
333  
334 Note that it is legitimate to pass the empty string as the name for a chain's
335 end point.
336  
337 If the end point's name does not start with a slash, it will be prefixed with
338 the controller's namespace.
339  
340 If you don't specify a C<chained> value for an end point, then it will use the
341 root URI, C</>, as the root of the chain.
342  
343 By default, no arguments are specified for a chain's end point, meaning it
344 will accept any number of arguments.
345  
346 =head1 CAVEATS
347  
348 When adding subroutines for end points to your controller, a name is generated
349 for each subroutine based on the chained path to the subroutine. Some
350 template-based views will automatically pick a template based on the
351 subroutine's name if you don't specify one explicitly. This won't work very
352 well with the bizarro names that this module generates, so you are strongly
353 encouraged to specify a template name explicitly.
354  
355 =head1 BUGS
356  
357 Please report any bugs or feature requests to
358 C<bug-catalystx-routes@rt.cpan.org>, or through the web interface at
359 L<http://rt.cpan.org>. I will be notified, and then you'll automatically be
360 notified of progress on your bug as I make changes.
361  
362 =head1 AUTHOR
363  
364 Dave Rolsky <autarch@urth.org>
365  
366 =head1 COPYRIGHT AND LICENSE
367  
368 This software is Copyright (c) 2011 by Dave Rolsky.
369  
370 This is free software, licensed under:
371  
372 The Artistic License 2.0
373  
374 =cut
375  
376  
377 __END__
↑ Summary