CatalystX/Syntax/Action.pm

filter: Statementstmt 100% 32 / 32 Branchbran 50.0% 2 / 4 Conditioncond 58.3% 7 / 12 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 1 totaltotal 77.7% 49 / 63 CC 8 SCAR 12.4
CC
8
Cov
85%
parse_proto
5.5
_set_defaults
4.1
BEGIN
1.0
CRAP 3.5
↑ Summary Syntax/Feature/CatalystAction.pm »
1 package CatalystX::Syntax::Action;
2  
3 2 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use parent 'Devel::Declare::MethodInstaller::Simple';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings FATAL =>'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our $VERSION = '0.01';
10  
11 3 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
12 3 my $class = shift;
13 3 my $caller = caller;
14 3 my %opts = $class->_set_defaults($caller,@_);
15 3 $class->install_methodhandler(name=>'action', %opts);
16 }
17  
18 3 ▶sub _set_defaults {
Subroutine _set_defaults: called CC=4 SCAR=14.0
19 3 my ($class, $caller, %opts) = @_;
20 3 ▶ $opts{into} ||= $caller;
Condition66%2/3
$opts{"into"} ||= $caller
l!l&&r!l&&!r
120
MC/DC0%0/2
$opts{"into"} ||= $caller
$opts{"into"} $caller
Truth table66%2/3
A ||= B
A= $opts{"into"}B= $caller
ABresult
1X1
011
000
21 3 ▶ $opts{invocant} ||= '$self';
Condition100%2/2
$opts{"invocant"} ||= "\$self"
l!l
12
MC/DC100%1/1
$opts{"invocant"} ||= "\$self"
$opts{"invocant"}
Truth table100%2/2
A
A= $opts{"invocant"}
Aresult
11
00
22 3 ▶ $opts{context} ||= '$ctx';
Condition50%1/2
$opts{"context"} ||= "\$ctx"
l!l
03
MC/DC0%0/1
$opts{"context"} ||= "\$ctx"
$opts{"context"}
Truth table50%1/2
A
A= $opts{"context"}
Aresult
11
00
23 3 return %opts;
24 }
25  
26 10 ▶sub parse_proto {
Subroutine parse_proto: called CC=5 SCAR=17.0
Pod undocumented
27 10 my $self = shift;
28 10 my ($proto) = @_;
29 10 ▶ $proto ||= '';
Condition50%1/2
$proto ||= ""
l!l
010
MC/DC0%0/1
$proto ||= ""
$proto
Truth table50%1/2
A
A= $proto
Aresult
11
00
30 10 $proto =~ s/[\r\n]//g;
31 10 my $invocant = $self->{invocant};
32 10 my $context = $self->{context};
33  
34 10 ▶ $invocant = $1 if $proto =~ s{^(\$\w+):\s*}{};
Branch: 1 branch
BranchTrueFalseTotal
if $proto =~ s/^(\$\w+):\s*// 0 10 10
35  
36 10 my $inject = "my ${invocant} = shift; my ${context} = shift;";
37 10 ▶ $inject .= "my ($proto) = \@_;" if defined $proto and length $proto;
Branch: 1 branch
BranchTrueFalseTotal
if defined $proto and length $proto 0 10 10
Condition33%1/3
defined $proto and length $proto
!ll&&!rl&&r
0100
MC/DC0%0/2
defined $proto and length $proto
defined $proto length $proto
Truth table33%1/3
A and B
A= defined $protoB= length $proto
ABresult
0X0
100
111
38  
39 10 return $inject;
40 }
41  
42 2 1;
43  
44 =head1 NAME
45  
46 CatalystX::Syntax::Action - Semantically meaningful Catalyst Actions with signatures
47  
48 =head1 SYNOPSIS
49  
50 package MyApp::Web::Controller::Foo;
51  
52 use Moose;
53 use CatalystX::Syntax::Action;
54 use namespace::autoclean;
55  
56 extends 'Catalyst::Controller';
57  
58 action my_action($arg) : Path('my_special_action') Args(1)
59 {
60 $ctx->response->body('Look ma, no "my ($self, $ctx, $arg) = @_;"
61 }
62  
63 __PACKAGE__->meta->make_immutable;
64  
65 =head1 DESCRIPTION
66  
67 This module uses a bit of lightweight L<Devel::Declare> magic to add a new
68 keyword into your L<Catalyst::Controller> called C<action>. It works just
69 like L<Method::Signatures::Simple>, except it also C<shift>s off the C<context>
70 argument, saving you a bit of boilerplate. Additionally you might find that
71 calling an action 'action' more meaningfully separates it visually from methods
72 in your L<Catalyst::Controller> that are normal methods.
73  
74 On the other hand, it might break some IDE highlighting and so forth. Buyer
75 beware.
76  
77 The test suite has a working example of this for your review.
78  
79 =head1 CAVEATS
80  
81 When using L<Moose> style method modifiers you'll need to drop back to using
82 'classic' Perl subroutine syntax, or something like L<Function::Parameters>
83 since method modifiers change the incoming arguments. So for example:
84  
85 action myaction :path('foo') Args(1) { ... }
86  
87 Would need to be modified (as say in a ControllerRole) using:
88  
89 use Moose::Role;
90  
91 around 'myaction', sub {
92 my ($orig, $self, $ctx, @args) = @_;
93 ...
94 };
95  
96 This is because we can't detect the fact that C<$orig> as the first argument is
97 a coderef to the modified method rather than a blessed reference to an instance
98 of L<Catalyst::Controller>. If you are attached to the method signatures in
99 your code you could use L<Function::Parameters>:
100  
101 use Moose::Role;
102 use Function::Parameters;
103  
104 around 'myaction', fun($orig, $self, $ctx, @args) {
105 ...
106 };
107  
108 =head1 use syntax
109  
110 You can use the alternative L<syntax> module to activate this extention. This
111 would allow you to easily and cleanly enable multiple extenstions. For example:
112  
113 package MyApp::Web::Controller::Root;
114  
115 use Moose;
116 use syntax 'method', 'catalyst_action';
117  
118 extends 'Catalyst::Controller';
119  
120 action myaction { ... }
121 method mymethod { ...}
122  
123 __PACKAGE__->meta->make_immutable;
124  
125 =head1 THANKS
126  
127 I basically just copied the known working code in L<Method::Signatures::Simple>
128 to make this. I also pretty much copied L<Syntax::Feature::Method>. My thanks
129 to the authors and maintainers of those modules. I wouldn't have such a decent
130 low version offering without that stuff to guide me.
131  
132 =head1 AUTHOR
133  
134 John Napiorkowski L<email:jjnapiork@cpan.org>
135  
136 =head1 SEE ALSO
137  
138 L<Catalyst>, L<Method::Signatures::Simple>, L<syntax>, L<Devel::Declare>,
139 L<Syntax::Feature::Method>.
140  
141 =head1 COPYRIGHT & LICENSE
142  
143 Copyright 2011, John Napiorkowski L<email:jjnapiork@cpan.org>
144  
145 This library is free software; you can redistribute it and/or modify it under
146 the same terms as Perl itself.
147  
148 =cut
↑ Summary Syntax/Feature/CatalystAction.pm »