move the Schema class
This commit is contained in:
parent
2e125c1771
commit
2acc581955
@ -9,7 +9,7 @@ vars:
|
||||
tasks:
|
||||
format:
|
||||
cmds:
|
||||
- git diff-ls {{.TARGET_BRANCH}} | grep -e '\.pm$\|\.t$' | xargs -IX perltidy -b X
|
||||
- git diff-ls --diff-filter=ACMR {{.TARGET_BRANCH}} | grep -e '\.pm$\|\.t$' | xargs -IX perltidy -b X
|
||||
|
||||
default:
|
||||
cmds:
|
||||
|
@ -1,7 +1,7 @@
|
||||
package Dancer2::Plugin::JsonApi::Registry;
|
||||
|
||||
use 5.32.0;
|
||||
use Dancer2::Plugin::JsonApi::Registry::Schema;
|
||||
use Dancer2::Plugin::JsonApi::Schema;
|
||||
|
||||
use Carp;
|
||||
|
||||
@ -39,7 +39,7 @@ Adds a data type to the registry.
|
||||
=cut
|
||||
|
||||
sub add_type ( $self, $type, $definition = {} ) {
|
||||
$self->{types}{$type} = Dancer2::Plugin::JsonApi::Registry::Schema->new(
|
||||
$self->{types}{$type} = Dancer2::Plugin::JsonApi::Schema->new(
|
||||
registry => $self,
|
||||
type => $type,
|
||||
%$definition
|
||||
@ -48,14 +48,14 @@ sub add_type ( $self, $type, $definition = {} ) {
|
||||
|
||||
=head2 type($type)
|
||||
|
||||
Returns the type's C<Dancer2::Plugin::JsonApi::Registry::Schema>. Throws an
|
||||
Returns the type's C<Dancer2::Plugin::JsonApi::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 );
|
||||
Dancer2::Plugin::JsonApi::Schema->new( type => $type );
|
||||
}
|
||||
|
||||
1;
|
||||
|
@ -1,6 +1,6 @@
|
||||
use 5.32.0;
|
||||
|
||||
package Dancer2::Plugin::JsonApi::Registry::Schema;
|
||||
package Dancer2::Plugin::JsonApi::Schema;
|
||||
|
||||
use Moo;
|
||||
|
10
t/registry.t
10
t/registry.t
@ -14,13 +14,15 @@ $registry->add_type(
|
||||
no warnings qw/ uninitialized /;
|
||||
return "/peoples/$data->{id}";
|
||||
}
|
||||
} } );
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
isa_ok $registry->type('people') =>
|
||||
'Dancer2::Plugin::JsonApi::Registry::Schema';
|
||||
isa_ok $registry->type('people') => 'Dancer2::Plugin::JsonApi::Schema';
|
||||
|
||||
like(
|
||||
$registry->serialize( people => {} ),
|
||||
{ jsonapi => { version => '1.0' } } );
|
||||
{ jsonapi => { version => '1.0' } }
|
||||
);
|
||||
|
||||
done_testing();
|
||||
|
@ -1,19 +1,19 @@
|
||||
use Test2::V0;
|
||||
|
||||
use Dancer2::Plugin::JsonApi::Registry::Schema;
|
||||
use Dancer2::Plugin::JsonApi::Schema;
|
||||
use Dancer2::Plugin::JsonApi::Registry;
|
||||
|
||||
use experimental qw/ signatures /;
|
||||
|
||||
my $type =
|
||||
Dancer2::Plugin::JsonApi::Registry::Schema->new( 'type' => 'thing' );
|
||||
Dancer2::Plugin::JsonApi::Schema->new( 'type' => 'thing' );
|
||||
|
||||
like $type->serialize( { attr1 => 'a', id => '123' }, { foo => 1 } ) => {
|
||||
jsonapi => { version => '1.0' },
|
||||
data => { type => 'thing', id => '123' }
|
||||
};
|
||||
|
||||
is( Dancer2::Plugin::JsonApi::Registry::Schema->new(
|
||||
is( Dancer2::Plugin::JsonApi::Schema->new(
|
||||
'type' => 'thing',
|
||||
id => 'foo'
|
||||
)->serialize( { foo => '123' } )->{data}{id} => '123',
|
||||
@ -35,12 +35,12 @@ is( $serialized->{data}{id} => '12',
|
||||
like $serialized->{data}, { links => { self => '/some/url' } }, "links";
|
||||
|
||||
sub schema_serialize ( $schema, $data ) {
|
||||
return Dancer2::Plugin::JsonApi::Registry::Schema->new(%$schema)
|
||||
return Dancer2::Plugin::JsonApi::Schema->new(%$schema)
|
||||
->serialize($data);
|
||||
}
|
||||
|
||||
like(
|
||||
Dancer2::Plugin::JsonApi::Registry::Schema->new(
|
||||
Dancer2::Plugin::JsonApi::Schema->new(
|
||||
type => 'thing',
|
||||
top_level_meta => {
|
||||
foo => 1,
|
||||
@ -54,7 +54,7 @@ like(
|
||||
|
||||
subtest 'attributes' => sub {
|
||||
my $serialized =
|
||||
Dancer2::Plugin::JsonApi::Registry::Schema->new( type => 'thing', )
|
||||
Dancer2::Plugin::JsonApi::Schema->new( type => 'thing', )
|
||||
->serialize( { id => 1, foo => 'bar' } );
|
||||
|
||||
is $serialized->{data} => {
|
||||
@ -67,7 +67,7 @@ subtest 'attributes' => sub {
|
||||
|
||||
subtest 'a single scalar == id', sub {
|
||||
my $serialized =
|
||||
Dancer2::Plugin::JsonApi::Registry::Schema->new( type => 'thing' )
|
||||
Dancer2::Plugin::JsonApi::Schema->new( type => 'thing' )
|
||||
->serialize('blah');
|
||||
|
||||
is $serialized->{data} => {
|
||||
@ -77,7 +77,7 @@ subtest 'a single scalar == id', sub {
|
||||
};
|
||||
|
||||
subtest 'allowed_attributes', sub {
|
||||
my $serialized = Dancer2::Plugin::JsonApi::Registry::Schema->new(
|
||||
my $serialized = Dancer2::Plugin::JsonApi::Schema->new(
|
||||
type => 'thing',
|
||||
allowed_attributes => ['foo'],
|
||||
)->serialize( { id => 1, foo => 2, bar => 3 } );
|
||||
@ -91,7 +91,7 @@ subtest 'allowed_attributes', sub {
|
||||
|
||||
subtest 'empty data', sub {
|
||||
my $serialized =
|
||||
Dancer2::Plugin::JsonApi::Registry::Schema->new( type => 'thing' )
|
||||
Dancer2::Plugin::JsonApi::Schema->new( type => 'thing' )
|
||||
->serialize(undef);
|
||||
|
||||
ok( !$serialized->{data}, "there is no data" );
|
||||
@ -113,7 +113,7 @@ package FakeApp {
|
||||
}
|
||||
|
||||
subtest "add the self link if tied to the app" => sub {
|
||||
my $serialized = Dancer2::Plugin::JsonApi::Registry::Schema->new(
|
||||
my $serialized = Dancer2::Plugin::JsonApi::Schema->new(
|
||||
type => 'thing',
|
||||
registry =>
|
||||
Dancer2::Plugin::JsonApi::Registry->new( app => FakeApp->new )
|
||||
@ -123,7 +123,7 @@ subtest "add the self link if tied to the app" => sub {
|
||||
};
|
||||
|
||||
subtest 'attributes function' => sub {
|
||||
my $serialized = Dancer2::Plugin::JsonApi::Registry::Schema->new(
|
||||
my $serialized = Dancer2::Plugin::JsonApi::Schema->new(
|
||||
type => 'thing',
|
||||
attributes => sub ( $data, @ ) {
|
||||
return +{ reverse %$data },;
|
Loading…
Reference in New Issue
Block a user