File Coverage

blib/lib/WebAPI/DBIC/Resource/JSONAPI/Role/Item.pm
Criterion Covered Total %
statement 3 10 30.0
branch n/a
condition n/a
subroutine 1 2 50.0
pod 0 1 0.0
total 4 13 30.7


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.003002';
3              
4 2     2   24142031 use Moo::Role;
  2         43655  
  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           my %id_search; @id_search{ @id_cols } = @{ $self->id };
  0            
  0            
30 0           $self->set( $self->set->search_rs(\%id_search) ); # narrow the set
31              
32             # set has been narrowed to the item, so we can render the item as if a set
33             # (which is what we need to do for JSON API, which doesn't really have an 'item')
34              
35 0           return $self->encode_json( $self->render_jsonapi_response() );
36             }
37              
38             1;
39              
40             __END__
41              
42             =pod
43              
44             =encoding UTF-8
45              
46             =head1 NAME
47              
48             WebAPI::DBIC::Resource::JSONAPI::Role::Item
49              
50             =head1 VERSION
51              
52             version 0.003002
53              
54             =head1 DESCRIPTION
55              
56             Provides methods to support the C<application/vnd.api+json> media type
57             for GET and HEAD requests for requests representing individual resources,
58             e.g. a single row of a database table.
59              
60             =head1 NAME
61              
62             WebAPI::DBIC::Resource::JSONAPI::Role::Item - methods related to handling JSON API requests for item resources
63              
64             =head1 AUTHOR
65              
66             Tim Bunce <Tim.Bunce@pobox.com>
67              
68             =head1 COPYRIGHT AND LICENSE
69              
70             This software is copyright (c) 2015 by Tim Bunce.
71              
72             This is free software; you can redistribute it and/or modify it under
73             the same terms as the Perl 5 programming language system itself.
74              
75             =cut