File Coverage

blib/lib/WebAPI/DBIC/Resource/JSONAPI/Role/Item.pm
Criterion Covered Total %
statement 3 12 25.0
branch n/a
condition n/a
subroutine 1 2 50.0
pod 0 1 0.0
total 4 15 26.6


line stmt bran cond sub pod time code
1             package WebAPI::DBIC::Resource::JSONAPI::Role::Item;
2             $WebAPI::DBIC::Resource::JSONAPI::Role::Item::VERSION = '0.004001';
3              
4 2     2   37945982 use Moo::Role;
  2         42376  
  2         24  
5              
6              
7             requires '_build_content_types_provided';
8             requires 'render_item_as_jsonapi_hash';
9             requires 'encode_json';
10             requires 'item';
11              
12              
13             around '_build_content_types_provided' => sub {
14             my $orig = shift;
15             my $self = shift;
16             my $types = $self->$orig();
17             unshift @$types, { 'application/vnd.api+json' => 'to_json_as_jsonapi' };
18             return $types;
19             };
20              
21             #sub to_json_as_jsonapi { return $_[0]->encode_json($_[0]->render_item_as_jsonapi_hash($_[0]->item)) }
22             sub to_json_as_jsonapi {
23 0     0 0   my $self = shift;
24              
25             # narrow the set to just contain the specified item
26             # XXX this narrowing ought to be moved elsewhere
27             # it's a bad idea to be a side effect of to_json_as_jsonapi
28 0           my @id_cols = $self->set->result_source->unique_constraint_columns( $self->id_unique_constraint_name );
29 0           @id_cols = map { $self->set->current_source_alias.".$_" } @id_cols;
  0            
30 0           my %id_search; @id_search{ @id_cols } = @{ $self->id };
  0            
  0            
31 0           $self->set( $self->set->search_rs(\%id_search) ); # narrow the set
32              
33             # set has been narrowed to the item, so we can render the item as if a set
34             # (which is what we need to do for JSON API, which doesn't really have an 'item')
35              
36 0           return $self->encode_json( $self->render_jsonapi_response() );
37             }
38              
39             1;
40              
41             __END__
42              
43             =pod
44              
45             =encoding UTF-8
46              
47             =head1 NAME
48              
49             WebAPI::DBIC::Resource::JSONAPI::Role::Item
50              
51             =head1 VERSION
52              
53             version 0.004001
54              
55             =head1 DESCRIPTION
56              
57             Provides methods to support the C<application/vnd.api+json> media type
58             for GET and HEAD requests for requests representing individual resources,
59             e.g. a single row of a database table.
60              
61             =head1 NAME
62              
63             WebAPI::DBIC::Resource::JSONAPI::Role::Item - methods related to handling JSON API requests for item resources
64              
65             =head1 AUTHOR
66              
67             Tim Bunce <Tim.Bunce@pobox.com>
68              
69             =head1 COPYRIGHT AND LICENSE
70              
71             This software is copyright (c) 2015 by Tim Bunce.
72              
73             This is free software; you can redistribute it and/or modify it under
74             the same terms as the Perl 5 programming language system itself.
75              
76             =cut