Commit | Line | Data |
75d07914 |
1 | package # hide from PAUSE |
c0e7b4e5 |
2 | DBIx::Class::Relationship::ProxyMethods; |
b28cc0ba |
3 | |
4 | use strict; |
5 | use warnings; |
ddc0a6c8 |
6 | use Sub::Name (); |
1edd1722 |
7 | use base qw/DBIx::Class/; |
b28cc0ba |
8 | |
71e65b39 |
9 | sub 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 |
18 | sub 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 |
37 | 1; |