| 1 | package WebAPI::DBIC::Resource::ActiveModel::Role::ItemWritable; | ||||||||||||
| 2 | 25 | $WebAPI::DBIC::Resource::ActiveModel::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/json' => 'from_activemodel_json' }; | |||||||||||
| 21 | 10 | return $types; | |||||||||||
| 22 | 25 | }; | |||||||||||
| 23 | |||||||||||||
| 24 | |||||||||||||
| 25 | 0 | ▶ | sub from_activemodel_json { | ||||||||||
|
Subroutine
from_activemodel_json: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 26 | 0 | my $self = shift; | |||||||||||
| 27 | |||||||||||||
| 28 | 0 | $self->_pre_update_resource_method( "_do_update_embedded_resources_activemodel" ); | |||||||||||
| 29 | |||||||||||||
| 30 | 0 | my $data = $self->decode_json( $self->request->content ); | |||||||||||
| 31 | |||||||||||||
| 32 | 0 | $self->update_resource($data, is_put_replace => 0); | |||||||||||
| 33 | |||||||||||||
| 34 | 0 | return; | |||||||||||
| 35 | } | ||||||||||||
| 36 | |||||||||||||
| 37 | |||||||||||||
| 38 | 0 | ▶ | sub _do_update_embedded_resources_activemodel { | ||||||||||
|
Subroutine
_do_update_embedded_resources_activemodel: not called CC=12 SCAR=50.5
| |||||||||||||
| 39 | 0 | my ($self, $item, $activemodel, $result_class) = @_; | |||||||||||
| 40 | |||||||||||||
| 41 | 0 | ▶ | my $embedded = delete $activemodel->{_embedded} || {}; | ||||||||||
|
Condition0%0/2
delete $activemodel->{'_embedded'} || {}
MC/DC0%0/1
delete $activemodel->{'_embedded'} || {}
delete $activemodel->{'_embedded'}
Truth table0%0/2
A
A= delete $activemodel->{'_embedded'}
| |||||||||||||
| 42 | |||||||||||||
| 43 | 0 | for my $rel (keys %$embedded) { | |||||||||||
| 44 | |||||||||||||
| 45 | 0 | ▶ | my $rel_info = $result_class->relationship_info($rel) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 46 | or die "$result_class doesn't have a '$rel' relation\n"; | ||||||||||||
| 47 | die "$result_class _embedded $rel isn't a 'single' relationship\n" | ||||||||||||
| 48 | 0 | ▶ | if $rel_info->{attrs}{accessor} ne 'single'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 49 | |||||||||||||
| 50 | 0 | my $rel_activemodel = $embedded->{$rel}; | |||||||||||
| 51 | 0 | ▶ | die "_embedded $rel data is not a hash\n" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 52 | if ref $rel_activemodel ne 'HASH'; | ||||||||||||
| 53 | |||||||||||||
| 54 | # work out what keys to copy from the subitem we're about to update | ||||||||||||
| 55 | # XXX this isn't required unless updating key fields - optimize | ||||||||||||
| 56 | 0 | my %fk_map; | |||||||||||
| 57 | 0 | my $cond = $rel_info->{cond}; | |||||||||||
| 58 | 0 | for my $sub_field (keys %$cond) { | |||||||||||
| 59 | 0 | my $our_field = $cond->{$sub_field}; | |||||||||||
| 60 | 0 | ▶ | $our_field =~ s/^self\.//x or confess "panic $rel $our_field"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 61 | 0 | ▶ | $sub_field =~ s/^foreign\.//x or confess "panic $rel $sub_field"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 62 | 0 | $fk_map{$our_field} = $sub_field; | |||||||||||
| 63 | |||||||||||||
| 64 | die "$result_class already contains a value for '$our_field'\n" | ||||||||||||
| 65 | 0 | ▶ | if defined $activemodel->{$our_field}; # null is ok | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 66 | } | ||||||||||||
| 67 | |||||||||||||
| 68 | # update this subitem (and any resources embedded in it) | ||||||||||||
| 69 | 0 | my $subitem = $item->$rel(); | |||||||||||
| 70 | 0 | $subitem = $self->_do_update_resource($subitem, $rel_activemodel, $rel_info->{source}); | |||||||||||
| 71 | |||||||||||||
| 72 | # copy the keys of the subitem up to the item we're about to update | ||||||||||||
| 73 | 0 | warn "$result_class $rel: propagating keys: @{[ %fk_map ]}\n" | |||||||||||
| 74 | 0 | ▶ | if $ENV{WEBAPI_DBIC_DEBUG}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 75 | 0 | ▶ | while ( my ($ourfield, $subfield) = each %fk_map) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 76 | 0 | $activemodel->{$ourfield} = $subitem->$subfield(); | |||||||||||
| 77 | } | ||||||||||||
| 78 | |||||||||||||
| 79 | # XXX perhaps save $subitem to optimise prefetch handling? | ||||||||||||
| 80 | } | ||||||||||||
| 81 | |||||||||||||
| 82 | 0 | return; | |||||||||||
| 83 | } | ||||||||||||
| 84 | |||||||||||||
| 85 | |||||||||||||
| 86 | 25 | 1; | |||||||||||
| 87 | |||||||||||||
| 88 | __END__ | ||||||||||||