our $AUTOLOAD;
#print "Storable::AUTOLOAD : attempting to run '$AUTOLOAD' (".join(', ', @_).")\n";
my $sub;
if($AUTOLOAD =~ /::(\w+)$/) {
my $name_to_parse = $1;
my ($AdaptorType, $is_an_id, $foo_id_method_name, $foo_obj_method_name)
unless($AdaptorType) {
throw("Storable::AUTOLOAD : could not parse '$name_to_parse'");
} elsif ($is_an_id) { # $name_to_parse was something like foo_dataflow_rule_id
$sub = sub {
my $self = shift @_;
if(@_) {
$self->{$foo_id_method_name} = shift @_;
$self->{$foo_obj_method_name} = undef; # invalidate the object itself
# attempt to lazy-load:
} elsif( !$self->{$foo_id_method_name} and my $foo_object=$self->{$foo_obj_method_name}) {
$self->{$foo_id_method_name} = $foo_object->dbID;
# warn "Lazy-loaded dbID (".$self->{$foo_id_method_name}.") from $AdaptorType object\n";
}
return $self->{$foo_id_method_name};
};
} else { # $name_to_parse was something like foo_dataflow_rule
$sub = sub {
my $self = shift @_;
if(@_) { # setter of the object itself
$self->{$foo_obj_method_name} = shift @_;
$self->{$foo_id_method_name} = undef; # invalidate the object_id
# attempt to lazy-load:
} elsif( !$self->{$foo_obj_method_name} and my $foo_object_id = $self->{$foo_id_method_name}) {
my $collection = $self->hive_pipeline && $self->hive_pipeline->collection_of($AdaptorType);
if( $collection and $self->{$foo_obj_method_name} = $collection->find_one_by('dbID', $foo_object_id) ) { # careful: $AdaptorType may not be unique (aliases)
weaken($self->{$foo_obj_method_name});
# warn "Lazy-loading object from $AdaptorType collection\n";
} elsif(my $adaptor = $self->adaptor) {
# warn "Lazy-loading object from $AdaptorType adaptor\n";
$self->{$foo_obj_method_name} = $adaptor->db->get_adaptor( $AdaptorType )->fetch_by_dbID( $foo_object_id );
} else {
# warn "Cannot lazy-load $foo_obj_method_name because the ".ref($self)." is not attached to an adaptor";
}
}
return $self->{$foo_obj_method_name};
};
} # choice of autoloadable functions
{
no strict 'refs'; ## no critic ProhibitNoStrict
{$AUTOLOAD} = $sub;
goto &$sub;
}
}
}