| 1 | # ABSTRACT: document builder class | ||||||||||||||||||||||||||||
| 2 | package PONAPI::Document::Builder::Document; | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 15 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 15 | ▶ | use PONAPI::Document::Builder::Resource; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 15 | ▶ | use PONAPI::Document::Builder::Resource::Null; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 15 | ▶ | use PONAPI::Document::Builder::Errors; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 15 | with 'PONAPI::Document::Builder', | |||||||||||||||||||||||||||
| 11 | 'PONAPI::Document::Builder::Role::HasLinksBuilder', | ||||||||||||||||||||||||||||
| 12 | 'PONAPI::Document::Builder::Role::HasMeta', | ||||||||||||||||||||||||||||
| 13 | 'PONAPI::Document::Builder::Role::HasPagination'; | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 15 | 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 | 0 | ▶ | default => sub { 200 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 26 | 15 | 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 | 15 | ); | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | has req_path => ( | ||||||||||||||||||||||||||||
| 38 | is => 'ro', | ||||||||||||||||||||||||||||
| 39 | isa => 'Str', | ||||||||||||||||||||||||||||
| 40 | 18 | ▶ | default => sub { '/' }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 15 | ); | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 15 | 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::Document::Builder::Resource ]', | ||||||||||||||||||||||||||||
| 55 | lazy => 1, | ||||||||||||||||||||||||||||
| 56 | 9 | ▶ | default => sub { +[] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | 15 | 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::Document::Builder::Resource | PONAPI::Document::Builder::Resource::Null ]', | ||||||||||||||||||||||||||||
| 69 | lazy => 1, | ||||||||||||||||||||||||||||
| 70 | 9 | ▶ | default => sub { +[] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 71 | 15 | 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 | 15 | has errors_builder => ( | |||||||||||||||||||||||||||
| 81 | init_arg => undef, | ||||||||||||||||||||||||||||
| 82 | is => 'ro', | ||||||||||||||||||||||||||||
| 83 | isa => 'PONAPI::Document::Builder::Errors', | ||||||||||||||||||||||||||||
| 84 | lazy => 1, | ||||||||||||||||||||||||||||
| 85 | predicate => 'has_errors_builder', | ||||||||||||||||||||||||||||
| 86 | builder => '_build_errors_builder', | ||||||||||||||||||||||||||||
| 87 | ); | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 3 | ▶ | sub _build_errors_builder { PONAPI::Document::Builder::Errors->new( parent => $_[0] ) } | ||||||||||||||||||||||||||
|
Subroutine
_build_errors_builder: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | sub convert_to_collection { $_[0]->_set_is_collection(1) } | ||||||||||||||||||||||||||
|
Subroutine
convert_to_collection: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 7 | ▶ | sub has_errors { | ||||||||||||||||||||||||||
|
Subroutine
has_errors: called CC=3 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 94 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 95 | 7 | ▶ | return $self->errors_builder->has_errors | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->has_errors_builder and $self->errors_builder->has_errors
MC/DC0%0/2
$self->has_errors_builder and $self->errors_builder->has_errors
$self->has_errors_builder
$self->errors_builder->has_errors
Truth table33%1/3
A and B
A= $self->has_errors_builderB= $self->errors_builder->has_errors
| |||||||||||||||||||||||||||||
| 96 | if $self->has_errors_builder and $self->errors_builder->has_errors; | ||||||||||||||||||||||||||||
| 97 | 0 | return 0; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 19 | ▶ | sub has_resource { | ||||||||||||||||||||||||||
|
Subroutine
has_resource: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 101 | 19 | my $self = shift; | |||||||||||||||||||||||||||
| 102 | 19 | ▶ | $self->has_resource_builders && $self->_num_resource_builders > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 5 | ▶ | sub has_resources { | ||||||||||||||||||||||||||
|
Subroutine
has_resources: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 106 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 107 | 5 | ▶ | $self->has_resource_builders && $self->_num_resource_builders > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 10 | ▶ | sub add_resource { | ||||||||||||||||||||||||||
|
Subroutine
add_resource: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 111 | 10 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 10 | ▶ | die 'Cannot add more then one resource unless the Document is in collection mode' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->has_resource and not $self->is_collection
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
| |||||||||||||||||||||||||||||
| 114 | if $self->has_resource && !$self->is_collection; | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 9 | my $builder = PONAPI::Document::Builder::Resource->new( %args, parent => $self ); | |||||||||||||||||||||||||||
| 117 | 9 | $self->_add_resource_builder( $builder ); | |||||||||||||||||||||||||||
| 118 | 9 | return $builder; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 1 | ▶ | sub add_null_resource { | ||||||||||||||||||||||||||
|
Subroutine
add_null_resource: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 122 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 1 | my $builder = PONAPI::Document::Builder::Resource::Null->new( parent => $self ); | |||||||||||||||||||||||||||
| 125 | 1 | $self->_add_resource_builder( $builder ); | |||||||||||||||||||||||||||
| 126 | 1 | return $builder; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 9 | ▶ | sub add_included { | ||||||||||||||||||||||||||
|
Subroutine
add_included: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 130 | 9 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 131 | 9 | my $builder = PONAPI::Document::Builder::Resource->new( parent => $self, %args ); | |||||||||||||||||||||||||||
| 132 | 9 | $self->_add_included( $builder ); | |||||||||||||||||||||||||||
| 133 | 9 | return $builder; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | sub add_self_link { | ||||||||||||||||||||||||||
|
Subroutine
add_self_link: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 137 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 138 | 0 | $self->links_builder->add_link( self => $self->req_path ); | |||||||||||||||||||||||||||
| 139 | 0 | return $self; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 29 | ▶ | sub build { | ||||||||||||||||||||||||||
|
Subroutine
build: called CC=16 SCAR=27.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 143 | 29 | my $self = shift; | |||||||||||||||||||||||||||
| 144 | 29 | my %args = @_; | |||||||||||||||||||||||||||
| 145 | 29 | my $result = +{ jsonapi => { version => $self->version } }; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 29 | ▶ | if ( ! $self->has_errors_builder ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 15 | ▶ | $result->{meta} = $self->_meta if $self->has_meta; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 15 | ▶ | $result->{links} = $self->links_builder->build if $self->has_links_builder; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 15 | ▶ | if ( $self->has_resource_builders ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 8 | ▶ | if ( $self->is_collection ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | # if it is a collection, then | ||||||||||||||||||||||||||||
| 154 | # call build on each one ... | ||||||||||||||||||||||||||||
| 155 | 5 | $result->{data} = [ map { $_->build( %args ) } @{ $self->_resource_builders } ]; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | else { | ||||||||||||||||||||||||||||
| 158 | # if it is a single resource, | ||||||||||||||||||||||||||||
| 159 | # just use that one | ||||||||||||||||||||||||||||
| 160 | 3 | ▶ | $result->{data} = $self->_get_resource_builder(0)->build( %args ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 8 | ▶ | if ( $self->has_included ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 4 | my $included_builders = $self->_included; | |||||||||||||||||||||||||||
| 171 | 4 | my (@included, %seen); | |||||||||||||||||||||||||||
| 172 | 4 | foreach my $builder ( @$included_builders ) { | |||||||||||||||||||||||||||
| 173 | 8 | ▶ | next if $seen{$builder->{type}}{$builder->{id}}++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 8 | push @included, $builder->build( %args ); | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | 4 | $result->{included} = \@included; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | else { | ||||||||||||||||||||||||||||
| 180 | 7 | ▶ | if ( $self->is_collection ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 2 | $result->{data} = []; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | else { | ||||||||||||||||||||||||||||
| 184 | 5 | ▶ | die "[PANIC] OH NOES, THIS SHOULD NEVER HAPPEN!!!!!" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | if ! $self->has_meta; | ||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 28 | ▶ | if ( $self->has_errors_builder ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | 14 | my $errors = $self->errors_builder->build; | |||||||||||||||||||||||||||
| 192 | 14 | ▶ | if ( $errors ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | 14 | ▶ | $_->{status} //= $self->status for @$errors; | ||||||||||||||||||||||||||
|
Condition33%1/3
$_->{"status"} //= $self->status
MC/DC0%0/2
$_->{"status"} //= $self->status
$_->{"status"}
$self->status
Truth table33%1/3
A //= B
A= $_->{"status"}B= $self->status
| |||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | return +{ | ||||||||||||||||||||||||||||
| 196 | 14 | jsonapi => +{ version => $self->version }, | |||||||||||||||||||||||||||
| 197 | errors => $errors, | ||||||||||||||||||||||||||||
| 198 | }; | ||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 14 | return $result; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 15 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 205 | 15 | ▶ | no Moose; 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | __END__ | ||||||||||||||||||||||||||||