| 1 | # ABSTRACT: document builder - relationship | ||||||||||||||||||||||||||||
| 2 | package PONAPI::Document::Builder::Relationship; | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 25 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 25 | ▶ | use PONAPI::Document::Builder::Resource::Identifier; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 25 | with 'PONAPI::Document::Builder', | |||||||||||||||||||||||||||
| 9 | 'PONAPI::Document::Builder::Role::HasLinksBuilder', | ||||||||||||||||||||||||||||
| 10 | 'PONAPI::Document::Builder::Role::HasMeta'; | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 25 | has name => ( | |||||||||||||||||||||||||||
| 13 | is => 'ro', | ||||||||||||||||||||||||||||
| 14 | isa => 'Str', | ||||||||||||||||||||||||||||
| 15 | required => 1, | ||||||||||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | has _resource_id_builders => ( | ||||||||||||||||||||||||||||
| 19 | init_arg => undef, | ||||||||||||||||||||||||||||
| 20 | traits => [ 'Array' ], | ||||||||||||||||||||||||||||
| 21 | is => 'ro', | ||||||||||||||||||||||||||||
| 22 | isa => 'ArrayRef[ PONAPI::Document::Builder::Resource::Identifier ]', | ||||||||||||||||||||||||||||
| 23 | lazy => 1, | ||||||||||||||||||||||||||||
| 24 | 19 | ▶ | default => sub { +[] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 25 | predicate => '_has_resource_id_builders', | |||||||||||||||||||||||||||
| 26 | handles => { | ||||||||||||||||||||||||||||
| 27 | '_num_resource_id_builders' => 'count', | ||||||||||||||||||||||||||||
| 28 | # private ... | ||||||||||||||||||||||||||||
| 29 | '_add_resource_id_builder' => 'push', | ||||||||||||||||||||||||||||
| 30 | '_get_resource_id_builder' => 'get', | ||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 25 | has collection => ( | |||||||||||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||||||||||
| 36 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 2 | ▶ | sub has_resource { | ||||||||||||||||||||||||||
|
Subroutine
has_resource: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 40 | 2 | my $self = $_[0]; | |||||||||||||||||||||||||||
| 41 | 2 | ▶ | $self->_has_resource_id_builders && $self->_num_resource_id_builders > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 20 | ▶ | sub has_resources { | ||||||||||||||||||||||||||
|
Subroutine
has_resources: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 45 | 20 | my $self = $_[0]; | |||||||||||||||||||||||||||
| 46 | 20 | ▶ | $self->_has_resource_id_builders && $self->_num_resource_id_builders > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 24 | ▶ | sub add_resource { | ||||||||||||||||||||||||||
|
Subroutine
add_resource: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 50 | 24 | my ( $self, $resource ) = @_; | |||||||||||||||||||||||||||
| 51 | 24 | my $b = PONAPI::Document::Builder::Resource::Identifier->new( parent => $self, %$resource ); | |||||||||||||||||||||||||||
| 52 | 24 | ▶ | $b->add_meta( %{ $resource->{meta} } ) if $resource->{meta}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 24 | $self->_add_resource_id_builder( $b ); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | sub add_self_link { $_[0]->_add_relationship_link('self') } | ||||||||||||||||||||||||||
|
Subroutine
add_self_link: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | sub add_related_link { $_[0]->_add_relationship_link('related') } | ||||||||||||||||||||||||||
|
Subroutine
add_related_link: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | sub _add_relationship_link { | ||||||||||||||||||||||||||
|
Subroutine
_add_relationship_link: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 60 | 0 | my ( $self, $key ) = @_; | |||||||||||||||||||||||||||
| 61 | 0 | my $rec = $self->parent->build; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | $self->links_builder->add_link( | ||||||||||||||||||||||||||||
| 64 | $key => $self->find_root->req_base | ||||||||||||||||||||||||||||
| 65 | . $rec->{type} | ||||||||||||||||||||||||||||
| 66 | . '/' . $rec->{id} | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | . ( $key eq 'self' ? '/relationships' : '' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | . '/' . $self->name | ||||||||||||||||||||||||||||
| 69 | ); | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | return $self; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 18 | ▶ | sub build { | ||||||||||||||||||||||||||
|
Subroutine
build: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 75 | 18 | my $self = $_[0]; | |||||||||||||||||||||||||||
| 76 | 18 | my $result = {}; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 18 | ▶ | if ( $self->collection || $self->has_resources ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->collection or $self->has_resources
MC/DC50%1/2
$self->collection or $self->has_resources
$self->collection
$self->has_resources
Truth table66%2/3
A or B
A= $self->collectionB= $self->has_resources
| |||||||||||||||||||||||||||||
| 79 | # if it is a collection, then | ||||||||||||||||||||||||||||
| 80 | # call build on each one ... | ||||||||||||||||||||||||||||
| 81 | 5 | $result->{data} = [ map { $_->build } @{ $self->_resource_id_builders } ]; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | else { | ||||||||||||||||||||||||||||
| 84 | # if it is a single resource, | ||||||||||||||||||||||||||||
| 85 | # just use that one | ||||||||||||||||||||||||||||
| 86 | 13 | $result->{data} = $self->_get_resource_id_builder(0)->build; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 18 | ▶ | $result->{links} = $self->links_builder->build if $self->has_links_builder; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 18 | ▶ | $result->{meta} = $self->_meta if $self->has_meta; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 18 | return $result; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 25 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 96 | 25 | ▶ | no Moose; 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | __END__ | ||||||||||||||||||||||||||||