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 38 ▶sub add_attribute {
Subroutine add_attribute: called CC=2 SCAR=7.0
Pod undocumented
50 38 my ( $self, $key, $value ) = @_;
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 10 ▶sub add_attributes {
Subroutine add_attributes: called CC=2 SCAR=6.9
Pod documented
62 10 my ( $self, %args ) = @_;
63 10 $self->add_attribute( $_, $args{ $_ } ) foreach keys %args;
64 10 return $self;
65 }
66  
67 17 ▶sub add_relationship {
Subroutine add_relationship: called CC=6 SCAR=17.9
Pod documented
68 17 my ( $self, $key, $resource, $collection ) = @_;
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 0 ▶sub add_self_link {
Subroutine add_self_link: not called CC=1 SCAR=6.9
Pod documented
93 0 my $self = shift;
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 23 ▶sub build {
Subroutine build: called CC=10 SCAR=23.9
Pod undocumented
100 23 my $self = shift;
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 0 ▶ ? 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__
« Document/Builder/Document.pm ↑ Summary Document/Builder.pm »