| 1 | =head1 NAME | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | Class::Persist::Proxy - Proxy for an object not loaded yet | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | use Class::Persist::Proxy; | ||||||||||||||||||||||||||||
| 8 | $proxy = Class::Persist::Proxy->new(); | ||||||||||||||||||||||||||||
| 9 | $proxy->class( "AClass" ); | ||||||||||||||||||||||||||||
| 10 | $proxy->owner( $owner ); | ||||||||||||||||||||||||||||
| 11 | $real = $proxy->load(); | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | Framework to replace objects in the DB by Proxy objects. | ||||||||||||||||||||||||||||
| 16 | This allows delayed loading of objects. | ||||||||||||||||||||||||||||
| 17 | A proxy acts as the real object itself, it should be transparent. | ||||||||||||||||||||||||||||
| 18 | When a method is called on the proxy, the real object is loaded in place of the proxy. | ||||||||||||||||||||||||||||
| 19 | If owner() is defined, it will autoload the object based on owner id, | ||||||||||||||||||||||||||||
| 20 | otherwise it will load the object based on real_id. | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head1 INHERITANCE | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | Class::Persist::Base | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 METHODS | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head2 class( $class ) | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =cut | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | package Class::Persist::Proxy; | ||||||||||||||||||||||||||||
| 33 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 1 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 1 | ▶ | use base qw( Class::Persist::Base ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 1 | __PACKAGE__->mk_accessors( qw(real_id) ); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 1 | our $AUTOLOAD; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =head2 oid( $id ) | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | Tries hard to return the oid of the object proxied, | ||||||||||||||||||||||||||||
| 46 | if it fails, returns the proxy oid. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =cut | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | sub oid { | ||||||||||||||||||||||||||
|
Subroutine
oid: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 52 | 0 | ▶ | return $self->set($Class::Persist::ID_FIELD, shift) if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | my $id = $self->real_id(); | |||||||||||||||||||||||||||
| 54 | 0 | ▶ | unless ($id) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | if ($self->get('owner')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | $self->load() or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | $id = $self->get($Class::Persist::ID_FIELD); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | else { | ||||||||||||||||||||||||||||
| 60 | 0 | $id = $self->SUPER::oid(); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 0 | $id; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =head2 class( $class ) | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | Get / set class. | ||||||||||||||||||||||||||||
| 70 | If no class is given, tries to guess using Class::Persist::Tracker | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =cut | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | sub class { | ||||||||||||||||||||||||||
|
Subroutine
class: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 76 | 0 | ▶ | return $self->set('class', shift) if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | unless ($self->get('class')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | require Class::Persist::Tracker; | |||||||||||||||||||||||||||
| 80 | 0 | ▶ | my $tracker = Class::Persist::Tracker->load($self->oid) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | or return $self->record('Class::Persist::Error::DB::NotFound', "object " . $self->oid . " not found", 1); | ||||||||||||||||||||||||||||
| 82 | 0 | $self->set('class', $tracker->class); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 0 | return $self->get('class'); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =head2 owner( $obj ) | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | Get / set owner. | ||||||||||||||||||||||||||||
| 90 | The owner is automatically proxied. | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =cut | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | sub owner { | ||||||||||||||||||||||||||
|
Subroutine
owner: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 96 | 0 | ▶ | if (my ($owner) = @_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | blessed($owner) or Class::Persist::Error::InvalidParameters->throw(text => "owner should be an object"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | unless ($owner->isa('Class::Persist::Proxy')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | my $proxy = Class::Persist::Proxy->new(); | |||||||||||||||||||||||||||
| 100 | 0 | $proxy->class( ref $owner ); | |||||||||||||||||||||||||||
| 101 | 0 | $proxy->oid( $owner->oid ); | |||||||||||||||||||||||||||
| 102 | 0 | $owner = $proxy; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 0 | return $self->set('owner', $owner); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | 0 | ▶ | return $self->get('owner') || $self->load->owner; | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->get('owner') || $self->load->owner
MC/DC0%0/2
$self->get('owner') || $self->load->owner
$self->get('owner')
$self->load->owner
Truth table0%0/3
A || B
A= $self->get('owner')B= $self->load->owner
| |||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head2 load() | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | Replace the proxy by its target object | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =cut | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | sub load { | ||||||||||||||||||||||||||
|
Subroutine
load: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 118 | 0 | ▶ | my $class = $self->class or return $self->record('Class::Persist::Error::InvalidParameters', "A class should be defined in proxy", 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 0 | ▶ | $self->loadModule( $class ) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | my $obj = $class->load($self->get('owner') || $self->oid); | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->get('owner') || $self->oid
MC/DC0%0/2
$self->get('owner') || $self->oid
$self->get('owner')
$self->oid
Truth table0%0/3
A || B
A= $self->get('owner')B= $self->oid
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | unless ($obj) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 0 | require Class::Persist::Tracker; | |||||||||||||||||||||||||||
| 125 | 0 | ▶ | if (my $tracker = Class::Persist::Tracker->load($self->get('owner') || $self->oid)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->get('owner') || $self->oid
MC/DC0%0/2
$self->get('owner') || $self->oid
$self->get('owner')
$self->oid
Truth table0%0/3
A || B
A= $self->get('owner')B= $self->oid
| |||||||||||||||||||||||||||||
| 126 | 0 | ▶ | if (my $object = $tracker->object) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 0 | $self->_duplicate_from($object); | |||||||||||||||||||||||||||
| 128 | 0 | bless $self => ref($object); | |||||||||||||||||||||||||||
| 129 | 0 | return $self; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 0 | return $self->record('Class::Persist::Error::DB::NotFound', "Could not load $class for $self", 1) | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | $self->_duplicate_from( $obj ); | |||||||||||||||||||||||||||
| 136 | 0 | bless $self => $class; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =head2 proxy( $obj ) | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Replace object by proxy | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =cut | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 0 | ▶ | sub proxy { | ||||||||||||||||||||||||||
|
Subroutine
proxy: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 147 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 148 | 0 | my $obj = shift; | |||||||||||||||||||||||||||
| 149 | 0 | my $owner = shift; | |||||||||||||||||||||||||||
| 150 | 0 | ▶ | return $obj if $obj->isa(__PACKAGE__); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | $obj->isa('Class::Persist') or Class::Persist::Error::InvalidParameters->throw(text => "object to proxy should be a Class::Persist"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | ▶ | $class->loadModule( ref $obj ) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 0 | my $self = $class->new(); | |||||||||||||||||||||||||||
| 155 | 0 | $self->class( ref $obj ); | |||||||||||||||||||||||||||
| 156 | 0 | ▶ | if ($owner) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 0 | $self->owner( $owner ); | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | 0 | $self->real_id( $obj->oid ); | |||||||||||||||||||||||||||
| 160 | 0 | $obj->_duplicate_from( $self ); | |||||||||||||||||||||||||||
| 161 | 0 | bless $obj => $class; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 166 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 167 | 0 | ▶ | $self = $self->load() or return; # die "Can't find in DB from ".(caller)[0]." line ".(caller)[2]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 0 | my $meth = substr($AUTOLOAD, rindex($AUTOLOAD, ':') + 1); | |||||||||||||||||||||||||||
| 169 | 0 | ▶ | my $can = $self->can($meth) or EO::Error::Method::NotFound->throw(text => "Method $meth unknownin class ".ref($self)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 0 | $can->($self, @_); | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 0 | ▶ | sub DESTROY { 1 } | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 0 | ▶ | sub clone { | ||||||||||||||||||||||||||
|
Subroutine
clone: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 176 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 177 | 0 | ▶ | $self = $self->load or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 0 | return $self->clone(@_); | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 1 | 1; | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | Class::Persist | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | Fotango | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =cut | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | # Local Variables: | ||||||||||||||||||||||||||||
| 194 | # mode:CPerl | ||||||||||||||||||||||||||||
| 195 | # cperl-indent-level: 2 | ||||||||||||||||||||||||||||
| 196 | # indent-tabs-mode: nil | ||||||||||||||||||||||||||||
| 197 | # End: | ||||||||||||||||||||||||||||