PONAPI/DAO/Request/Role/HasFields.pm

filter: Statementstmt 100% 28 / 28 Branchbran 91.6% 11 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 97.7% 43 / 44 CC 9 SCAR 19.5
CC
9
Cov
98%
_validate_fields
9.0
BEGIN
1.0
__ANON__
1.0
CRAP 7.0
« PONAPI/Builder/Document.pm ↑ Summary Test/PONAPI/Repository/MockDB.pm »
1 # ABSTRACT: DAO request role - `fields`
2 package PONAPI::DAO::Request::Role::HasFields;
3  
4 8 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 has fields => (
7 traits => [ 'Hash' ],
8 is => 'ro',
9 isa => 'HashRef',
10 88 ▶ default => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
11 8 handles => {
12 "has_fields" => 'count',
13 },
14 );
15  
16 sub _validate_fields {
17 14 ▶ my $self = shift;
Subroutine _validate_fields: called CC=9 SCAR=22.0
18 14 ▶ return unless $self->has_fields;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_fields 0 14 14
19  
20 14 my $fields = $self->fields;
21 14 my $repo = $self->repository;
22  
23 14 foreach my $fields_type ( keys %$fields ) {
24 18 ▶ if ( !$repo->has_type( $fields_type ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $repo->has_type($fields_type)) { } 2 16 18
25 2 $self->_bad_request( "Type `$fields_type` doesn't exist.", 404 );
26 }
27 else {
28 16 my $fields_array = $fields->{$fields_type};
29 16 my @fields = @$fields_array;
30  
31 16 my $ok = $repo->type_has_fields( $fields_type, \@fields );
32 16 ▶ next if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 11 5 16
33  
34 # Sigh... let's test for this:
35 # fields => { articles => [qw/ title authors /] }
36 # where authors is a *relationship*
37 # There can't be clashes, so yes, this is fine. Somehow.
38 # http://jsonapi.org/format/#document-resource-object-fields
39 5 my (@real_fields, @relationships);
40 5 foreach my $maybe_rel ( @fields ) {
41 6 ▶ if ( $repo->has_relationship($fields_type, $maybe_rel) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($repo->has_relationship($fields_type, $maybe_rel)) { } 2 4 6
42 2 push @relationships, $maybe_rel;
43 }
44 else {
45 4 push @real_fields, $maybe_rel;
46 }
47 }
48  
49 $ok = @real_fields
50 5 ▶ ? $repo->type_has_fields($fields_type, \@real_fields)
Branch: 1 branch
BranchTrueFalseTotal
@real_fields ? : 4 1 5
51 : 1;
52  
53 5 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 3 2 5
54 3 $self->_bad_request(
55 "Type `$fields_type` does not have at least one of the requested fields"
56 );
57 }
58 }
59 }
60 }
61  
62 8 ▶no Moose::Role; 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 __END__
65  
66 =pod
67  
68 =encoding UTF-8
69  
70 =head1 NAME
71  
72 PONAPI::DAO::Request::Role::HasFields - DAO request role - `fields`
73  
74 =head1 VERSION
75  
76 version 0.002012
77  
78 =head1 AUTHORS
79  
80 =over 4
81  
82 =item *
83  
84 Mickey Nasriachi <mickey@cpan.org>
85  
86 =item *
87  
88 Stevan Little <stevan@cpan.org>
89  
90 =item *
91  
92 Brian Fraser <hugmeir@cpan.org>
93  
94 =back
95  
96 =head1 COPYRIGHT AND LICENSE
97  
98 This software is copyright (c) 2016 by Mickey Nasriachi, Stevan Little, Brian Fraser.
99  
100 This is free software; you can redistribute it and/or modify it under
101 the same terms as the Perl 5 programming language system itself.
102  
103 =cut
« PONAPI/Builder/Document.pm ↑ Summary Test/PONAPI/Repository/MockDB.pm »