49 lines
951 B
Perl
49 lines
951 B
Perl
package Dancer2::Plugin::JsonApi::Registry;
|
|
|
|
use 5.32.0;
|
|
use Dancer2::Plugin::JsonApi::Schema;
|
|
|
|
use Carp;
|
|
|
|
use Moo;
|
|
|
|
use experimental qw/ signatures /;
|
|
|
|
sub serialize ( $self, $type, $data, $extra_data = {} ) {
|
|
return $self->type($type)->serialize( $data, $extra_data );
|
|
}
|
|
|
|
sub deserialize ( $self, $data, $included = [] ) {
|
|
|
|
my $type =
|
|
ref $data->{data} eq 'ARRAY'
|
|
? $data->{data}[0]->{type}
|
|
: $data->{data}{type};
|
|
|
|
return $self->type($type)->deserialize( $data, $included );
|
|
}
|
|
|
|
has types => (
|
|
is => 'ro',
|
|
default => sub { +{} },
|
|
);
|
|
|
|
has app => ( is => 'ro', );
|
|
|
|
sub add_type ( $self, $type, $definition = {} ) {
|
|
$self->{types}{$type} = Dancer2::Plugin::JsonApi::Schema->new(
|
|
registry => $self,
|
|
type => $type,
|
|
%$definition
|
|
);
|
|
}
|
|
|
|
sub type ( $self, $type ) {
|
|
return $self->types->{$type} //=
|
|
Dancer2::Plugin::JsonApi::Schema->new( type => $type );
|
|
}
|
|
|
|
1;
|
|
|
|
__END__
|