Document/Builder/Resource.pm

filter: Statementstmt 88.8% 48 / 54 Branchbran 76.9% 20 / 26 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.0% 9 / 10 Podpod 60.0% 3 / 5 totaltotal 81.0% 81 / 100 CC 17 SCAR 18.2
CC
17
Cov
83%
build
10.9
add_relationship
6.0
add_attribute
2.0
CRAP 6.2
« Document/Builder/Document.pm ↑ Summary Document/Builder.pm »
1 # ABSTRACT: document builder - resource
2 package PONAPI::Document::Builder::Resource;
3  
4 20 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 20 ▶use PONAPI::Document::Builder::Relationship;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 20 with 'PONAPI::Document::Builder',
9 'PONAPI::Document::Builder::Role::HasLinksBuilder',
10 'PONAPI::Document::Builder::Role::HasMeta';
11  
12 20 has id => ( is => 'ro', isa => 'Str', required => 1 );
13 20 has type => ( is => 'ro', isa => 'Str', required => 1 );
14  
15 has _attributes => (
16 init_arg => undef,
17 traits => [ 'Hash' ],
18 is => 'ro',
19 isa => 'HashRef',
20 lazy => 1,
21 25 ▶ default => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
22 20 handles => {
23 'has_attributes' => 'count',
24 'has_attribute_for' => 'exists',
25 # private ...
26 '_add_attribute' => 'set',
27 '_get_attribute' => 'get',
28 '_keys_attributes' => 'keys',
29 }
30 );
31  
32 has _relationships => (
33 init_arg => undef,
34 traits => [ 'Hash' ],
35 is => 'ro',
36 isa => 'HashRef[ PONAPI::Document::Builder::Relationship ]',
37 lazy => 1,
38 24 ▶ default => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
39 20 handles => {
40 'has_relationships' => 'count',
41 'has_relationship_for' => 'exists',
42 # private ...
43 '_add_relationship' => 'set',
44 '_get_relationship' => 'get',
45 '_keys_relationships' => 'keys',
46 }
47 );
48  
49 sub add_attribute {
50 38 ▶ my ( $self, $key, $value ) = @_;
Subroutine add_attribute: called CC=2 SCAR=7.0
Pod undocumented
51  
52 38 ▶ $self->raise_error( 400,
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_relationship_for($key) 0 38 38
53 title => 'Attribute key conflict, a relation already exists for key: ' . $key
54 ) if $self->has_relationship_for( $key );
55  
56 38 $self->_add_attribute( $key, $value );
57  
58 38 return $self;
59 }
60  
61 sub add_attributes {
62 10 ▶ my ( $self, %args ) = @_;
Subroutine add_attributes: called CC=2 SCAR=6.9
Pod documented
63 10 $self->add_attribute( $_, $args{ $_ } ) foreach keys %args;
64 10 return $self;
65 }
66  
67 sub add_relationship {
68 17 ▶ my ( $self, $key, $resource, $collection ) = @_;
Subroutine add_relationship: called CC=6 SCAR=17.9
Pod documented
69  
70 17 ▶ $self->raise_error( 400,
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_attribute_for($key) 0 17 17
71 title => 'Relationship key conflict, an attribute already exists for key: ' . $key
72 ) if $self->has_attribute_for( $key );
73  
74 17 ▶ my @resources =
Branch: 2 branches
BranchTrueFalseTotal
ref $resource eq 'ARRAY' ? : 3 14 17
ref $resource eq 'HASH' ? : 13 1 14
75 ( ref $resource eq 'ARRAY' ) ? @$resource :
76 ( ref $resource eq 'HASH' ) ? $resource :
77 die 'Relationship resource information must be a reference (HASH or ARRAY)';
78  
79 16 ▶ my $builder = $self->has_relationship_for($key)
Branch: 1 branch
BranchTrueFalseTotal
$self->has_relationship_for($key) ? : 1 15 16
80 ? $self->_get_relationship($key)
81 : PONAPI::Document::Builder::Relationship->new(
82 parent => $self,
83 name => $key,
84 collection => $collection,
85 );
86  
87 16 $builder->add_resource( $_ ) foreach @resources;
88  
89 16 $self->_add_relationship( $key => $builder );
90 }
91  
92 sub add_self_link {
93 0 ▶ my $self = shift;
Subroutine add_self_link: not called CC=1 SCAR=6.9
Pod documented
94 0 my $base = $self->find_root->req_base;
95 0 $self->links_builder->add_link( self => $base . $self->{type} . '/' . $self->{id} );
96 0 return $self;
97 }
98  
99 sub build {
100 23 ▶ my $self = shift;
Subroutine build: called CC=10 SCAR=23.9
Pod undocumented
101 23 my %args = @_;
102 23 my $result = {};
103  
104 23 $result->{id} = $self->id;
105 23 $result->{type} = $self->type;
106 23 ▶ $result->{links} = $self->links_builder->build if $self->has_links_builder;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_links_builder 10 13 23
107 23 ▶ $result->{meta} = $self->_meta if $self->has_meta;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_meta 1 22 23
108  
109 # support filtered output for attributes/relationships through args
110 23 my @field_filters;
111 0 @field_filters = @{ $args{fields}{ $self->type } }
112 23 ▶ if exists $args{fields} and exists $args{fields}{ $self->type };
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"fields"} and exists $args{"fields"}{$self->type} 0 23 23
Condition33%1/3
exists $args{"fields"} and exists $args{"fields"}{$self->type}
!ll&&!rl&&r
2300
MC/DC0%0/2
exists $args{"fields"} and exists $args{"fields"}{$self->type}
exists $args{"fields"} exists $args{"fields"}{$self->type}
Truth table33%1/3
A and B
A= exists $args{"fields"}B= exists $args{"fields"}{$self->type}
ABresult
0X0
100
111
113  
114 23 ▶ if ( $self->has_attributes ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_attributes) 16 7 23
115 my @attributes = @field_filters
116 16 ▶ ? grep { $self->has_attribute_for($_) } @field_filters
Branch: 1 branch
BranchTrueFalseTotal
@field_filters ? : 0 16 16
117 : $self->_keys_attributes;
118  
119 16 $result->{attributes} = +{ map { $_ => $self->_get_attribute($_) } @attributes };
120 }
121  
122 $result->{relationships} = +{
123 23 ▶ map { my $v = $self->_get_relationship($_); $v ? ( $_ => $v->build ) : () }
Branch: 3 branches
BranchTrueFalseTotal
if $self->has_relationships 10 13 23
$v ? : 14 0 14
@field_filters ? : 0 10 10
124 ( @field_filters ? @field_filters : $self->_keys_relationships )
125 } if $self->has_relationships;
126  
127 23 return $result;
128 }
129  
130 20 __PACKAGE__->meta->make_immutable;
131 20 ▶no Moose; 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
132  
133 __END__
134  
135 =pod
136  
137 =encoding UTF-8
138  
139 =head1 NAME
140  
141 PONAPI::Document::Builder::Resource - document builder - resource
142  
143 =head1 VERSION
144  
145 version 0.001001
146  
147 =head1 SYNOPSIS
148  
149 use PONAPI::Document::Builder::Resource;
150  
151 PONAPI::Document::Builder::Resource->new(
152 id => $id,
153 type => $type,
154 );
155  
156 =head1 DESCRIPTION
157  
158 C<PONAPI::Document::Builder::Resource> is used internally by
159 C<PONAPI::Document> to build C<{json:api}> documents. Generally,
160 these will be created by C<< PONAPI::Document->add_resources >>, which
161 will return an object that you can call C<add_relationship>,
162 C<add_attributes>, and others on.
163  
164 =head1 METHODS
165  
166 =over
167  
168 =item * new( id => $id, type => $type, parent => $parent )
169  
170 Create a new object. C<id> and C<type> are mandatory.
171  
172 Parent is assigned by C<< PONAPI::Document->add_resources >>,
173 so you don't need to specify it.
174  
175 =item * id
176  
177 Returns the id of this object.
178  
179 =item * type
180  
181 Returns the type of this object.
182  
183 =item * add_attributes( $attr_name => $value, ... )
184  
185 Adds the specified attributes to the object.
186  
187 =item * add_relationship( $relationship_name => $resource, $is_a_collection)
188  
189 Adds C<$resource> to the C<$relationship_name> relationship for this
190 object. Will create the relationship if it doesn't already exist.
191  
192 Note that trying to add multiple resources when C<$is_a_collection> is false
193 will result in an error.
194  
195 =item * add_links( $link_name => $url, ... )
196  
197 See L<PONAPI::Document/add_links>.
198  
199 =item * add_self_link
200  
201 See L<PONAPI::Document/add_self_link>.
202  
203 =item * add_meta
204  
205 See L<PONAPI::Document/add_meta>.
206  
207 =item * parent
208  
209 See L<PONAPI::Document/parent>.
210  
211 =item * find_root
212  
213 See L<PONAPI::Document/find_root>.
214  
215 =item * is_root
216  
217 See L<PONAPI::Document/is_root>.
218  
219 =back
220  
221 =cut
222  
223 =head1 AUTHORS
224  
225 =over 4
226  
227 =item *
228  
229 Mickey Nasriachi <mickey@cpan.org>
230  
231 =item *
232  
233 Stevan Little <stevan@cpan.org>
234  
235 =item *
236  
237 Brian Fraser <hugmeir@cpan.org>
238  
239 =back
240  
241 =head1 COPYRIGHT AND LICENSE
242  
243 This software is copyright (c) 2016 by Mickey Nasriachi, Stevan Little, Brian Fraser.
244  
245 This is free software; you can redistribute it and/or modify it under
246 the same terms as the Perl 5 programming language system itself.
247  
248 =cut
« Document/Builder/Document.pm ↑ Summary Document/Builder.pm »