| 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
MC/DC0%0/2
$opts{"into"} ||= $caller
$opts{"into"}
$caller
Truth table66%2/3
A ||= B
A= $opts{"into"}B= $caller
| |||||||||||||||||||||||||||||
| 21 | 3 | ▶ | $opts{invocant} ||= '$self'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts{"invocant"} ||= "\$self"
MC/DC100%1/1
$opts{"invocant"} ||= "\$self"
$opts{"invocant"}
Truth table100%2/2
A
A= $opts{"invocant"}
| |||||||||||||||||||||||||||||
| 22 | 3 | ▶ | $opts{context} ||= '$ctx'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$opts{"context"} ||= "\$ctx"
MC/DC0%0/1
$opts{"context"} ||= "\$ctx"
$opts{"context"}
Truth table50%1/2
A
A= $opts{"context"}
| |||||||||||||||||||||||||||||
| 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 ||= ""
MC/DC0%0/1
$proto ||= ""
$proto
Truth table50%1/2
A
A= $proto
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $proto and length $proto
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
| |||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||