From 03a6299985bd0ada69f7294e9e7f5242193f70eb Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 31 Oct 2024 16:34:01 +0000 Subject: [PATCH] Bug 30657: Move to AUTOLOAD --- Koha/Patron.pm | 57 +++++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 9072a7b186c..3e56e8911da 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -108,43 +108,35 @@ Koha::Patron - Koha Patron Object class =head2 Class Methods -=head3 new, _new_from_dbic, _add_dynamic_methods +=head3 new -These methods are used internally. They dynamically add accessors to Koha::Patron objects resulting from -calls to ->new, ->find and ->search for any defined extended patron attributes. Said accessors will be named -after the 'code' of the attribute type and will return either the single value or an array of values for -attribute types that are repeatable. +Our methods below include an AUTOLOAD that will dynamically create accessor methods for any +defined extended patron attributes. -=cut - -sub new { - my ( $class, $params ) = @_; - - my $self = $class->next::method($params); - - $self->_add_dynamic_methods; +Said accessors will be named after the 'code' of the attribute type and will return either +the single value or an array of values for attribute types that are repeatable. - return $self; -} - -sub _new_from_dbic { - my $self = shift; - my ( $source, $data, $prefetch ) = @_; - - my $obj = $self->next::method(@_); +=cut - $obj->_add_dynamic_methods; +our $AUTOLOAD; - return $obj; -} +# This autoload generates a method for each extended attribute type defined in the system upon the first call +# for a method matching an attribute type name. +# +# TODO: This could be abstracted in the future as a Mixin for extended_attribute, ill_attributes and additional_fields -sub _add_dynamic_methods { - my $self = shift; +sub AUTOLOAD { + my @args = @_; + my $self = shift(@args); + my $name = $AUTOLOAD; + $name =~ s/.*:://; # Remove package name + my $found = 0; # Fetch attributes dynamically my $attribute_types = Koha::Patron::Attribute::Types->search(); while ( my $attr = $attribute_types->next ) { my $code = $attr->code; + $found = 1 if ( $code eq $name ); # Dynamically create accessor method no strict 'refs'; @@ -170,6 +162,19 @@ sub _add_dynamic_methods { } } } + + if ($found) { + return $self->$name(); + } else { + my $wt = 'SUPER::' . $name; + return $self->$wt(@args); + } +} + +sub new { + my ( $class, $params ) = @_; + + return $class->SUPER::new($params); } =head3 fixup_cardnumber -- 2.47.0