Class-Persist

 view release on metacpan or  search on metacpan

lib/Class/Persist/Proxy.pm  view on Meta::CPAN

=head1 NAME

Class::Persist::Proxy - Proxy for an object not loaded yet

=head1 SYNOPSIS

  use Class::Persist::Proxy;
  $proxy = Class::Persist::Proxy->new();
  $proxy->class( "AClass" );
  $proxy->owner( $owner );
  $real = $proxy->load();
  
=head1 DESCRIPTION

  Framework to replace objects in the DB by Proxy objects.
  This allows delayed loading of objects.
  A proxy acts as the real object itself, it should be transparent.
  When a method is called on the proxy, the real object is loaded in place of the proxy.
  If owner() is defined, it will autoload the object based on owner id, 
  otherwise it will load the object based on real_id.

=head1 INHERITANCE

  Class::Persist::Base

=head1 METHODS

=head2 class( $class )

=cut

package Class::Persist::Proxy;
use strict;
use warnings;
use Scalar::Util qw( blessed );
use base  qw( Class::Persist::Base );
__PACKAGE__->mk_accessors( qw(real_id) );

our $AUTOLOAD;



=head2 oid( $id )

Tries hard to return the oid of the object proxied,
if it fails, returns the proxy oid.

=cut

sub oid {
  my $self = shift;
  return $self->set($Class::Persist::ID_FIELD, shift) if @_;
  my $id = $self->real_id();
  unless ($id) {
    if ($self->get('owner')) {
      $self->load() or return;
      $id = $self->get($Class::Persist::ID_FIELD);
    }
    else {
      $id = $self->SUPER::oid();
    }
  }
  $id;
}


=head2 class( $class )

Get / set class.
If no class is given, tries to guess using Class::Persist::Tracker

=cut

sub class {
  my $self = shift;
  return $self->set('class', shift) if @_;



( run in 3.151 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )