Dancer2-Plugin-JsonApi/lib/Dancer2/Plugin/JsonApi/Registry.pm

62 lines
1.1 KiB
Perl

package Dancer2::Plugin::JsonApi::Registry;
use 5.32.0;
use Dancer2::Plugin::JsonApi::Registry::Schema;
use Carp;
use Moo;
use experimental qw/ signatures /;
=head1 DESCRIPTION
The registry for the different types of data managed by the plugin.
=head1 METHODS
=head2 serialize($type,$data,$extra_data={})
Returns the serialized form of C<$data>.
=cut
sub serialize ( $self, $type, $data, $extra_data = {} ) {
return $self->type($type)->serialize( $data, $extra_data );
}
has types => (
is => 'ro',
default => sub { +{} },
);
=head2 add_type($type, $definition = {})
Adds a data type to the registry.
=cut
sub add_type ( $self, $type, $definition = {} ) {
$self->{types}{$type} = Dancer2::Plugin::JsonApi::Registry::Schema->new(
registry => $self,
type => $type,
%$definition
);
}
=head2 type($type)
Returns the type's C<Dancer2::Plugin::JsonApi::Registry::Schema>. Throws an
error if the type does not exist.
=cut
sub type ( $self, $type ) {
return $self->types->{$type} //=
Dancer2::Plugin::JsonApi::Registry::Schema->new( type => $type );
}
1;
__END__