From ea71a63a767df2212cc2d4d25f22178df5ef9db1 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::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. To test: 1. Apply this patchset 2. Run: $ kshell k$ prove t/Koha/Boolean.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: Tomas Cohen Arazi Signed-off-by: David Nind --- Koha/Boolean.pm | 134 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 Koha/Boolean.pm diff --git a/Koha/Boolean.pm b/Koha/Boolean.pm new file mode 100644 index 0000000000..865d5e0078 --- /dev/null +++ b/Koha/Boolean.pm @@ -0,0 +1,134 @@ +package Koha::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::Boolean - Booleans, with extra Koha + +=head1 API + +=head2 Class methods + +=head3 new + + my $bool = Koha::Boolean->new( $value ); + +Constructor method to generate a Koha::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.20.1