From 53201e1b68e6227130ba20bc6f5ad695c95b75c7 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 20 Feb 2018 17:17:50 -0300 Subject: [PATCH] Bug 19966: Add Koha::Object->read_only If ->read_only is called, only accessor methods will be allowed on this object. use Koha::Patrons; my $p = Koha::Patrons->find(1); say $p->borrowernumber; $p->surname('another surname')->store; say $p->surname; => Will work $p = Koha::Patrons->find(1); $p->read_only; $p->surname('another surname again')->store; => Will explode Problem: use Koha::Patrons; my $p = Koha::Patrons->find(1); say $p->borrowernumber; $p->read_only; $p->{_read_only} = 0; $p->surname('another surname again')->store; => Will not explode --- Koha/Object.pm | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Koha/Object.pm b/Koha/Object.pm index d53654b15c..226bebe277 100644 --- a/Koha/Object.pm +++ b/Koha/Object.pm @@ -222,6 +222,20 @@ sub unblessed { return { $self->_result->get_columns }; } +=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; +} + =head3 $object->TO_JSON Returns an unblessed representation of the object, suitable for JSON output. @@ -344,6 +358,8 @@ sub AUTOLOAD { # Using direct setter/getter like $item->barcode() or $item->barcode($barcode); if ( grep {/^$method$/} @columns ) { if ( @_ ) { + die "Setter method $method called on read-only object" + if $self->{_read_only}; $self->_result()->set_column( $method, @_ ); return $self; } else { @@ -352,6 +368,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 update ); Koha::Exceptions::Object::MethodNotCoveredByTests->throw( "The method $method is not covered by tests!" ) unless grep {/^$method$/} @known_methods; -- 2.11.0