PONAPI/Builder/Document.pm

filter: Statementstmt 100% 84 / 84 Branchbran 93.7% 30 / 32 Conditioncond 77.7% 7 / 9 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 20 / 20 Podpod 0.0% 0 / 9 totaltotal 90.0% 144 / 160 CC 22 SCAR 19.8
CC
22
Cov
97%
build
16.0
has_errors
3.0
add_resource
3.0
CRAP 7.3
« PONAPI/DAO/Request.pm ↑ Summary PONAPI/DAO/Request/Role/HasFields.pm »
1 # ABSTRACT: document builder class
2 package PONAPI::Builder::Document;
3  
4 23 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 23 ▶use PONAPI::Builder::Resource;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 23 ▶use PONAPI::Builder::Resource::Null;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 23 ▶use PONAPI::Builder::Errors;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 23 with 'PONAPI::Builder',
11 'PONAPI::Builder::Role::HasLinksBuilder',
12 'PONAPI::Builder::Role::HasMeta',
13 'PONAPI::Builder::Role::HasPagination';
14  
15 23 has version => (
16 is => 'ro',
17 isa => 'Str',
18 required => 1,
19 );
20  
21 has status => (
22 init_arg => undef,
23 is => 'ro',
24 isa => 'Num',
25 71 ▶ default => sub { 200 },
Subroutine __ANON__: called CC=1 SCAR=0.0
26 23 writer => 'set_status',
27 lazy => 1,
28 predicate => 'has_status',
29 );
30  
31 has req_base => (
32 is => 'ro',
33 isa => 'Str',
34 18 ▶ default => sub { '/' },
Subroutine __ANON__: called CC=1 SCAR=0.0
35 23 );
36  
37 has req_path => (
38 is => 'ro',
39 isa => 'Str',
40 18 ▶ default => sub { '/' },
Subroutine __ANON__: called CC=1 SCAR=0.0
41 23 );
42  
43 23 has is_collection => (
44 is => 'ro',
45 writer => '_set_is_collection',
46 isa => 'Bool',
47 default => 0
48 );
49  
50 has _included => (
51 init_arg => undef,
52 traits => [ 'Array' ],
53 is => 'ro',
54 isa => 'ArrayRef[ PONAPI::Builder::Resource ]',
55 lazy => 1,
56 85 ▶ default => sub { +[] },
Subroutine __ANON__: called CC=1 SCAR=0.0
57 23 handles => {
58 'has_included' => 'count',
59 # private ...
60 '_add_included' => 'push',
61 }
62 );
63  
64 has _resource_builders => (
65 init_arg => undef,
66 traits => [ 'Array' ],
67 is => 'ro',
68 isa => 'ArrayRef[ PONAPI::Builder::Resource | PONAPI::Builder::Resource::Null ]',
69 lazy => 1,
70 85 ▶ default => sub { +[] },
Subroutine __ANON__: called CC=1 SCAR=0.0
71 23 predicate => 'has_resource_builders',
72 handles => {
73 '_num_resource_builders' => 'count',
74 # private ...
75 '_add_resource_builder' => 'push',
76 '_get_resource_builder' => 'get',
77 }
78 );
79  
80 23 has errors_builder => (
81 init_arg => undef,
82 is => 'ro',
83 isa => 'PONAPI::Builder::Errors',
84 lazy => 1,
85 predicate => 'has_errors_builder',
86 builder => '_build_errors_builder',
87 );
88  
89 99 ▶sub _build_errors_builder { PONAPI::Builder::Errors->new( parent => $_[0] ) }
Subroutine _build_errors_builder: called CC=1 SCAR=0.0
90  
91 28 ▶sub convert_to_collection { $_[0]->_set_is_collection(1) }
Subroutine convert_to_collection: called CC=1 SCAR=0.0
Pod undocumented
92  
93 sub has_errors {
94 137 ▶ my $self = shift;
Subroutine has_errors: called CC=3 SCAR=11.0
Pod undocumented
95 137 ▶ return $self->errors_builder->has_errors
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_errors_builder and $self->errors_builder->has_errors 112 25 137
Condition66%2/3
$self->has_errors_builder and $self->errors_builder->has_errors
!ll&&!rl&&r
250112
MC/DC50%1/2
$self->has_errors_builder and $self->errors_builder->has_errors
$self->has_errors_builder $self->errors_builder->has_errors
Truth table66%2/3
A and B
A= $self->has_errors_builderB= $self->errors_builder->has_errors
ABresult
0X0
100
111
96 if $self->has_errors_builder and $self->errors_builder->has_errors;
97 25 return 0;
98 }
99  
100 sub has_resource {
101 172 ▶ my $self = shift;
Subroutine has_resource: called CC=2 SCAR=6.9
Pod undocumented
102 172 ▶ $self->has_resource_builders && $self->_num_resource_builders > 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_resource_builders and $self->_num_resource_builders > 0 91 81 172
103 }
104  
105 sub has_resources {
106 5 ▶ my $self = shift;
Subroutine has_resources: called CC=2 SCAR=6.9
Pod undocumented
107 5 ▶ $self->has_resource_builders && $self->_num_resource_builders > 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_resource_builders and $self->_num_resource_builders > 1 3 2 5
108 }
109  
110 sub add_resource {
111 107 ▶ my ($self, %args) = @_;
Subroutine add_resource: called CC=3 SCAR=11.0
Pod undocumented
112  
113 107 ▶ die 'Cannot add more then one resource unless the Document is in collection mode'
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_resource and not $self->is_collection 1 106 107
Condition100%3/3
$self->has_resource and not $self->is_collection
!ll&&!rl&&r
78281
MC/DC100%2/2
$self->has_resource and not $self->is_collection
$self->has_resource not $self->is_collection
Truth table100%3/3
A and not(B)
A= $self->has_resourceB= not $self->is_collection
ABresult
0X0
100
111
114 if $self->has_resource && !$self->is_collection;
115  
116 106 my $builder = PONAPI::Builder::Resource->new( %args, parent => $self );
117 106 $self->_add_resource_builder( $builder );
118 106 return $builder;
119 }
120  
121 sub add_null_resource {
122 7 ▶ my $self = shift;
Subroutine add_null_resource: called CC=1 SCAR=0.0
Pod undocumented
123  
124 7 my $builder = PONAPI::Builder::Resource::Null->new( parent => $self );
125 7 $self->_add_resource_builder( $builder );
126 7 return $builder;
127 }
128  
129 sub add_included {
130 27 ▶ my ($self, %args) = @_;
Subroutine add_included: called CC=1 SCAR=0.0
Pod undocumented
131 27 my $builder = PONAPI::Builder::Resource->new( parent => $self, %args );
132 27 $self->_add_included( $builder );
133 27 return $builder;
134 }
135  
136 sub add_self_link {
137 28 ▶ my $self = shift;
Subroutine add_self_link: called CC=1 SCAR=0.0
Pod undocumented
138 28 $self->links_builder->add_link( self => $self->req_path );
139 28 return $self;
140 }
141  
142 sub build {
143 227 ▶ my $self = shift;
Subroutine build: called CC=16 SCAR=27.8
Pod undocumented
144 227 my %args = @_;
145 227 my $result = +{ jsonapi => { version => $self->version } };
146  
147 227 ▶ if ( ! $self->has_errors_builder ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_errors_builder) 117 110 227
148 117 ▶ $result->{meta} = $self->_meta if $self->has_meta;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_meta 40 77 117
149 117 ▶ $result->{links} = $self->links_builder->build if $self->has_links_builder;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_links_builder 34 83 117
150  
151 117 ▶ if ( $self->has_resource_builders ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_resource_builders) { } 88 29 117
152 88 ▶ if ( $self->is_collection ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_collection) { } 29 59 88
153 # if it is a collection, then
154 # call build on each one ...
155 29 $result->{data} = [ map { $_->build( %args ) } @{ $self->_resource_builders } ];
156 }
157 else {
158 # if it is a single resource,
159 # just use that one
160 59 ▶ $result->{data} = $self->_get_resource_builder(0)->build( %args )
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_resource 59 0 59
161 if $self->has_resource;
162 }
163  
164  
165 # http://jsonapi.org/format/#document-compound-documents
166 # "A compound document MUST NOT include more than one resource
167 # object for each type and id pair."
168 # So in short, we need to check that we don't have any duplicates.
169 88 ▶ if ( $self->has_included ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_included) 13 75 88
170 13 my $included_builders = $self->_included;
171 13 my (@included, %seen);
172 13 foreach my $builder ( @$included_builders ) {
173 26 ▶ next if $seen{$builder->{type}}{$builder->{id}}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$builder->{"type"}}{$builder->{"id"}}++ 1 25 26
174 25 push @included, $builder->build( %args );
175 }
176 13 $result->{included} = \@included;
177 }
178 }
179 else {
180 29 ▶ if ( $self->is_collection ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_collection) { } 3 26 29
181 3 $result->{data} = [];
182 }
183 else {
184 26 ▶ die "[PANIC] OH NOES, THIS SHOULD NEVER HAPPEN!!!!!"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_meta 1 25 26
185 if ! $self->has_meta;
186 }
187 }
188 }
189  
190 226 ▶ if ( $self->has_errors_builder ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_errors_builder) 110 116 226
191 110 my $errors = $self->errors_builder->build;
192 110 ▶ if ( $errors ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errors) 110 0 110
193 110 ▶ $_->{status} //= $self->status for @$errors;
Condition66%2/3
$_->{"status"} //= $self->status
l!l&&r!l&&!r
321050
MC/DC0%0/2
$_->{"status"} //= $self->status
$_->{"status"} $self->status
Truth table66%2/3
A //= B
A= $_->{"status"}B= $self->status
ABresult
1X1
011
000
194 }
195 return +{
196 110 jsonapi => +{ version => $self->version },
197 errors => $errors,
198 };
199 }
200  
201 116 return $result;
202 }
203  
204 23 __PACKAGE__->meta->make_immutable;
205 23 ▶no Moose; 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
206  
207 __END__
208  
209 =pod
210  
211 =encoding UTF-8
212  
213 =head1 NAME
214  
215 PONAPI::Builder::Document - document builder class
216  
217 =head1 VERSION
218  
219 version 0.002012
220  
221 =head1 AUTHORS
222  
223 =over 4
224  
225 =item *
226  
227 Mickey Nasriachi <mickey@cpan.org>
228  
229 =item *
230  
231 Stevan Little <stevan@cpan.org>
232  
233 =item *
234  
235 Brian Fraser <hugmeir@cpan.org>
236  
237 =back
238  
239 =head1 COPYRIGHT AND LICENSE
240  
241 This software is copyright (c) 2016 by Mickey Nasriachi, Stevan Little, Brian Fraser.
242  
243 This is free software; you can redistribute it and/or modify it under
244 the same terms as the Perl 5 programming language system itself.
245  
246 =cut
« PONAPI/DAO/Request.pm ↑ Summary PONAPI/DAO/Request/Role/HasFields.pm »