Bug 17441 - t/db_dependent/Letters.t fails on Jenkins
[koha.git] / Koha / Object.pm
blobcf55089c06da6a6518cc2726adbe9312d18f74ad
1 package Koha::Object;
3 # Copyright ByWater Solutions 2014
4 # Copyright 2016 Koha Development Team
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 3 of the License, or (at your option) any later
11 # version.
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 use Modern::Perl;
23 use Carp;
25 use Koha::Database;
27 =head1 NAME
29 Koha::Object - Koha Object base class
31 =head1 SYNOPSIS
33 use Koha::Object;
34 my $object = Koha::Object->new({ property1 => $property1, property2 => $property2, etc... } );
36 =head1 DESCRIPTION
38 This class must always be subclassed.
40 =head1 API
42 =head2 Class Methods
44 =cut
46 =head3 Koha::Object->new();
48 my $object = Koha::Object->new();
49 my $object = Koha::Object->new($attributes);
51 Note that this cannot be used to retrieve record from the DB.
53 =cut
55 sub new {
56 my ( $class, $attributes ) = @_;
57 my $self = {};
59 if ($attributes) {
60 $self->{_result} =
61 Koha::Database->new()->schema()->resultset( $class->_type() )
62 ->new($attributes);
65 croak("No _type found! Koha::Object must be subclassed!")
66 unless $class->_type();
68 bless( $self, $class );
72 =head3 Koha::Object->_new_from_dbic();
74 my $object = Koha::Object->_new_from_dbic($dbic_row);
76 =cut
78 sub _new_from_dbic {
79 my ( $class, $dbic_row ) = @_;
80 my $self = {};
82 # DBIC result row
83 $self->{_result} = $dbic_row;
85 croak("No _type found! Koha::Object must be subclassed!")
86 unless $class->_type();
88 croak( "DBIC result _type " . ref( $self->{_result} ) . " isn't of the _type " . $class->_type() )
89 unless ref( $self->{_result} ) eq "Koha::Schema::Result::" . $class->_type();
91 bless( $self, $class );
95 =head3 $object->store();
97 Saves the object in storage.
98 If the object is new, it will be created.
99 If the object previously existed, it will be updated.
101 Returns:
102 $self if the store was a success
103 undef if the store failed
105 =cut
107 sub store {
108 my ($self) = @_;
110 return $self->_result()->update_or_insert() ? $self : undef;
113 =head3 $object->delete();
115 Removes the object from storage.
117 Returns:
118 1 if the deletion was a success
119 0 if the deletion failed
120 -1 if the object was never in storage
122 =cut
124 sub delete {
125 my ($self) = @_;
127 # Deleting something not in storage thows an exception
128 return -1 unless $self->_result()->in_storage();
130 # Return a boolean for succcess
131 return $self->_result()->delete() ? 1 : 0;
134 =head3 $object->set( $properties_hashref )
136 $object->set(
138 property1 => $property1,
139 property2 => $property2,
140 property3 => $propery3,
144 Enables multiple properties to be set at once
146 Returns:
147 1 if all properties were set.
148 0 if one or more properties do not exist.
149 undef if all properties exist but a different error
150 prevents one or more properties from being set.
152 If one or more of the properties do not exist,
153 no properties will be set.
155 =cut
157 sub set {
158 my ( $self, $properties ) = @_;
160 my @columns = @{$self->_columns()};
162 foreach my $p ( keys %$properties ) {
163 unless ( grep {/^$p$/} @columns ) {
164 carp("No property $p!");
165 return 0;
169 return $self->_result()->set_columns($properties) ? $self : undef;
172 =head3 $object->unblessed();
174 Returns an unblessed representation of object.
176 =cut
178 sub unblessed {
179 my ($self) = @_;
181 return { $self->_result->get_columns };
184 =head3 $object->_result();
186 Returns the internal DBIC Row object
188 =cut
190 sub _result {
191 my ($self) = @_;
193 # If we don't have a dbic row at this point, we need to create an empty one
194 $self->{_result} ||=
195 Koha::Database->new()->schema()->resultset( $self->_type() )->new({});
197 return $self->{_result};
200 =head3 $object->_columns();
202 Returns an arrayref of the table columns
204 =cut
206 sub _columns {
207 my ($self) = @_;
209 # If we don't have a dbic row at this point, we need to create an empty one
210 $self->{_columns} ||= [ $self->_result()->result_source()->columns() ];
212 return $self->{_columns};
216 =head3 AUTOLOAD
218 The autoload method is used only to get and set values for an objects properties.
220 =cut
222 sub AUTOLOAD {
223 my $self = shift;
225 my $method = our $AUTOLOAD;
226 $method =~ s/.*://;
228 my @columns = @{$self->_columns()};
229 # Using direct setter/getter like $item->barcode() or $item->barcode($barcode);
230 if ( grep {/^$method$/} @columns ) {
231 if ( @_ ) {
232 $self->_result()->set_column( $method, @_ );
233 return $self;
234 } else {
235 my $value = $self->_result()->get_column( $method );
236 return $value;
240 my @known_methods = qw( is_changed id in_storage get_column );
242 carp "The method $method is not covered by tests or does not exist!" and return unless grep {/^$method$/} @known_methods;
244 my $r = eval { $self->_result->$method(@_) };
245 if ( $@ ) {
246 carp "No method $method found for " . ref($self) . " " . $@;
247 return
249 return $r;
252 =head3 _type
254 This method must be defined in the child class. The value is the name of the DBIC resultset.
255 For example, for borrowers, the _type method will return "Borrower".
257 =cut
259 sub _type { }
261 sub DESTROY { }
263 =head1 AUTHOR
265 Kyle M Hall <kyle@bywatersolutions.com>
267 Jonathan Druart <jonathan.druart@bugs.koha-community.org>
269 =cut