Merge branch 'registry-type'
This commit is contained in:
commit
ae39624e80
@ -1,6 +1,9 @@
|
||||
package Dancer2::Plugin::JsonApi::Registry;
|
||||
|
||||
use 5.32.0;
|
||||
use Dancer2::Plugin::JsonApi::Registry::Type;
|
||||
|
||||
use Carp;
|
||||
|
||||
use Moo;
|
||||
|
||||
@ -22,7 +25,35 @@ sub serialize($self,$type,$data,$meta={}) {
|
||||
return {};
|
||||
}
|
||||
|
||||
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::Type->new(
|
||||
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";
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
__END__
|
||||
|
||||
|
10
lib/Dancer2/Plugin/JsonApi/Registry/Type.pm
Normal file
10
lib/Dancer2/Plugin/JsonApi/Registry/Type.pm
Normal file
@ -0,0 +1,10 @@
|
||||
package Dancer2::Plugin::JsonApi::Registry::Type;
|
||||
|
||||
use Moo;
|
||||
|
||||
has type => (
|
||||
required => 1,
|
||||
is => 'ro',
|
||||
);
|
||||
|
||||
1;
|
23
t/registry.t
Normal file
23
t/registry.t
Normal file
@ -0,0 +1,23 @@
|
||||
use Test2::V0;
|
||||
|
||||
use Dancer2::Plugin::JsonApi::Registry;
|
||||
|
||||
use experimental qw/ signatures /;
|
||||
|
||||
my $registry = Dancer2::Plugin::JsonApi::Registry->new;
|
||||
|
||||
$registry->add_type(
|
||||
people => {
|
||||
id => 'id',
|
||||
links => {
|
||||
self => sub($data) {
|
||||
return "/peoples/$data->{id}"
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
isa_ok $registry->type('people') => 'Dancer2::Plugin::JsonApi::Registry::Type';
|
||||
|
||||
|
||||
done_testing();
|
Loading…
Reference in New Issue
Block a user