PONAPI/Builder/Resource.pm

filter: Statementstmt 96.3% 52 / 54 Branchbran 76.9% 20 / 26 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 5 totaltotal 83.0% 83 / 100 CC 17 SCAR 18.1
CC
17
Cov
88%
build
10.9
add_relationship
6.0
add_attribute
2.0
CRAP 6.1
« Test/PONAPI/Repository/MockDB.pm ↑ Summary PONAPI/DAO/Request/Role/HasDataMethods.pm »
1 # ABSTRACT: document builder - resource
2 package PONAPI::Builder::Resource;
3  
4 28 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 28 ▶use PONAPI::Builder::Relationship;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 28 with 'PONAPI::Builder',
9 'PONAPI::Builder::Role::HasLinksBuilder',
10 'PONAPI::Builder::Role::HasMeta';
11  
12 28 has id => ( is => 'ro', isa => 'Str', required => 1 );
13 28 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 140 ▶ default => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
22 28 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::Builder::Relationship ]',
37 lazy => 1,
38 139 ▶ default => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
39 28 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 356 ▶ my ( $self, $key, $value ) = @_;
Subroutine add_attribute: called CC=2 SCAR=7.0
Pod undocumented
51  
52 356 ▶ $self->raise_error( 400,
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_relationship_for($key) 0 356 356
53 title => 'Attribute key conflict, a relation already exists for key: ' . $key
54 ) if $self->has_relationship_for( $key );
55  
56 356 $self->_add_attribute( $key, $value );
57  
58 356 return $self;
59 }
60  
61 sub add_attributes {
62 28 ▶ my ( $self, %args ) = @_;
Subroutine add_attributes: called CC=2 SCAR=6.9
Pod undocumented
63 28 $self->add_attribute( $_, $args{ $_ } ) foreach keys %args;
64 28 return $self;
65 }
66  
67 sub add_relationship {
68 136 ▶ my ( $self, $key, $resource, $collection ) = @_;
Subroutine add_relationship: called CC=6 SCAR=17.9
Pod undocumented
69  
70 136 ▶ $self->raise_error( 400,
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_attribute_for($key) 0 136 136
71 title => 'Relationship key conflict, an attribute already exists for key: ' . $key
72 ) if $self->has_attribute_for( $key );
73  
74 136 ▶ my @resources =
Branch: 2 branches
BranchTrueFalseTotal
ref $resource eq 'ARRAY' ? : 3 133 136
ref $resource eq 'HASH' ? : 132 1 133
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 135 ▶ my $builder = $self->has_relationship_for($key)
Branch: 1 branch
BranchTrueFalseTotal
$self->has_relationship_for($key) ? : 24 111 135
80 ? $self->_get_relationship($key)
81 : PONAPI::Builder::Relationship->new(
82 parent => $self,
83 name => $key,
84 collection => $collection,
85 );
86  
87 135 $builder->add_resource( $_ ) foreach @resources;
88  
89 135 $self->_add_relationship( $key => $builder );
90 }
91  
92 sub add_self_link {
93 96 ▶ my $self = shift;
Subroutine add_self_link: called CC=1 SCAR=0.0
Pod undocumented
94 96 my $base = $self->find_root->req_base;
95 96 $self->links_builder->add_link( self => $base . $self->{type} . '/' . $self->{id} );
96 96 return $self;
97 }
98  
99 sub build {
100 379 ▶ my $self = shift;
Subroutine build: called CC=10 SCAR=23.9
Pod undocumented
101 379 my %args = @_;
102 379 my $result = {};
103  
104 379 $result->{id} = $self->id;
105 379 $result->{type} = $self->type;
106 379 ▶ $result->{links} = $self->links_builder->build if $self->has_links_builder;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_links_builder 343 36 379
107 379 ▶ $result->{meta} = $self->_meta if $self->has_meta;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_meta 1 378 379
108  
109 # support filtered output for attributes/relationships through args
110 379 my @field_filters;
111 0 @field_filters = @{ $args{fields}{ $self->type } }
112 379 ▶ 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 379 379
Condition33%1/3
exists $args{"fields"} and exists $args{"fields"}{$self->type}
!ll&&!rl&&r
37900
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 379 ▶ if ( $self->has_attributes ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_attributes) 341 38 379
115 my @attributes = @field_filters
116 341 ▶ ? grep { $self->has_attribute_for($_) } @field_filters
Branch: 1 branch
BranchTrueFalseTotal
@field_filters ? : 0 341 341
117 : $self->_keys_attributes;
118  
119 341 $result->{attributes} = +{ map { $_ => $self->_get_attribute($_) } @attributes };
120 }
121  
122 $result->{relationships} = +{
123 379 ▶ map { my $v = $self->_get_relationship($_); $v ? ( $_ => $v->build ) : () }
Branch: 3 branches
BranchTrueFalseTotal
if $self->has_relationships 319 60 379
$v ? : 416 0 416
@field_filters ? : 0 319 319
124 ( @field_filters ? @field_filters : $self->_keys_relationships )
125 } if $self->has_relationships;
126  
127 379 return $result;
128 }
129  
130 28 __PACKAGE__->meta->make_immutable;
131 28 ▶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::Builder::Resource - document builder - resource
142  
143 =head1 VERSION
144  
145 version 0.002012
146  
147 =head1 AUTHORS
148  
149 =over 4
150  
151 =item *
152  
153 Mickey Nasriachi <mickey@cpan.org>
154  
155 =item *
156  
157 Stevan Little <stevan@cpan.org>
158  
159 =item *
160  
161 Brian Fraser <hugmeir@cpan.org>
162  
163 =back
164  
165 =head1 COPYRIGHT AND LICENSE
166  
167 This software is copyright (c) 2016 by Mickey Nasriachi, Stevan Little, Brian Fraser.
168  
169 This is free software; you can redistribute it and/or modify it under
170 the same terms as the Perl 5 programming language system itself.
171  
172 =cut
« Test/PONAPI/Repository/MockDB.pm ↑ Summary PONAPI/DAO/Request/Role/HasDataMethods.pm »