Bug 23893: Add new_from_api and set_from_api to Koha::Object
This patch introduces the following methods to the Koha::Object class: - set_from_api - new_from_api This methods are going to be used when writing API controllers that map to the attributes to the DB schema ones. To test: 1. Apply this patchset 2. Run: $ kshell k$ prove t/db_dependent/Koha/Object.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: Josef Moravec <josef.moravec@gmail.com> Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io> Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com> Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
This commit is contained in:
parent
b9af3d71b1
commit
340f77732a
1 changed files with 58 additions and 32 deletions
|
@ -446,6 +446,64 @@ sub from_api_mapping {
|
|||
return $self->{_from_api_mapping};
|
||||
}
|
||||
|
||||
=head3 new_from_api
|
||||
|
||||
my $object = Koha::Object->new_from_api;
|
||||
my $object = Koha::Object->new_from_api( $attrs );
|
||||
|
||||
Creates a new object, mapping the API attribute names to the ones on the DB schema.
|
||||
|
||||
=cut
|
||||
|
||||
sub new_from_api {
|
||||
my ( $class, $params ) = @_;
|
||||
|
||||
my $self = $class->new;
|
||||
return $self->set_from_api( $params );
|
||||
}
|
||||
|
||||
=head3 set_from_api
|
||||
|
||||
my $object = Koha::Object->new(...);
|
||||
$object->set_from_api( $attrs )
|
||||
|
||||
Sets the object's attributes mapping API attribute names to the ones on the DB schema.
|
||||
|
||||
=cut
|
||||
|
||||
sub set_from_api {
|
||||
my ( $self, $from_api_params ) = @_;
|
||||
|
||||
return $self->set( $self->attributes_from_api( $from_api_params ) );
|
||||
}
|
||||
|
||||
=head3 attributes_from_api
|
||||
|
||||
my $attributes = attributes_from_api( $params );
|
||||
|
||||
Returns the passed params, converted from API naming into the model.
|
||||
|
||||
=cut
|
||||
|
||||
sub attributes_from_api {
|
||||
my ( $self, $from_api_params ) = @_;
|
||||
|
||||
my $from_api_mapping = $self->from_api_mapping;
|
||||
|
||||
my $params;
|
||||
|
||||
while (my ($key, $value) = each %{ $from_api_params } ) {
|
||||
if ( exists $from_api_mapping->{$key} ) {
|
||||
$params->{$from_api_mapping->{$key}} = $value;
|
||||
}
|
||||
else {
|
||||
$params->{$key} = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
=head3 $object->unblessed_all_relateds
|
||||
|
||||
my $everything_into_one_hashref = $object->unblessed_all_relateds
|
||||
|
@ -558,38 +616,6 @@ sub AUTOLOAD {
|
|||
return $r;
|
||||
}
|
||||
|
||||
=head3 attributes_from_api
|
||||
|
||||
my $attributes = attributes_from_api( $params );
|
||||
|
||||
Returns the passed params, converted from API naming into the model.
|
||||
|
||||
=cut
|
||||
|
||||
sub attributes_from_api {
|
||||
my ( $self, $attributes ) = @_;
|
||||
|
||||
my $mapping = $self->from_api_mapping;
|
||||
|
||||
foreach my $attribute ( keys %{$mapping} ) {
|
||||
my $mapped_attribute = $mapping->{$attribute};
|
||||
if ( exists $attributes->{$attribute}
|
||||
&& defined $mapped_attribute )
|
||||
{
|
||||
# key => !undef
|
||||
$attributes->{$mapped_attribute} = delete $attributes->{$attribute};
|
||||
}
|
||||
elsif ( exists $attributes->{$attribute}
|
||||
&& !defined $mapped_attribute )
|
||||
{
|
||||
# key => undef / to be deleted
|
||||
delete $attributes->{$attribute};
|
||||
}
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
=head3 _type
|
||||
|
||||
This method must be defined in the child class. The value is the name of the DBIC resultset.
|
||||
|
|
Loading…
Reference in a new issue