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

60 lines
1.0 KiB
Perl
Raw Normal View History

2023-10-31 14:39:35 +00:00
package Dancer2::Plugin::JsonApi::Registry;
use 5.32.0;
2023-10-31 19:54:28 +00:00
use Dancer2::Plugin::JsonApi::Registry::Schema;
2023-10-31 16:17:28 +00:00
use Carp;
2023-10-31 14:39:35 +00:00
use Moo;
use experimental qw/ signatures /;
=head1 DESCRIPTION
The registry for the different types of data managed by the plugin.
=head1 METHODS
2023-10-31 19:54:28 +00:00
=head2 serialize($type,$data,$extra_data={})
2023-10-31 14:39:35 +00:00
Returns the serialized form of C<$data>.
=cut
2023-10-31 19:54:28 +00:00
sub serialize($self,$type,$data,$extra_data={}) {
return $self->type($type)->top_level_serialize($data,$extra_data);
2023-10-31 14:39:35 +00:00
}
2023-10-31 16:17:28 +00:00
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={}) {
2023-10-31 19:54:28 +00:00
$self->{types}{$type} = Dancer2::Plugin::JsonApi::Registry::Schema->new(
2023-10-31 16:17:28 +00:00
type => $type,
%$definition
);
}
=head2 type($type)
Returns the type's C<Dancer2::Plugin::JsonApi::Registry::Type>. Throws an
error if the type does not exist.
=cut
sub type($self,$type) {
return $self->types->{$type} || carp "type '$type' not found\n";
}
2023-10-31 14:39:35 +00:00
1;
__END__