From b255bb3a8b5b118699e49b4e3921aed98853879e Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi 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 Signed-off-by: David Nind Signed-off-by: Jonathan Druart --- 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 00000000000..f670861766d --- /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 . + +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 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 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, Etomascohen@theke.ioE + +=cut + +1; -- 2.25.1