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