Better linking in Relatiomship POD
[dbsrgits/DBIx-Class-Historic.git] / lib / DBIx / Class / Relationship / ProxyMethods.pm
CommitLineData
75d07914 1package # hide from PAUSE
c0e7b4e5 2 DBIx::Class::Relationship::ProxyMethods;
b28cc0ba 3
4use strict;
5use warnings;
ddc0a6c8 6use Sub::Name ();
1edd1722 7use base qw/DBIx::Class/;
b28cc0ba 8
71e65b39 9sub register_relationship {
10 my ($class, $rel, $info) = @_;
11 if (my $proxy_list = $info->{attrs}{proxy}) {
223b8fe3 12 $class->proxy_to_related($rel,
13 (ref $proxy_list ? @$proxy_list : $proxy_list));
b28cc0ba 14 }
71e65b39 15 $class->next::method($rel, $info);
b28cc0ba 16}
17
223b8fe3 18sub proxy_to_related {
19 my ($class, $rel, @proxy) = @_;
20 no strict 'refs';
21 no warnings 'redefine';
22 foreach my $proxy (@proxy) {
ddc0a6c8 23 my $name = join '::', $class, $proxy;
24 *$name = Sub::Name::subname $name,
223b8fe3 25 sub {
26 my $self = shift;
27 my $val = $self->$rel;
28 if (@_ && !defined $val) {
29 $val = $self->create_related($rel, { $proxy => $_[0] });
30 @_ = ();
31 }
32 return ($val ? $val->$proxy(@_) : undef);
33 }
34 }
35}
36
b28cc0ba 371;