File Coverage

blib/lib/PONAPI/DAO/Request/Role/HasFields.pm
Criterion Covered Total %
statement 25 25 100.0
branch 11 12 91.6
condition n/a
subroutine 3 3 100.0
pod n/a
total 39 40 97.5


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   7778 use Moose::Role;
  8         18  
  8         124  
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 14     14   697 my $self = shift;
18 14 50       779 return unless $self->has_fields;
19              
20 14         667 my $fields = $self->fields;
21 14         577 my $repo = $self->repository;
22              
23 14         61 foreach my $fields_type ( keys %$fields ) {
24 18 100       79 if ( !$repo->has_type( $fields_type ) ) {
25 2         13 $self->_bad_request( "Type `$fields_type` doesn't exist.", 404 );
26             }
27             else {
28 16         38 my $fields_array = $fields->{$fields_type};
29 16         48 my @fields = @$fields_array;
30              
31 16         86 my $ok = $repo->type_has_fields( $fields_type, \@fields );
32 16 100       112 next if $ok;
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         10 my (@real_fields, @relationships);
40 5         29 foreach my $maybe_rel ( @fields ) {
41 6 100       30 if ( $repo->has_relationship($fields_type, $maybe_rel) ) {
42 2         8 push @relationships, $maybe_rel;
43             }
44             else {
45 4         15 push @real_fields, $maybe_rel;
46             }
47             }
48              
49             $ok = @real_fields
50 5 100       28 ? $repo->type_has_fields($fields_type, \@real_fields)
51             : 1;
52              
53 5 100       26 if (!$ok) {
54 3         21 $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     8   45240 no Moose::Role; 1;
  8         20  
  8         48  
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.002006
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