Bugzilla – Attachment 128883 Details for
Bug 29746
Add a handy Koha::Result::Boolean class
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 29746: Add Koha::Result::Boolean
Bug-29746-Add-KohaResultBoolean.patch (text/plain), 3.82 KB, created by
Tomás Cohen Arazi (tcohen)
on 2021-12-23 12:21:30 UTC
(
hide
)
Description:
Bug 29746: Add Koha::Result::Boolean
Filename:
MIME Type:
Creator:
Tomás Cohen Arazi (tcohen)
Created:
2021-12-23 12:21:30 UTC
Size:
3.82 KB
patch
obsolete
>From d44000f1d33f6bdab1271ea6e9d8f726e08101da Mon Sep 17 00:00:00 2001 >From: Tomas Cohen Arazi <tomascohen@theke.io> >Date: Tue, 21 Dec 2021 09:44:24 -0300 >Subject: [PATCH] Bug 29746: Add Koha::Result::Boolean > >This patch introduces a new OO class that can be used as return value >from methods that need to return boolean values, but also provide some >feedback. This last bit is implemented using Koha::Object::Message >objects that can carry valuable information. > >This class can also implement a `to_api()` method so it is suitable for >API usage. And so the Koha::Object::Message class. Will be done as >needed. > >If some other result types are required, then we can move some of the >messaging logic to a top-level Koha::Result class this one inherits from >(and the new one as well, say, Integer?). > >To test: >1. Apply this patchset >2. Run: > $ kshell > k$ prove t/Koha/Result/Boolean.t >=> SUCCESS: Tests pass! >3. Sign off :-D > >Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io> >Signed-off-by: David Nind <david@davidnind.com> >--- > Koha/Result/Boolean.pm | 134 +++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 134 insertions(+) > create mode 100644 Koha/Result/Boolean.pm > >diff --git a/Koha/Result/Boolean.pm b/Koha/Result/Boolean.pm >new file mode 100644 >index 0000000000..f670861766 >--- /dev/null >+++ b/Koha/Result/Boolean.pm >@@ -0,0 +1,134 @@ >+package Koha::Result::Boolean; >+ >+# Copyright ByWater Solutions 2021 >+# Copyright Theke Solutions 2021 >+# >+# This file is part of Koha. >+# >+# Koha is free software; you can redistribute it and/or modify it >+# under the terms of the GNU General Public License as published by >+# the Free Software Foundation; either version 3 of the License, or >+# (at your option) any later version. >+# >+# Koha is distributed in the hope that it will be useful, but >+# WITHOUT ANY WARRANTY; without even the implied warranty of >+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the >+# GNU General Public License for more details. >+# >+# You should have received a copy of the GNU General Public License >+# along with Koha; if not, see <http://www.gnu.org/licenses>. >+ >+use Modern::Perl; >+ >+use overload bool => \&as_bool; >+ >+use Koha::Object::Message; >+ >+=head1 NAME >+ >+Koha::Result::Boolean - Booleans, with extra Koha >+ >+=head1 API >+ >+=head2 Class methods >+ >+=head3 new >+ >+ my $bool = Koha::Result::Boolean->new( $value ); >+ >+Constructor method to generate a Koha::Result::Boolean object. I<value> is >+a boolean expression. >+ >+=cut >+ >+sub new { >+ my ( $class, $value ) = @_; >+ >+ $value //= 1; # default to true >+ $value = ($value) ? 1 : 0; >+ >+ my $self = { >+ value => $value, >+ _messages => [], >+ }; >+ >+ return bless ( $self, $class ); >+} >+ >+=head3 set_value >+ >+ $bool->set_value(1); >+ $bool->set_value(0); >+ >+Set the boolean value for the object. >+ >+=cut >+ >+sub set_value { >+ my ( $self, $value ) = @_; >+ >+ $self->{value} = ($value) ? 1 : 0; >+ >+ return $self; >+} >+ >+=head3 messages >+ >+ my @messages = @{ $bool->messages }; >+ >+Returns the I<Koha::Object::Message> objects that were recorded. >+ >+=cut >+ >+sub messages { >+ my ( $self ) = @_; >+ >+ $self->{_messages} = [] >+ unless defined $self->{_messages}; >+ >+ return $self->{_messages}; >+} >+ >+=head3 add_message >+ >+ $bool->add_message( >+ { >+ message => $message, >+ [ type => 'error', >+ payload => $payload ] >+ } >+ ); >+ >+Adds a message. >+ >+=cut >+ >+sub add_message { >+ my ( $self, $params ) = @_; >+ >+ push @{ $self->{_messages} }, Koha::Object::Message->new($params); >+ >+ return $self; >+} >+ >+=head2 Internal methods >+ >+=head3 as_bool >+ >+Internal method that exposes the boolean value of the object >+ >+=cut >+ >+sub as_bool { >+ my ($self) = @_; >+ >+ return $self->{value}; >+} >+ >+=head1 AUTHORS >+ >+Tomas Cohen Arazi, E<lt>tomascohen@theke.ioE<gt> >+ >+=cut >+ >+1; >-- >2.32.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 29746
:
128830
|
128831
|
128834
|
128835
|
128882
|
128883
|
128997
|
128998
|
129007