Bug 17091: Add AUTOLOAD to Koha::Objects
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Tue, 9 Aug 2016 14:46:11 +0000 (15:46 +0100)
committerBrendan Gallagher <brendan@bywatersolutions.com>
Tue, 11 Oct 2016 03:43:15 +0000 (03:43 +0000)
Up to now if a Koha::Objects based object needs to call DBIx::Class methods, we
have to create a new method for Koha::Objects, something like:
  sub method {
    my $self = shift;
    return $self->_resultset->method
  }

To simplify and ease the call to DBIx::Class method, this patch defines an
AUTOLOAD to call the DBIx::Class method on the resultset we are encapsulating.

Signed-off-by: Hector Castro <hector.hecaxmmx@gmail.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Brendan Gallagher <brendan@bywatersolutions.com>
Koha/Objects.pm

index 97436ad..03f39dc 100644 (file)
@@ -280,7 +280,29 @@ sub columns {
     return Koha::Database->new->schema->resultset( $class->_type )->result_source->columns;
 }
 
+=head3 AUTOLOAD
 
+The autoload method is used call DBIx::Class method on a resultset.
+
+Important: If you plan to use one of the DBIx::Class methods you must provide
+relevant tests in t/db_dependent/Koha/Objects.t
+Currently count, pager, reset and update are covered.
+
+=cut
+
+sub AUTOLOAD {
+    my ( $self, @params ) = @_;
+
+    my $method = our $AUTOLOAD;
+    $method =~ s/.*:://;
+
+    my $r = eval { $self->_resultset->$method(@params) };
+    if ( $@ ) {
+        carp "No method $method found for " . ref($self) . " " . $@;
+        return
+    }
+    return $r;
+}
 
 =head3 _type