File Coverage

blib/lib/WebAPI/DBIC/Resource/ActiveModel/Role/ItemWritable.pm
Criterion Covered Total %
statement 9 36 25.0
branch 0 14 0.0
condition 0 2 0.0
subroutine 3 5 60.0
pod 0 1 0.0
total 12 58 20.6


line stmt bran cond sub pod time code
1             package WebAPI::DBIC::Resource::ActiveModel::Role::ItemWritable;
2             $WebAPI::DBIC::Resource::ActiveModel::Role::ItemWritable::VERSION = '0.004001';
3              
4 2     2   36862582 use Carp qw(croak confess);
  2         21  
  2         333  
5 2     2   1215 use Devel::Dwarn;
  2         22449  
  2         39  
6              
7 2     2   1297 use Moo::Role;
  2         39496  
  2         12  
8              
9              
10             requires 'decode_json';
11             requires 'request';
12              
13             requires '_pre_update_resource_method';
14              
15              
16             around '_build_content_types_accepted' => sub {
17             my $orig = shift;
18             my $self = shift;
19             my $types = $self->$orig();
20             unshift @$types, { 'application/json' => 'from_activemodel_json' };
21             return $types;
22             };
23              
24              
25             sub from_activemodel_json {
26 0     0 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             sub _do_update_embedded_resources_activemodel {
39 0     0     my ($self, $item, $activemodel, $result_class) = @_;
40              
41 0   0       my $embedded = delete $activemodel->{_embedded} || {};
42              
43 0           for my $rel (keys %$embedded) {
44              
45 0 0         my $rel_info = $result_class->relationship_info($rel)
46             or die "$result_class doesn't have a '$rel' relation\n";
47 0 0         die "$result_class _embedded $rel isn't a 'single' relationship\n"
48             if $rel_info->{attrs}{accessor} ne 'single';
49              
50 0           my $rel_activemodel = $embedded->{$rel};
51 0 0         die "_embedded $rel data is not a hash\n"
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 0         $our_field =~ s/^self\.//x or confess "panic $rel $our_field";
61 0 0         $sub_field =~ s/^foreign\.//x or confess "panic $rel $sub_field";
62 0           $fk_map{$our_field} = $sub_field;
63              
64 0 0         die "$result_class already contains a value for '$our_field'\n"
65             if defined $activemodel->{$our_field}; # null is ok
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 0         warn "$result_class $rel: propagating keys: @{[ %fk_map ]}\n"
  0            
74             if $ENV{WEBAPI_DBIC_DEBUG};
75 0           while ( my ($ourfield, $subfield) = each %fk_map) {
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             1;
87              
88             __END__
89              
90             =pod
91              
92             =encoding UTF-8
93              
94             =head1 NAME
95              
96             WebAPI::DBIC::Resource::ActiveModel::Role::ItemWritable
97              
98             =head1 VERSION
99              
100             version 0.004001
101              
102             =head1 NAME
103              
104             WebAPI::DBIC::Resource::ActiveModel::Role::ItemWritable - methods handling JSON API requests to update item resources
105              
106             =head1 AUTHOR
107              
108             Tim Bunce <Tim.Bunce@pobox.com>
109              
110             =head1 COPYRIGHT AND LICENSE
111              
112             This software is copyright (c) 2015 by Tim Bunce.
113              
114             This is free software; you can redistribute it and/or modify it under
115             the same terms as the Perl 5 programming language system itself.
116              
117             =cut