File Coverage

blib/lib/Mojolicious/Command/generate/routes_restsful_just_routes.pm
Criterion Covered Total %
statement 12 109 11.0
branch 0 60 0.0
condition 0 56 0.0
subroutine 4 11 36.3
pod 1 1 100.0
total 17 237 7.1


line stmt bran cond sub pod time code
1             package Mojolicious::Command::generate::routes_restsful_just_routes;
2 1     1   1663 use Lingua::EN::Inflect 'PL';
  1         22241  
  1         134  
3 1     1   12 use Mojo::Base 'Mojolicious::Command';
  1         1  
  1         11  
4            
5 1     1   230 use Mojo::Util qw(class_to_file class_to_path camelize class_to_path);
  1         2  
  1         177  
6            
7             has description =>
8             'Generate Mojolicious route code using a Mojolicious::Plugin::Routes::Restful hash';
9             has usage =>
10             "Usage: $0 generate Mojolicious route code using a Mojolicious::Plugin::Routes::Restful hash\n";
11            
12             our $VERSION = '0.0.1';
13             our @all_routes = ();
14 1     1   5 use Data::Dumper;
  1         1  
  1         1739  
15            
16             $Data::Dumper::Indent = 0;
17             $Data::Dumper::Terse = 1;
18            
19             sub run {
20 0     0 1   my ( $self, $name, $in_routes ) = @_;
21            
22            
23             # Script
24            
25 0           for my $sub_ref (qw/ PARENT CONFIG /) {
26             die __PACKAGE__, ": missing '$sub_ref' hash in parameters\n"
27 0 0         unless exists( $in_routes->{$sub_ref} );
28             }
29            
30 0           my $config = $in_routes->{CONFIG};
31            
32 0           my $routes = $in_routes->{PARENT};
33            
34 0           foreach my $key ( keys( %{$routes} ) ) {
  0            
35            
36             my $resource =
37 0           _make_site( "PARENT", $key, $routes->{$key}, $config, $key, $key );
38            
39 0           my $route = $routes->{$key};
40            
41 0           foreach my $inline_key ( keys( %{ $route->{INLINE} } ) ) {
  0            
42            
43             die __PACKAGE__, ": INLINE must be a Hash Ref\n"
44 0 0         if ( ref( $route->{INLINE} ) ne 'HASH' );
45            
46             _make_site( "INLINE", $inline_key, $route->{INLINE}->{$inline_key},
47 0           $config, $key, $resource, $routes->{$key}->{STASH} );
48            
49             }
50            
51 0           foreach my $sub_route_key ( keys( %{ $route->{CHILD} } ) ) {
  0            
52            
53             _make_site( "CHILD", $sub_route_key,
54             $route->{CHILD}->{$sub_route_key},
55 0           $config, $key, $resource, $routes->{$key}->{STASH} );
56            
57             }
58             }
59            
60 0           $self->render_to_rel_file( 'just_code', $name, \@all_routes);
61            
62            
63            
64             }
65            
66            
67             sub _get_methods {
68 0     0     my ($via) = @_;
69            
70 0 0         return "['GET']"
71             unless ($via);
72 0           my $valid = {
73             GET => 1,
74             POST => 1,
75             PUT => 1,
76             PATCH => 1,
77             DELETE => 1
78             };
79            
80 0           my @uc_via = map( uc($_), @{$via} );
  0            
81            
82 0           return Dumper(\@uc_via);
83            
84             }
85            
86             sub _make_site {
87            
88 0     0     my ( $type, $key, $route, $config, $parent, $resource, $parent_stash ) = @_;
89            
90 0   0       my $route_stash = $route->{STASH} || {};
91 0 0         $route_stash = { %{$route_stash}, %{$parent_stash} }
  0            
  0            
92             if ($parent_stash);
93 0   0       my $action = $route->{ACTION} || "show";
94 0           my $methods = _get_methods( $route->{VIA} );
95 0   0       my $controller = $route->{CONTROLLER} || $key;
96            
97 0 0         if ( $type eq 'PARENT' ) {
98            
99             $resource = _api_site( $key, $route->{API}, $config->{API} )
100 0 0         if ( exists( $route->{API} ) );
101            
102             return $resource || $key
103 0 0 0       if ( exists( $route->{API_ONLY} ) );
104            
105             push(@all_routes,{url=>"/$key",
106             methods=>$methods,
107             controller=>"$controller#$action",
108             stash=>Dumper( $route_stash)})
109            
110 0 0         unless ( exists( $route->{NO_ROOT} ) );
111            
112             push(@all_routes,{url=>"/$key/:id",
113             methods=>$methods,
114             controller=>"$controller#$action",
115             stash=>Dumper( $route_stash)})
116 0 0         unless ( exists( $route->{NO_ID} ) );
117            
118 0   0       return $resource || $key
119            
120             }
121 0   0       $controller = $route->{CONTROLLER} || $parent; #aways use parent on kids
122 0           $route_stash->{parent} = $resource;
123 0           $route_stash->{child} = $key;
124 0 0         if ( $type eq 'INLINE' ) {
    0          
125            
126 0   0       $action = $route->{ACTION} || $key;
127            
128             _sub_inline_api_site( $resource, $key, $route->{API}, $config->{API} )
129 0 0         if ( exists( $route->{API} ) );
130            
131             return
132 0 0         if ( exists( $route->{API_ONLY} ) );
133            
134 0 0         if ( exists( $route->{NO_ID} ) ) {
135 0           push(@all_routes,{url=>"/$parent/$key",
136             methods=>$methods,
137             controller=>"$controller#$action",
138             stash=>Dumper( $route_stash)});
139             }
140             else {
141 0           push(@all_routes,{url=>"/$parent/:id/$key",
142             methods=>$methods,
143             controller=>"$controller#$action",
144             stash=>Dumper( $route_stash)});
145            
146             }
147             }
148             elsif ( $type eq 'CHILD' ) {
149            
150             _sub_api_site( $resource, $key, $route->{API}, $config->{API} )
151 0 0         if ( exists( $route->{API} ) );
152            
153             return
154 0 0         if ( exists( $route->{API_ONLY} ) );
155            
156 0   0       $action = $route->{ACTION} || $key;
157 0           push(@all_routes,{url=>"/$parent/:id/$key",
158             methods=>$methods,
159             controller=>"$controller#$action",
160             stash=>Dumper( $route_stash)});
161 0           push(@all_routes,{url=>"/$parent/:id/$key/:child_id",
162             methods=>$methods,
163             controller=>"$controller#$action",
164             stash=>Dumper( $route_stash)});
165             }
166            
167             }
168            
169             sub _api_url {
170            
171 0     0     my ( $resource, $config ) = @_;
172 0   0       my $ver = $config->{VERSION} || "";
173 0   0       my $prefix = $config->{RESOURCE_PREFIX} || "";
174 0           my $url = join( "/", grep( $_ ne "", ( $ver, $prefix, $resource ) ) );
175 0           return $url;
176             }
177            
178             sub _api_site {
179            
180 0     0     my ( $key, $api, $config ) = @_;
181            
182 0   0       my $resource = $api->{RESOURCE} || PL($key);
183 0           my $verbs = $api->{VERBS};
184 0   0       my $stash = $api->{STASH} || {};
185 0   0       my $contoller = $api->{CONTROLLER} || $resource;
186 0   0       my $contoller_prefix = $config->{PREFIX} || "api";
187 0           my $url = _api_url( $resource, $config );
188            
189            
190            
191             push(@all_routes,{url=>"/" .$url ,
192             methods=>"['GET']",
193             controller=>"$contoller_prefix-$contoller#get",
194             stash=>Dumper($stash)})
195 0 0         if ( $verbs->{RETRIEVE} );
196            
197             push(@all_routes,{url=>"/" .$url."/:id" ,
198             methods=>"['GET']",
199             controller=>"$contoller_prefix-$contoller#get",
200             stash=>Dumper($stash)})
201 0 0         if ( $verbs->{RETRIEVE} );
202            
203            
204             push(@all_routes,{url=>"/" .$url ,
205             methods=>"['POST']",
206             controller=>"$contoller_prefix-$contoller#create",
207             stash=>Dumper($stash)})
208 0 0         if ( $verbs->{CREATE} );
209            
210             push(@all_routes,{url=>"/" .$url. "/:id",
211             methods=>"['PATCH']",
212             controller=>"$contoller_prefix-$contoller#update",
213             stash=>Dumper($stash)})
214 0 0         if ( $verbs->{UPDATE} );
215            
216             push(@all_routes,{url=>"/" .$url. "/:id",
217             methods=>"['PUT']",
218             controller=>"$contoller_prefix-$contoller#replace",
219             stash=>Dumper($stash)})
220 0 0         if ( $verbs->{REPLACE} );
221            
222             push(@all_routes,{url=>"/" .$url. "/:id",
223             methods=>"['DELETE']",
224             controller=>"$contoller_prefix-$contoller#delete",
225             stash=>Dumper($stash)})
226 0 0         if ( $verbs->{DELETE} );
227            
228 0           return $resource;
229            
230             }
231            
232             sub _sub_api_site {
233            
234 0     0     my ( $parent, $key, $api, $config ) = @_;
235            
236 0   0       my $child_resource = $api->{RESOURCE} || PL($key);
237 0           my $verbs = $api->{VERBS};
238 0   0       my $stash = $api->{STASH} || {};
239 0   0       my $child_controller = $api->{CONTROLLER} || $child_resource;
240 0   0       my $contoller_prefix = $config->{PREFIX} || "api";
241 0           my $url = _api_url( $parent, $config );
242 0           $stash->{parent} = $parent;
243 0           $stash->{child} = $child_resource;
244            
245            
246             push(@all_routes,{url=>"/" .$url ."/:id/" . $child_resource,
247             methods=>"['GET']",
248             controller=>"$contoller_prefix-$parent#$child_resource#get",
249             stash=>Dumper($stash)})
250 0 0         if ( $verbs->{RETRIEVE} );
251            
252             push(@all_routes,{url=>"/" .$url. "/:id/" . $child_resource."/:child_id",
253             methods=>"['GET']",
254             controller=>"$contoller_prefix-$child_controller#get",
255             stash=>Dumper($stash)})
256 0 0         if ( $verbs->{RETRIEVE} );
257            
258             push(@all_routes,{url=>"/" .$url. "/:id/" . $child_resource,
259             methods=>"['POST']",
260             controller=>"$contoller_prefix-$child_controller#create",
261             stash=>Dumper($stash)})
262 0 0         if ( $verbs->{CREATE} );
263            
264             push(@all_routes,{url=>"/" .$url. "/:id/" . $child_resource."/:child_id",
265             methods=>"['PUT']",
266             controller=>"$contoller_prefix-$child_controller#replace",
267             stash=>Dumper($stash)})
268 0 0         if ( $verbs->{REPLACE} );
269            
270             push(@all_routes,{url=>"/" .$url. "/:id/" . $child_resource."/:child_id",
271             methods=>"['PATCH']",
272             controller=>"$contoller_prefix-$child_controller#update",
273             stash=>Dumper($stash)})
274 0 0         if ( $verbs->{UPDATE} );
275            
276             push(@all_routes,{url=>"/" .$url. "/:id/" . $child_resource."/:child_id",
277             methods=>"['DELETE']",
278             controller=>"$contoller_prefix-$child_controller#delete",
279             stash=>Dumper($stash)})
280 0 0         if ( $verbs->{DELETE} );
281             }
282            
283             sub _sub_inline_api_site {
284            
285 0     0     my ( $parent, $key, $api, $config ) = @_;
286 0           my $verbs = $api->{VERBS};
287 0   0       my $child_resource = $api->{RESOURCE} || PL($key); #this should be action
288 0   0       my $stash = $api->{STASH} || {};
289 0   0       my $action = $api->{ACTION} || $child_resource;
290 0   0       my $contoller_prefix = $config->{PREFIX} || "api";
291 0           my $url = _api_url( $parent, $config );
292 0           $stash->{parent} = $parent;
293 0           $stash->{child} = $child_resource;
294            
295             push(@all_routes,{url=>"/" .$url. "/:id/" . $child_resource,
296             methods=>"['GET']",
297             controller=>"$contoller_prefix-$parent#$action",
298             stash=>Dumper($stash)})
299 0 0         if ( $verbs->{RETRIEVE} );
300            
301             push(@all_routes,{url=>"/" .$url. "/:id/" . $child_resource,
302             methods=>"['PATCH']",
303             controller=>"$contoller_prefix-$parent#$action",
304             stash=>Dumper($stash)})
305            
306 0 0         if ( $verbs->{UPDATE} );
307            
308             }
309            
310            
311             # Ooh. "Big Pink." It's the only gum with the breath-freshening power of ham.
312            
313             1;
314            
315             =pod
316            
317             =head1 NAME
318            
319             Mojolicious::Command::generate::routes_restsful_just_routes - Generate just the perl code for routes from a Mojolicious::Plugin::Routes::Restful HASH
320            
321             =head1 SYNOPSIS
322            
323             my $commands = Mojolicious::Commands->new;
324             my $gen = Mojolicious::Command::generate::routes_restsful_just_routes->new;
325             $gen->run('RoutesRestfulCode',{
326             CONFIG => { Namespaces => ['RouteRestfulApp::Controller'] },
327             PARENT => {...
328            
329             =head1 DESCRIPTION
330            
331             Give L a hash that was created for L
332             it will generate the code for the described routes.
333            
334             It is not intended to use this generator from the command line. Best to use it in a script. See the script dir for an example.
335            
336             See L for details on how to make a Hash for this generator.
337            
338             =head1 ATTRIBUTES
339            
340             L inherits all attributes from
341             L and implements the following new ones.
342            
343             =head2 description
344            
345             my $description = $app->description;
346             $app = $app->description('Foo');
347            
348             Short description of this command, used for the command list.
349            
350             =head2 usage
351            
352             my $usage = $app->usage;
353             $app = $app->usage('Foo');
354            
355             Usage information for this command, used for the help screen.
356            
357             =head1 METHODS
358            
359             L inherits all methods from
360             L and implements the following new ones.
361            
362             =head2 run
363            
364             $app->run($name,$hash);
365            
366             Generates the code. Where $name is the name of the file you want to create, and $hash is a valid L hash.
367            
368             =head1 SEE ALSO
369            
370             L, L, L, L.
371            
372             =head1 LICENSE AND COPYRIGHT
373            
374             Copyright 2016 John Scoles.
375             This program is free software; you can redistribute it and/or modify it
376             under the terms of either: the GNU General Public License as published
377             by the Free Software Foundation; or the Artistic License.
378             See http://dev.perl.org/licenses/ for more information.
379            
380             =cut
381            
382             __DATA__