File Coverage

blib/lib/PONAPI/DAO/Request/Role/HasFields.pm
Criterion Covered Total %
statement 26 26 100.0
branch 13 14 92.8
condition n/a
subroutine 3 3 100.0
pod n/a
total 42 43 97.6


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