Bug 23770: Add Koha::Object(s)->to_api method
Generic method that changes the attribute names of an object in the presence of the to_api_mapping method. It otherwise falls back to returning the TO_JSON output. This is WIP submitted early for scrutiny. Tests and example usage in an API controller coming. Signed-off-by: Josef Moravec <josef.moravec@gmail.com> Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl> Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
This commit is contained in:
parent
d7d5a40548
commit
3c0c75e6af
|
|
@ -356,6 +356,40 @@ sub _numeric_column_type {
|
|||
return ( grep { $column_type eq $_ } @numeric_types) ? 1 : 0;
|
||||
}
|
||||
|
||||
=head3 to_api
|
||||
|
||||
my $object_for_api = $object->to_api;
|
||||
|
||||
Returns a representation of the object, suitable for API output.
|
||||
|
||||
=cut
|
||||
|
||||
sub to_api {
|
||||
my ( $self ) = @_;
|
||||
my $json_object = $self->TO_JSON;
|
||||
|
||||
# Rename attributes if there's a mapping
|
||||
if ( $self->can('to_api_mapping') ) {
|
||||
foreach my $column ( keys %{$self->to_api_mapping} ) {
|
||||
my $mapped_column = $self->to_api_mapping->{$column};
|
||||
if ( exists $json_object->{$column}
|
||||
&& defined $mapped_column )
|
||||
{
|
||||
# key != undef
|
||||
$json_object->{$mapped_column} = delete $json_object->{$column};
|
||||
}
|
||||
elsif ( exists $json_object->{$column}
|
||||
&& !defined $mapped_column )
|
||||
{
|
||||
# key == undef
|
||||
delete $json_object->{$column};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $json_object;
|
||||
}
|
||||
|
||||
=head3 $object->unblessed_all_relateds
|
||||
|
||||
my $everything_into_one_hashref = $object->unblessed_all_relateds
|
||||
|
|
|
|||
|
|
@ -308,6 +308,18 @@ sub TO_JSON {
|
|||
return [ map { $_->TO_JSON } $self->as_list ];
|
||||
}
|
||||
|
||||
=head3 Koha::Objects->to_api
|
||||
|
||||
Returns a representation of the objects, suitable for API output .
|
||||
|
||||
=cut
|
||||
|
||||
sub to_api {
|
||||
my ($self) = @_;
|
||||
|
||||
return [ map { $_->to_api } $self->as_list ];
|
||||
}
|
||||
|
||||
=head3 Koha::Objects->_wrap
|
||||
|
||||
wraps the DBIC object in a corresponding Koha object
|
||||
|
|
|
|||
Loading…
Reference in New Issue