attributes can be a function
This commit is contained in:
parent
0a2eee55ce
commit
e93942cb61
@ -90,6 +90,20 @@ Serializes the inner C<$data>.
|
|||||||
|
|
||||||
=cut
|
=cut
|
||||||
|
|
||||||
|
has attributes => (
|
||||||
|
is => 'ro',
|
||||||
|
default => sub {
|
||||||
|
my $self = shift;
|
||||||
|
return sub {
|
||||||
|
my ( $data, $extra_data ) = @_;
|
||||||
|
return {} if ref $data ne 'HASH';
|
||||||
|
my @keys = grep { not $self->relationships->{$_} }
|
||||||
|
grep { $_ ne $self->id } keys %$data;
|
||||||
|
return { $data->%{@keys} };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
sub serialize_data ( $self, $data, $extra_data = {}, $included = undef ) {
|
sub serialize_data ( $self, $data, $extra_data = {}, $included = undef ) {
|
||||||
|
|
||||||
return [ map { $self->serialize_data( $_, $extra_data, $included ) }
|
return [ map { $self->serialize_data( $_, $extra_data, $included ) }
|
||||||
@ -108,12 +122,12 @@ sub serialize_data ( $self, $data, $extra_data = {}, $included = undef ) {
|
|||||||
$s->{links} = gen_links( $self->links, $data, $extra_data );
|
$s->{links} = gen_links( $self->links, $data, $extra_data );
|
||||||
}
|
}
|
||||||
|
|
||||||
$s->{attributes} = +{ pairgrep { $a ne $self->id } %$data };
|
$s->{attributes} = gen_links( $self->attributes, $data, $extra_data );
|
||||||
|
|
||||||
my %relationships = $self->relationships->%*;
|
my %relationships = $self->relationships->%*;
|
||||||
|
|
||||||
for my $key ( keys %relationships ) {
|
for my $key ( keys %relationships ) {
|
||||||
my $attr = delete $s->{attributes}{$key} or next;
|
my $attr = $data->{$key};
|
||||||
|
|
||||||
my @inc;
|
my @inc;
|
||||||
|
|
||||||
|
@ -120,6 +120,17 @@ subtest "add the self link if tied to the app" => sub {
|
|||||||
)->serialize(undef);
|
)->serialize(undef);
|
||||||
|
|
||||||
is $serialized->{links}{self} => '/some/path';
|
is $serialized->{links}{self} => '/some/path';
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
subtest 'attributes function' => sub {
|
||||||
|
my $serialized = Dancer2::Plugin::JsonApi::Registry::Schema->new(
|
||||||
|
type => 'thing',
|
||||||
|
attributes => sub ( $data, @ ) {
|
||||||
|
return +{ reverse %$data },;
|
||||||
|
},
|
||||||
|
)->serialize( { id => 1, 'a' .. 'd' } );
|
||||||
|
|
||||||
|
is $serialized->{data}{attributes} => { 1 => 'id', b => 'a', d => 'c' };
|
||||||
|
};
|
||||||
|
|
||||||
done_testing();
|
done_testing();
|
||||||
|
Loading…
Reference in New Issue
Block a user