blib/lib/Mojolicious/Plugin/Restify/OtherActions.pm

filter: Statementstmt 100% 21 / 21 Branchbran 50.0% 2 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod 100% 1 / 1 totaltotal 93.1% 27 / 29 CC 3 SCAR 7.9
CC
3
Cov
92%
__ANON__
3.0
register
1.0
BEGIN
1.0
CRAP 2.2
↑ Summary
1 package Mojolicious::Plugin::Restify::OtherActions;
2 1 $Mojolicious::Plugin::Restify::OtherActions::VERSION = '0.04';
3 1 ▶use Mojo::Base 'Mojolicious::Plugin::Restify';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶sub register() {
Subroutine register: called CC=1 SCAR=0.0
Pod documented
6 1 my $s = shift;
7 1 my ($app, $conf) = @_;
8 1 $s->SUPER::register(@_);
9 1 my $original_code = $app->routes->shortcuts->{collection};
10 $app->routes->add_shortcut(
11 # replace original shortcut
12 1 ▶ collection => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
13 1 my $coll = $original_code->(@_);
14 1 my $r = shift;
15 1 my $path = shift;
16 1 ▶ my $options = ref $_[0] eq 'HASH' ? shift : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 1 0 1
17 1 my $rname = $options->{route_name};
18 1 my $or = $r->find($rname);
19 1 ▶ if ($or->to_string =~ /:${rname}_id/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($or->to_string =~ /:${rname}_id/u) 1 0 1
20 # M::P::Restify give same name to /accounts and /accounts/:accounts_id
21 # if I'm here my proposed patch
22 # https://github.com/kwakwaversal/mojolicious-plugin-restify/pull/19
23 # has not been still accepted, so I rename :accounts_id route
24 1 $or->name("${rname}_id");
25 # and find route again to match /accounts
26 1 $or = $r->find("$options->{route_name}")
27 }
28 $or->get("list/:query/*opt")->to(action => 'list',
29 1 opt => undef)->name($options->{route_name} . "_otheractions");
30 1 return $coll;
31  
32 }
33 1 );
34 }
35  
36 1 1;
37  
38 =pod
39  
40 =head1 NAME
41  
42 Mojolicious::Plugin::Restify::OtherActions - Mojolicious plug-in which extends Restify with more actions
43  
44 =for html <p><img alt="GitHub last commit" src="https://img.shields.io/github/last-commit/EmilianoBruni/Mojolicious-Plugin-Restify-OtherActions?style=plastic"> <a href="https://travis-ci.com/EmilianoBruni/mojolicious-plugin-mongodbv2"><img alt="Travis tests" src="https://img.shields.io/travis/com/EmilianoBruni/Mojolicious-Plugin-Restify-OtherActions?label=Travis%20tests&style=plastic"></a></p>
45  
46 =head1 VERSION
47  
48 version 0.04
49  
50 =head1 SYNOPSIS
51  
52 plugin 'Restify::OtherActions';
53  
54 =head1 DESCRIPTION
55  
56 Extends L<Mojolicious::Plugin::Restify> allowing to call other methods over REST collection
57  
58 =encoding UTF-8
59  
60 =head1 USAGE
61  
62 When you create your controller (see L<Mojolicious::Plugin::Restify> documentation),
63 you can use, as an example, this list method
64  
65 sub list {
66 my $c = shift;
67 my $query = $c->stash('query');
68 return $c->$query if ($query);
69 ...your original list code ...
70 }
71  
72 to redirect your call to an alternative C<$query> method.
73  
74 As an example, if your endpoint is C</accounts> then C</accounts/list/my_method/other/parameters>
75 is redirect to C<< $c->my_method >> and remaining url is available in C<< $c->stash->('opt') >>.
76  
77 In addition to standard routes added by L<Mojolicious::Plugin::Restify>, a new route is added
78  
79 # Pattern Methods Name Class::Method Name
80 # ------- ------- ---- ------------------
81 # ....
82 # +/list/:query/*opt GET accounts_otheractions Accounts::list
83  
84 =head1 Notes about Mojolicious::Plugin::Restify
85  
86 This module extends L<Mojolicious::Plugin::Restify> but solves also a little bug in route naming.
87  
88 In L<Mojolicious::Plugin::Restify> /accounts and /accounts/:accounts_id have the same name (accounts).
89  
90 This module replace the second route appending "_id" so that in original module where there is
91  
92 # Pattern Methods Name Class::Method Name
93 # ------- ------- ---- ------------------
94 # ...
95 # +/:accounts_id * "accounts"
96  
97 here there is
98  
99 # +/:accounts_id * "accounts_id".
100  
101 There is a pull request in github repository for this little problem
102  
103 L<https://github.com/kwakwaversal/mojolicious-plugin-restify/pull/19>
104  
105 =head1 BUGS/CONTRIBUTING
106  
107 Please report any bugs through the web interface at L<https://github.com/EmilianoBruni/Mojolicious-Plugin-Restify-OtherActions/issues>
108 If you want to contribute changes or otherwise involve yourself in development, feel free to fork the Git repository from
109 L<https://github.com/EmilianoBruni/Mojolicious-Plugin-Restify-OtherActions/>.
110  
111 =head1 SUPPORT
112  
113 You can find this documentation with the perldoc command too.
114  
115 perldoc Mojolicious::Plugin::Restify::OtherActions
116  
117 =head1 AUTHOR
118  
119 Emiliano Bruni <info@ebruni.it>
120  
121 =head1 COPYRIGHT AND LICENSE
122  
123 This software is copyright (c) 2021 by Emiliano Bruni.
124  
125 This is free software; you can redistribute it and/or modify it under
126 the same terms as the Perl 5 programming language system itself.
127  
128 =cut
129  
130 __END__
↑ Summary