From d2508a2fecf04d017385bca1f1276786bdced259 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Tue, 31 Oct 2023 12:16:36 -0400 Subject: [PATCH 1/2] add the registry test --- t/registry.t | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 t/registry.t diff --git a/t/registry.t b/t/registry.t new file mode 100644 index 0000000..4b82f67 --- /dev/null +++ b/t/registry.t @@ -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(); From f7d4eadf7442e30e55e53ade764eb0b63dd73b4d Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Tue, 31 Oct 2023 12:17:28 -0400 Subject: [PATCH 2/2] add registry::type --- lib/Dancer2/Plugin/JsonApi/Registry.pm | 33 ++++++++++++++++++++- lib/Dancer2/Plugin/JsonApi/Registry/Type.pm | 10 +++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 lib/Dancer2/Plugin/JsonApi/Registry/Type.pm diff --git a/lib/Dancer2/Plugin/JsonApi/Registry.pm b/lib/Dancer2/Plugin/JsonApi/Registry.pm index 5630b51..1443a27 100644 --- a/lib/Dancer2/Plugin/JsonApi/Registry.pm +++ b/lib/Dancer2/Plugin/JsonApi/Registry.pm @@ -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. 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__ - diff --git a/lib/Dancer2/Plugin/JsonApi/Registry/Type.pm b/lib/Dancer2/Plugin/JsonApi/Registry/Type.pm new file mode 100644 index 0000000..4d76621 --- /dev/null +++ b/lib/Dancer2/Plugin/JsonApi/Registry/Type.pm @@ -0,0 +1,10 @@ +package Dancer2::Plugin::JsonApi::Registry::Type; + +use Moo; + +has type => ( + required => 1, + is => 'ro', +); + +1;