Document/Builder/Relationships.pm

filter: Statementstmt 91.8% 45 / 49 Branchbran 71.4% 10 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 85.7% 6 / 7 Podpod 25.0% 1 / 4 totaltotal 83.7% 62 / 74 CC 10 SCAR 12.5
CC
10
Cov
87%
build_links_document
5.1
build_document
4.0
build
3.0
CRAP 3.5
« Document.pm ↑ Summary Document/Builder/Role/Type.pm »
1 package JSONAPI::Document::Builder::Relationships;
2 6 $JSONAPI::Document::Builder::Relationships::VERSION = '2.0';
3 =head1 NAME
4  
5 JSONAPI::Document::Builder::Relationships - Related Resource Document builder
6  
7 =head1 VERSION
8  
9 version 2.0
10  
11 =head1 DESCRIPTION
12  
13 Builds the related resource document for a given row.
14  
15 =cut
16  
17 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 with 'JSONAPI::Document::Builder::Role::Parameters',
19 'JSONAPI::Document::Builder::Role::Attributes',
20 'JSONAPI::Document::Builder::Role::Type';
21  
22 6 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 =head2 row
25  
26 The C<DBIx::Class::Row> for C<relationship>.
27  
28 Note this is not the relationship row, rather
29 it is its parent.
30  
31 =cut
32  
33 6 has row => (
34 is => 'ro',
35 required => 1,
36 );
37  
38 =head2 relationship
39  
40 String name of the relationship.
41  
42 =cut
43  
44 6 has relationship => (
45 is => 'ro',
46 required => 1,
47 );
48  
49 =head2 with_attributes
50  
51 Boolean; Default: false
52  
53 If specified, will build the relationship with attributes
54 instead of links.
55  
56 Default behaviour is to build with links.
57  
58 =cut
59  
60 has with_attributes => (
61 is => 'ro',
62 0 ▶ default => sub { 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
63 6 );
64  
65 =head2 build : HashRef
66  
67 Main caller method; Builds the related resource document.
68  
69 =cut
70  
71 sub build {
72 21 ▶ my ($self) = @_;
Subroutine build: called CC=3 SCAR=11.1
Pod documented
73 21 my $row = $self->row;
74 21 my $rel = $self->relationship;
75  
76 21 ▶ unless ($row->has_relationship($rel)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($row->has_relationship($rel)) 0 21 21
77 0 return undef;
78 }
79  
80 21 ▶ if ($self->with_attributes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->with_attributes) 11 10 21
81 11 return $self->build_document($row, $rel);
82 }
83  
84 10 return $self->build_links_document($row, $rel);
85 }
86  
87 =head2 build_links_document(DBIx::Class::Row $row, Str $relationship) : HashRef
88  
89 Builds a HashRef containing strings that represent URLs for fetching
90 the given relationship, as well as the relationship ID(s).
91  
92 =cut
93  
94 sub build_links_document {
95 10 ▶ my ($self, $row, $relationship) = @_;
Subroutine build_links_document: called CC=5 SCAR=16.2
Pod undocumented
96  
97 10 ▶ unless ($self->api_url) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->api_url) 0 10 10
98 0 Carp::confess('Missing required argument: api_url');
99 }
100  
101 10 my $relationship_type = $self->document_type($relationship);
102  
103 10 my $data;
104 10 my $rel_info = $row->result_source->relationship_info($relationship);
105 10 ▶ if ($rel_info->{attrs}->{accessor} eq 'multi') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel_info->{'attrs'}{'accessor'} eq 'multi') { } 5 5 10
106 5 $data = [];
107 5 my @rs = $row->$relationship->all();
108 5 foreach my $related_row (@rs) {
109 8 push @$data, { id => $related_row->id, type => $relationship_type };
110 }
111 } else {
112 5 ▶ if (my $related_row = $row->$relationship) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $related_row = $row->$relationship) 5 0 5
113 5 $data = {
114 id => $related_row->id,
115 type => $relationship_type
116 };
117 }
118 }
119  
120 10 my $row_type = $self->document_type(lc($row->result_source->source_name()));
121  
122 return {
123 10 links => {
124 self => $self->api_url . '/'
125 . $row_type . '/'
126 . $row->id
127 . '/relationships/'
128 . $self->format_type($relationship),
129 related => $self->api_url . '/' . $row_type . '/' . $row->id . '/' . $self->format_type($relationship),
130 },
131 data => $data,
132 };
133 }
134  
135 =head2 build_document(DBIx::Class::Row $row, Str $relationship) : HashRef
136  
137 Builds a HashRef of the relationship(s) with attributes.
138  
139 =cut
140  
141 sub build_document {
142 11 ▶ my ($self, $row, $relationship) = @_;
Subroutine build_document: called CC=4 SCAR=14.0
Pod undocumented
143 11 my $rel_info = $row->result_source->relationship_info($relationship);
144 11 ▶ if ($rel_info->{attrs}->{accessor} eq 'multi') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel_info->{'attrs'}{'accessor'} eq 'multi') { } 5 6 11
145 5 my @results;
146 5 my @rs = $row->$relationship->all();
147 5 foreach my $related_row (@rs) {
148 8 push @results, $self->build_single_document($related_row, $relationship);
149 }
150 5 return { data => \@results };
151 } else {
152 6 ▶ if (my $related_row = $row->$relationship) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $related_row = $row->$relationship) { } 6 0 6
153 6 return { data => $self->build_single_document($related_row, $relationship), };
154 } else {
155 0 return { data => undef };
156 }
157 }
158 }
159  
160 =head2 build_single_document(DBIx::Class::Row $related_row, Str $relationship) : HashRef
161  
162 Builds a HashRef representing a single relationship row.
163  
164 =cut
165  
166 sub build_single_document {
167 14 ▶ my ($self, $related_row, $relationship) = @_;
Subroutine build_single_document: called CC=1 SCAR=0.0
Pod undocumented
168 return {
169 14 id => $related_row->id,
170 type => $self->document_type($relationship),
171 attributes => $self->get_attributes($related_row),
172 };
173 }
174  
175 6 1;
« Document.pm ↑ Summary Document/Builder/Role/Type.pm »