Advertisement
Guest User

Untitled

a guest
Apr 24th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.73 KB | None | 0 0
  1. use strict;
  2. use utf8;
  3. package Mojolicious::Plugin::CRUDRoutingHelper;
  4. {
  5.   $Mojolicious::Plugin::CRUDRoutingHelper::VERSION = '0.2.1';
  6. }
  7. # ABSTRACT: routing helpers for HTTP CRUD
  8.  
  9. use Mojo::Base 'Mojolicious::Plugin';
  10.  
  11. use Lingua::EN::Inflect qw/PL/;
  12.  
  13. sub register {
  14.     my ($self, $app) = @_;
  15.    
  16.     $app->routes->add_shortcut(
  17.         rest_routes => sub {
  18.             my $r = shift;
  19.             my $params = { @_ ? (ref $_[0] ? %{ $_[0] } : @_) : (0=>undef) };
  20.  
  21.             my $name = $params->{name};
  22.             my $readonly = $params->{readonly} || 0;
  23.            
  24.             my $plural = PL($name, 10);
  25.            
  26.             $app->log->debug("Creating routes for resource '$name'");
  27.            
  28.             #
  29.             # Generate "/$name" route, handled by controller $name
  30.             #
  31.             my $resource = $r->route("/$plural")->to("$name#");
  32.    
  33.             # GET requests - lists the collection of this resource
  34.             $resource->get->to('#rest_list')->name("list_$plural");
  35.             $app->log->info("Created route    GET ".$r->to_string."/$plural   (list_$plural)");
  36.    
  37.             if (!$readonly) {
  38.                 # POST requests - creates a new resource
  39.                 $resource->post->to('#rest_create')->name("create_$name");
  40.                 $app->log->info("Created route   POST ".$r->to_string."/$plural   (create_$name)");
  41.             };
  42.            
  43.             #
  44.             # Generate "/$name/:id" route, also handled by controller $name
  45.             #
  46.  
  47.             # resource routes might be chained, so we need to define an
  48.             # individual id and pass its name to the controller (idname)
  49.             $resource = $r->route("/$plural/:${name}id")->to("$name#", idname => "${name}id");
  50.            
  51.             # GET requests - lists a single resource
  52.             $resource->get->to('#rest_show')->name("show_$name");
  53.             $app->log->info("Created route    GET ".$r->to_string."/$plural/:${name}id   (show_$name)");
  54.            
  55.             if (!$readonly) {
  56.                 # DELETE requests - deletes a resource
  57.                 $resource->delete->to('#rest_remove')->name("delete_$name");
  58.                 $app->log->info("Created route DELETE ".$r->to_string."/$plural/:${name}id   (delete_$name)");
  59.                
  60.                 # PUT requests - updates a resource
  61.                 $resource->put->to('#rest_update')->name("update_$name");
  62.                 $app->log->info("Created route    PUT ".$r->to_string."/$plural/:${name}id   (update_$name)");
  63.             }    
  64.            
  65.             return $resource;
  66.         }
  67.     );
  68. }
  69.  
  70. 1;
  71.  
  72. __END__
  73.  
  74. =pod
  75.  
  76. =head1 NAME
  77.  
  78. Mojolicious::Plugin::CRUDRoutingHelper - routing helpers for HTTP CRUD
  79.  
  80. =head1 VERSION
  81.  
  82. version 0.2.1
  83.  
  84. =head1 DESCRIPTION
  85.  
  86. This Mojolicious plugin adds some routing helpers for CRUD operations via HTTP
  87. to the app. The routes are intended to be used by AJAX applications.
  88.  
  89. =head1 EXTENDS
  90.  
  91. =over 4
  92.  
  93. =item * L<Mojolicious::Plugin>
  94.  
  95. =back
  96.  
  97. =head1 METHODS
  98.  
  99. =head2 register
  100.  
  101. Adds the routing helpers. Is called by Mojolicious.
  102.  
  103. =head1 MOJOLICIOUS SHORTCUTS
  104.  
  105. =head2 rest_routes
  106.  
  107. Can be used to easily generate the needed RESTful routes for a resource.
  108.  
  109.     $self->rest_routes(name => 'user');
  110.     #    GET /api/users
  111.     #   POST /api/users
  112.     #    GET /api/users/:userid
  113.     # DELETE /api/users/:userid
  114.     #    PUT /api/users/:userid
  115.  
  116.     $self->rest_routes(name => 'log', readonly => 1);
  117.     #    GET /api/logs
  118.     #    GET /api/logs/:logid
  119.  
  120. The target controller has to implement the following methods:
  121.  
  122. =over 4
  123.  
  124. =item *
  125.  
  126. create
  127.  
  128. =item *
  129.  
  130. update
  131.  
  132. =item *
  133.  
  134. list
  135.  
  136. =item *
  137.  
  138. show
  139.  
  140. =item *
  141.  
  142. remove
  143.  
  144. =back
  145.  
  146. B<Parameters>
  147.  
  148. =over 4
  149.  
  150. =item *
  151.  
  152. name - name of the resource (will be used to generate the route and the controller name)
  153.  
  154. =item *
  155.  
  156. readonly - (optional) if set to 1, no create/update/delete routes will be created
  157.  
  158. =back
  159.  
  160. =head1 AUTHOR
  161.  
  162. Jens Berthold E<lt>jens.berthold@jebecs.deE<gt>
  163.  
  164. =head1 COPYRIGHT AND LICENSE
  165.  
  166. Copyright 2002-2013 by Jens Berthold E<lt>jens.berthold@jebecs.deE<gt>.
  167.  
  168. This library is free software; you can redistribute it and/or modify
  169. it under the same terms as Perl itself.
  170.  
  171. =cut
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement