| 1 | package WebAPI::DBIC::Resource::HAL::Role::ItemWritable; | ||||||||||||
| 2 | 25 | $WebAPI::DBIC::Resource::HAL::Role::ItemWritable::VERSION = '0.004002'; | |||||||||||
| 3 | |||||||||||||
| 4 | 25 | ▶ | use Carp qw(croak confess); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 25 | ▶ | use Devel::Dwarn; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 25 | ▶ | use Moo::Role; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | |||||||||||||
| 10 | 25 | requires 'decode_json'; | |||||||||||
| 11 | 25 | requires 'request'; | |||||||||||
| 12 | |||||||||||||
| 13 | 25 | requires '_pre_update_resource_method'; | |||||||||||
| 14 | |||||||||||||
| 15 | |||||||||||||
| 16 | 10 | ▶ | around '_build_content_types_accepted' => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | 10 | my $orig = shift; | |||||||||||
| 18 | 10 | my $self = shift; | |||||||||||
| 19 | 10 | my $types = $self->$orig(); | |||||||||||
| 20 | 10 | unshift @$types, { 'application/hal+json' => 'from_hal_json' }; | |||||||||||
| 21 | 10 | return $types; | |||||||||||
| 22 | 25 | }; | |||||||||||
| 23 | |||||||||||||
| 24 | |||||||||||||
| 25 | 2 | ▶ | sub from_hal_json { | ||||||||||
|
Subroutine
from_hal_json: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 26 | 2 | my $self = shift; | |||||||||||
| 27 | 2 | $self->_pre_update_resource_method( "_do_update_embedded_resources_hal" ); | |||||||||||
| 28 | 2 | my $data = $self->decode_json( $self->request->content ); | |||||||||||
| 29 | 2 | $self->update_resource($data, is_put_replace => 0); | |||||||||||
| 30 | 2 | return; | |||||||||||
| 31 | } | ||||||||||||
| 32 | |||||||||||||
| 33 | |||||||||||||
| 34 | 2 | ▶ | sub _do_update_embedded_resources_hal { | ||||||||||
|
Subroutine
_do_update_embedded_resources_hal: called CC=12 SCAR=45.6
| |||||||||||||
| 35 | 2 | my ($self, $item, $hal, $result_class) = @_; | |||||||||||
| 36 | |||||||||||||
| 37 | 2 | my $links = delete $hal->{_links}; | |||||||||||
| 38 | 2 | my $meta = delete $hal->{_meta}; | |||||||||||
| 39 | 2 | ▶ | my $embedded = delete $hal->{_embedded} || {}; | ||||||||||
|
Condition50%1/2
delete $hal->{'_embedded'} || {}
MC/DC0%0/1
delete $hal->{'_embedded'} || {}
delete $hal->{'_embedded'}
Truth table50%1/2
A
A= delete $hal->{'_embedded'}
| |||||||||||||
| 40 | |||||||||||||
| 41 | 2 | for my $rel (keys %$embedded) { | |||||||||||
| 42 | |||||||||||||
| 43 | 0 | ▶ | my $rel_info = $result_class->relationship_info($rel) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 44 | or die "$result_class doesn't have a '$rel' relation\n"; | ||||||||||||
| 45 | die "$result_class _embedded $rel isn't a 'single' relationship\n" | ||||||||||||
| 46 | 0 | ▶ | if $rel_info->{attrs}{accessor} ne 'single'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 47 | |||||||||||||
| 48 | 0 | my $rel_hal = $embedded->{$rel}; | |||||||||||
| 49 | 0 | ▶ | die "_embedded $rel data is not a hash\n" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 50 | if ref $rel_hal ne 'HASH'; | ||||||||||||
| 51 | |||||||||||||
| 52 | # work out what keys to copy from the subitem we're about to update | ||||||||||||
| 53 | # XXX this isn't required unless updating key fields - optimize | ||||||||||||
| 54 | 0 | my %fk_map; | |||||||||||
| 55 | 0 | my $cond = $rel_info->{cond}; | |||||||||||
| 56 | 0 | for my $sub_field (keys %$cond) { | |||||||||||
| 57 | 0 | my $our_field = $cond->{$sub_field}; | |||||||||||
| 58 | 0 | ▶ | $our_field =~ s/^self\.//x or confess "panic $rel $our_field"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 59 | 0 | ▶ | $sub_field =~ s/^foreign\.//x or confess "panic $rel $sub_field"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 60 | 0 | $fk_map{$our_field} = $sub_field; | |||||||||||
| 61 | |||||||||||||
| 62 | die "$result_class already contains a value for '$our_field'\n" | ||||||||||||
| 63 | 0 | ▶ | if defined $hal->{$our_field}; # null is ok | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 64 | } | ||||||||||||
| 65 | |||||||||||||
| 66 | # update this subitem (and any resources embedded in it) | ||||||||||||
| 67 | 0 | my $subitem = $item->$rel(); | |||||||||||
| 68 | 0 | $subitem = $self->_do_update_resource($subitem, $rel_hal, $rel_info->{source}); | |||||||||||
| 69 | |||||||||||||
| 70 | # copy the keys of the subitem up to the item we're about to update | ||||||||||||
| 71 | 0 | warn "$result_class $rel: propagating keys: @{[ %fk_map ]}\n" | |||||||||||
| 72 | 0 | ▶ | if $ENV{WEBAPI_DBIC_DEBUG}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 73 | 0 | ▶ | while ( my ($ourfield, $subfield) = each %fk_map) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 74 | 0 | $hal->{$ourfield} = $subitem->$subfield(); | |||||||||||
| 75 | } | ||||||||||||
| 76 | |||||||||||||
| 77 | # XXX perhaps save $subitem to optimise prefetch handling? | ||||||||||||
| 78 | } | ||||||||||||
| 79 | |||||||||||||
| 80 | 2 | return; | |||||||||||
| 81 | } | ||||||||||||
| 82 | |||||||||||||
| 83 | |||||||||||||
| 84 | 25 | 1; | |||||||||||
| 85 | |||||||||||||
| 86 | __END__ | ||||||||||||