blib/lib/Catalyst/ActionRole/NamedFields.pm

filter: Statementstmt 94.4% 17 / 18 Branchbran 50.0% 1 / 2 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 84.6% 22 / 26 CC 3 SCAR 8.4
CC
3
Cov
86%
_build_named_fields
3.2
BEGIN
1.0
__ANON__
1.0
CRAP 2.3
↑ Summary
1 package Catalyst::ActionRole::NamedFields;
2  
3 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '0.001';
6  
7 1 has named_fields => (
8 is=>'ro',
9 required=>1,
10 lazy=>1,
11 builder=>'_build_named_fields');
12  
13 2 ▶ sub _build_named_fields {
Subroutine _build_named_fields: called CC=3 SCAR=11.6
14 2 my ($self) = @_;
15 2 ▶ my $fields = join ',', @{$self->attributes->{Field}||['']};
Condition50%1/2
$self->attributes->{'Field'} || ['']
l!l
20
MC/DC0%0/1
$self->attributes->{'Field'} || ['']
$self->attributes->{'Field'}
Truth table50%1/2
A
A= $self->attributes->{'Field'}
Aresult
11
00
16 2 my $cb = eval qq[
17 sub {
18 my \@args = \@{\$_[0]};
19 my \%query = \%{\$_[1]};
20 return $fields;
21 }];
22  
23 2 ▶ unless($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) { } 2 0 2
24 0 die "Trouble building Fields for action '$self': $@";
25 } else {
26 2 return $cb;
27 }
28 }
29  
30 2 ▶around 'execute', sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
31 2 my ($orig, $self, $controller, $ctx, @args) = @_;
32 2 local %_ = $self->named_fields->(\@args, $ctx->req->query_parameters);
33 2 local $_ = $ctx;
34 2 return $self->$orig($controller, $ctx, @args);
35 1 };
36  
37 1 1;
38  
39 =head1 NAME
40  
41 Catalyst::ActionRole::NamedFields - Name your fields
42  
43 =head1 SYNOPSIS
44  
45 package MyApp::Controller::Example;
46  
47 use Moose;
48 use MooseX::MethodAttributes;
49  
50 extends 'Catalyst::Controller';
51  
52 sub fields :Local Args(1) Does('NamedFields') Field(id=>$args[0],search=>$query{q}) {
53 # $_ is localized to $c
54 $_->res->body("id is '$_{id}' and search is '$_{search}'");
55 }
56  
57 __PACKAGE__->meta->make_immutable;
58  
59 For https://localhost/example/fields/arg1?q=terms
60  
61 "id is 'arg1' and search is 'terms'"
62  
63 =head1 DESCRIPTION
64  
65 This is functionality I needed for a different upcoming distribution but it was
66 clearly isolated well enough I thought it merited a stand alone release. By itself
67 maybe not so useful but you can use it to build interesting experiments.
68  
69 This defines an action attribute called 'Field', which can be used to map arguments
70 and query parameters for a request to the '%_' special variable. The value of the
71 attribute is evaluted into a callback, so be warned about what you put there. Its
72 evaluated in a context where @args is the action arguments (typically defined by the
73 Args or CaptureArgs attribute) and where %query is %{$c->req->query_parameters}. We
74 also localize $_ to be the current context or '$c' which you might find a useful
75 shorthand (although keeping mind that $_ gets clobbered easily in many common functions
76 such as map.
77  
78 The idea is to map some variables to %_ for ease of access. Although the syntax as
79 shown above is actually more verbose than the old fashioned way, one can vision some
80 additional action roles or attribute helpers that could build upon this for interesting
81 experiments.
82  
83 Documentation here is deliberately light since I imagine most won't have a direct use
84 for it unless one can review the source and tests for meaningful information.
85  
86 =head1 AUTHOR
87  
88 John Napiorkowski <jnapiork@cpan.org>
89
90 =head1 COPYRIGHT
91
92 Copyright (c) 2015 the above named AUTHOR
93
94 =head1 LICENSE
95
96 You may distribute this code under the same terms as Perl itself.
97
98 =cut
↑ Summary