File Coverage

blib/lib/WebAPI/DBIC/Resource/ActiveModel/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::ActiveModel::Role::Item;
2             $WebAPI::DBIC::Resource::ActiveModel::Role::Item::VERSION = '0.004001';
3              
4 2     2   36237139 use Moo::Role;
  2         41899  
  2         10  
5              
6              
7             requires '_build_content_types_provided';
8             requires 'render_item_as_activemodel_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/json' => 'to_json_as_activemodel' };
18             return $types;
19             };
20              
21             #sub to_json_as_activemodel { return $_[0]->encode_json($_[0]->render_item_as_activemodel_hash($_[0]->item)) }
22             sub to_json_as_activemodel {
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_activemodel
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_activemodel_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::ActiveModel::Role::Item
50              
51             =head1 VERSION
52              
53             version 0.004001
54              
55             =head1 DESCRIPTION
56              
57             Provides methods to support the C<application/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             The response is intended to be compatible with the Ember Data ActiveModelAdapter
62             L<http://emberjs.com/api/data/classes/DS.ActiveModelAdapter.html>
63              
64             =head1 NAME
65              
66             WebAPI::DBIC::Resource::ActiveModel::Role::Item - methods related to handling requests for item resources
67              
68             =head1 AUTHOR
69              
70             Tim Bunce <Tim.Bunce@pobox.com>
71              
72             =head1 COPYRIGHT AND LICENSE
73              
74             This software is copyright (c) 2015 by Tim Bunce.
75              
76             This is free software; you can redistribute it and/or modify it under
77             the same terms as the Perl 5 programming language system itself.
78              
79             =cut