Bugzilla – Attachment 96656 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: Implement Koha::Object->from_api_mapping
Bug-23893-Implement-KohaObject-fromapimapping.patch (text/plain), 4.35 KB, created by
Tomás Cohen Arazi (tcohen)
on 2019-12-27 15:22:02 UTC
(
hide
)
Description:
Bug 23893: Implement Koha::Object->from_api_mapping
Filename:
MIME Type:
Creator:
Tomás Cohen Arazi (tcohen)
Created:
2019-12-27 15:22:02 UTC
Size:
4.35 KB
patch
obsolete
>From 45ec381ea35e304775e058be429a75dd8cd9918d Mon Sep 17 00:00:00 2001 >From: Tomas Cohen Arazi <tomascohen@theke.io> >Date: Mon, 11 Nov 2019 15:03:28 -0300 >Subject: [PATCH] Bug 23893: Implement Koha::Object->from_api_mapping > >This patch implements the from_api_mapping method, that calculates (and >caches) the reserve mapping from the to_api_mapping method. > >A generic to_api_mapping is added to simplify things a bit in the generic >to_api method and so the mappings are reusable in the way they are in >from_api_mapping. > >To test: >1. Apply this patches >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> >--- > Koha/Object.pm | 102 +++++++++++++++++++++++++++++++++++++++++-------- > 1 file changed, 87 insertions(+), 15 deletions(-) > >diff --git a/Koha/Object.pm b/Koha/Object.pm >index ce4c231591..215e9dcf92 100644 >--- a/Koha/Object.pm >+++ b/Koha/Object.pm >@@ -383,28 +383,68 @@ sub to_api { > my ( $self ) = @_; > my $json_object = $self->TO_JSON; > >+ my $to_api_mapping = $self->to_api_mapping; >+ > # 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}; >- } >+ foreach my $column ( keys %{$to_api_mapping} ) { >+ my $mapped_column = $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 to_api_mapping >+ >+ my $mapping = $object->to_api_mapping; >+ >+Generic method that returns the attribute name mappings required to >+render the object on the API. >+ >+Note: this only returns an empty I<hashref>. Each class should have its >+own mapping returned. >+ >+=cut >+ >+sub to_api_mapping { >+ return {}; >+} >+ >+=head3 from_api_mapping >+ >+ my $mapping = $object->from_api_mapping; >+ >+Generic method that returns the attribute name mappings so the data that >+comes from the API is correctly renamed to match what is required for the DB. >+ >+=cut >+ >+sub from_api_mapping { >+ my ( $self ) = @_; >+ >+ my $to_api_mapping = $self->to_api_mapping; >+ >+ unless ( $self->{_from_api_mapping} ) { >+ while (my ($key, $value) = each %{ $to_api_mapping } ) { >+ $self->{_from_api_mapping}->{$value} = $key >+ if defined $value; >+ } >+ } >+ >+ return $self->{_from_api_mapping}; >+} >+ > =head3 $object->unblessed_all_relateds > > my $everything_into_one_hashref = $object->unblessed_all_relateds >@@ -517,6 +557,38 @@ 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. >-- >2.24.1
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