File Coverage

blib/lib/Dancer2/Plugin/JsonApi/Registry.pm
Criterion Covered Total %
statement 39 39 100.0
branch 2 2 100.0
condition 2 3 66.6
subroutine 9 9 100.0
pod 3 4 75.0
total 55 57 96.4


line stmt bran cond sub pod time code
1             package Dancer2::Plugin::JsonApi::Registry;
2             our $AUTHORITY = 'cpan:YANICK';
3             $Dancer2::Plugin::JsonApi::Registry::VERSION = '0.0.1';
4 10     10   835584 use 5.32.0;
  10         53  
5 10     10   4679 use Dancer2::Plugin::JsonApi::Schema;
  10         47  
  10         553  
6              
7 10     10   108 use Carp;
  10         22  
  10         840  
8              
9 10     10   67 use Moo;
  10         66  
  10         66  
10              
11 10     10   5213 use experimental qw/ signatures /;
  10         24  
  10         101  
12              
13 12     12 1 16033 sub serialize ( $self, $type, $data, $extra_data = {} ) {
  12         24  
  12         27  
  12         24  
  12         22  
  12         22  
14 12         42 return $self->type($type)->serialize( $data, $extra_data );
15             }
16              
17 2     2 0 38399 sub deserialize ( $self, $data, $included = [] ) {
  2         6  
  2         6  
  2         6  
  2         4  
18              
19             my $type =
20             ref $data->{data} eq 'ARRAY'
21             ? $data->{data}[0]->{type}
22 2 100       20 : $data->{data}{type};
23              
24 2         13 return $self->type($type)->deserialize( $data, $included );
25             }
26              
27             has types => (
28             is => 'ro',
29             default => sub { +{} },
30             );
31              
32             has app => ( is => 'ro', );
33              
34 8     8 1 142 sub add_type ( $self, $type, $definition = {} ) {
  8         18  
  8         43  
  8         17  
  8         16  
35 8         182 $self->{types}{$type} = Dancer2::Plugin::JsonApi::Schema->new(
36             registry => $self,
37             type => $type,
38             %$definition
39             );
40             }
41              
42 19     19 1 45 sub type ( $self, $type ) {
  19         39  
  19         35  
  19         33  
43 19   66     203 return $self->types->{$type} //=
44             Dancer2::Plugin::JsonApi::Schema->new( type => $type );
45             }
46              
47             1;
48              
49             __END__