Bugzilla – Attachment 96128 Details for
Bug 23893
Add ->new_from_api and ->set_from_api methods to Koha::Object
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 23893: Add new_from_api and set_from_api to Koha::Object
Bug-23893-Add-newfromapi-and-setfromapi-to-KohaObj.patch (text/plain), 2.90 KB, created by
Josef Moravec
on 2019-12-10 06:33:20 UTC
(
hide
)
Description:
Bug 23893: Add new_from_api and set_from_api to Koha::Object
Filename:
MIME Type:
Creator:
Josef Moravec
Created:
2019-12-10 06:33:20 UTC
Size:
2.90 KB
patch
obsolete
>From 67fc3fc8c717bbc23387b9996994ea844cfc40b3 Mon Sep 17 00:00:00 2001 >From: Tomas Cohen Arazi <tomascohen@theke.io> >Date: Mon, 11 Nov 2019 16:07:03 -0300 >Subject: [PATCH] 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> >--- > Koha/Object.pm | 56 +++++++++++++++++++++++++++++++++++++++++--------------- > 1 file changed, 41 insertions(+), 15 deletions(-) > >diff --git a/Koha/Object.pm b/Koha/Object.pm >index 79dc177f78..7aceeba9bf 100644 >--- a/Koha/Object.pm >+++ b/Koha/Object.pm >@@ -434,6 +434,37 @@ 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 $object->unblessed_all_relateds > > my $everything_into_one_hashref = $object->unblessed_all_relateds >@@ -555,27 +586,22 @@ Returns the passed params, converted from API naming into the model. > =cut > > sub attributes_from_api { >- my ( $self, $attributes ) = @_; >+ my ( $self, $from_api_params ) = @_; > >- my $mapping = $self->from_api_mapping; >+ my $from_api_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}; >+ my $params; >+ >+ while (my ($key, $value) = each %{ $from_api_params } ) { >+ if ( exists $from_api_mapping->{$key} ) { >+ $params->{$from_api_mapping->{$key}} = $value; > } >- elsif ( exists $attributes->{$attribute} >- && !defined $mapped_attribute ) >- { >- # key => undef / to be deleted >- delete $attributes->{$attribute}; >+ else { >+ $params->{$key} = $value; > } > } > >- return $attributes; >+ return $params; > } > > =head3 _type >-- >2.11.0
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 23893
:
95276
|
95277
|
95278
|
95311
|
95312
|
95313
|
95314
|
95315
|
96126
|
96127
|
96128
|
96129
|
96130
|
96652
|
96655
|
96656
|
96657
|
96658
|
96659
|
96660
|
96661
|
96662
|
96665
|
96666
|
96920
|
96921
|
96922
|
96923
|
96924
|
96925
|
96926
|
96927
|
96928
|
96929
|
96930
|
96931
|
96932
|
96933
|
96934
|
96935
|
96936
|
96937
|
97391