Plugin/StructuredParameters.pm

filter: Statementstmt 83.3% 15 / 18 Branchbran 0.0% 0 / 4 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 85.7% 6 / 7 Podpod 100% 4 / 4 totaltotal 72.2% 26 / 36 CC 4 SCAR 15.0
CC
4
Cov
67%
isa_structured_parameter_exception
12.0
__ANON__
2.0
BEGIN
1.0
CRAP 4.5
« Utils/StructuredParameters.pm ↑ Summary TraitFor/Request/StructuredParameters.pm »
1 package Catalyst::Plugin::StructuredParameters;
2  
3 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶sub structured_body { return shift->req->structured_body(@_) }
Subroutine structured_body: called CC=1 SCAR=0.0
Pod documented
7 1 ▶sub structured_data { return shift->req->structured_data(@_) }
Subroutine structured_data: called CC=1 SCAR=0.0
Pod documented
8 2 ▶sub structured_query { return shift->req->structured_query(@_) }
Subroutine structured_query: called CC=1 SCAR=0.0
Pod documented
9  
10 0 ▶sub isa_structured_parameter_exception {
Subroutine isa_structured_parameter_exception: not called CC=3 SCAR=24.8
Pod documented
11 0 my ($self, $obj) = @_;
12 0 ▶ return 0 unless Scalar::Util::blessed($obj);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($obj) 0 0 0
13 0 ▶ return $obj->isa('Catalyst::Exception::StructuredParameter') ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$obj->isa('Catalyst::Exception::StructuredParameter') ? : 0 0 0
14 }
15  
16 1 ▶around request_class_traits => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
17 1 my ($orig, $self, @args) = @_;
18 1 my $traits = $self->$orig(@args);
19 1 ▶ return [ @{$traits||[]}, 'Catalyst::TraitFor::Request::StructuredParameters' ];
Condition50%1/2
$traits || []
l!l
10
MC/DC0%0/1
$traits || []
$traits
Truth table50%1/2
A
A= $traits
Aresult
11
00
20 1 };
21  
22 1 1;
23  
24 =head1 NAME
25  
26 Catalyst::Plugin::StructuredParameters - Plug to add the structured parameter request trait plus proxy methods
27  
28 =head1 SYNOPSIS
29  
30 package MyApp;
31 use Catalyst 'StructuredParameters';
32
33 MyApp->setup;
34  
35 package MyApp::Controller::Root;
36  
37 sub body :Local {
38 my ($self, $c) = @_;
39 my %clean = $c->structured_body
40 ->permitted(['person'], +{'email' => []})
41 ->namespace(['person'])
42 ->permitted(
43 'name',
44 'age',
45 'address' => ['street' => ['number', 'zip'],
46 +{'credit_cards' => [
47 'number',
48 'exp' => [qw/year month day/],
49 ]},
50 )->to_hash;
51  
52 ## Do something with the sanitized body parameters
53 }
54  
55 ## Don't forget to add code to handle any exceptions
56  
57 sub end :Action {
58 my ($self, $c) = @_;
59 if(my $error = $c->last_error) {
60 $c->clear_errors; ## Clear the error stack unless you want the default Catalyst error
61 if($c->isa_strong_parameter_exception($error)) {
62 ## Something here like return a Bad Request 4xx view or similar.
63 }
64 }
65 }
66  
67 ## Alternatively handle with L<CatalystX::Errors> (don't forget to add the plugin to your
68 ## application class.)
69
70 sub end :Action Does(RenderErrors) { }
71  
72 You should review L<Catalyst::TraitFor::Request::StructuredParameters> for a more detailed SYNOPSIS and
73 explanation of how all this works.
74  
75 =head1 DESCRIPTION
76  
77 This plugin will add in the L<Catalyst::TraitFor::Request::StructuredParameters> request class trait
78 and proxy some of its methods to the context. You might find this a bit less typing.
79  
80 All the main documentation is in L<Catalyst::TraitFor::Request::StructuredParameters>.
81  
82 NOTE: This plugin only works with For L<Catalyst> v5.90090 or greater. If you must use an older
83 version of L<Catalyst> you'll need to use the workaround described in the SYNOPSIS of
84 L<Catalyst::TraitFor::Request::StructuredParameters>.
85  
86 =head1 METHODS
87  
88 This role defines the following methods:
89  
90 =head2 structured_body
91  
92 =head2 structured_data
93  
94 =head2 structured_query
95  
96 These just proxy to the same methods under the L<Catalyst::Request> object.
97  
98 =head2 isa_structured_parameter_exception
99  
100 This is just a convenience method that returns true if a possible exception is both a blessed
101 object and ISA L<Catalyst::Exception::StructuredParameter>. Since you need to add checking for
102 this everytime I added this method to save a bit of trouble.
103  
104 =head1 AUTHOR
105  
106 See L<Catalyst::TraitFor::Request::StructuredParameters>
107  
108 =head1 SEE ALSO
109  
110 L<Catalyst>, L<Catalyst::TraitFor::Request::StructuredParameters>
111  
112 =head1 COPYRIGHT & LICENSE
113  
114 See L<Catalyst::TraitFor::Request::StructuredParameters>
115  
116 =cut
« Utils/StructuredParameters.pm ↑ Summary TraitFor/Request/StructuredParameters.pm »