From 05fd6eae35d95a670dce9d042477bddb39974111 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 23 Jul 2021 08:14:10 -0400 Subject: [PATCH] [FOR DISCUSSION] Bug 28739: Add Koha::Object->read_only --- Koha/Object.pm | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/Koha/Object.pm b/Koha/Object.pm index 8e5892ceec..7ffd8ff2ed 100644 --- a/Koha/Object.pm +++ b/Koha/Object.pm @@ -378,6 +378,18 @@ sub add_message { my ( $self, $params ) = @_; push @{ $self->{_messages} }, Koha::Object::Message->new($params); +} + +=head3 $object->read_only(); + +Set the object as read_only, only getter methods will be allowed + +=cut + +sub read_only { + my ($self) = @_; + + $self->{_read_only} = 1; return $self; } @@ -814,6 +826,8 @@ sub AUTOLOAD { # Using direct setter/getter like $item->barcode() or $item->barcode($barcode); if ( grep { $_ eq $method } @columns ) { if ( @_ ) { + die "Setter method $method called on read-only object" + if $self->{_read_only}; $self->_result()->set_column( $method, @_ ); return $self; } else { @@ -822,6 +836,9 @@ sub AUTOLOAD { } } + die "Cannot call method $method on a read-only object" + if $self->{_read_only}; + my @known_methods = qw( is_changed id in_storage get_column discard_changes make_column_dirty ); Koha::Exceptions::Object::MethodNotCoveredByTests->throw( @@ -829,7 +846,6 @@ sub AUTOLOAD { show_trace => 1 ) unless grep { $_ eq $method } @known_methods; - if ( $method eq 'update' ) { return $self if ( ( caller() )[0] eq 'Template::Document' ); } -- 2.30.1 (Apple Git-130)