| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 107 | 23 | ▶ | $result->{meta} = $self->_meta if $self->has_meta; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
exists $args{"fields"} and exists $args{"fields"}{$self->type}
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}
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 23 | ▶ | if ( $self->has_attributes ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | my @attributes = @field_filters | ||||||||||||||||||||||||||||
| 116 | 0 | ▶ | ? grep { $self->has_attribute_for($_) } @field_filters | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||