Replace many closure-based proxy methods with static qsubs
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Relationship / Accessor.pm
1 package # hide from PAUSE
2     DBIx::Class::Relationship::Accessor;
3
4 use strict;
5 use warnings;
6 use DBIx::Class::Carp;
7 use DBIx::Class::_Util qw(quote_sub perlstring);
8 use namespace::clean;
9
10 our %_pod_inherit_config =
11   (
12    class_map => { 'DBIx::Class::Relationship::Accessor' => 'DBIx::Class::Relationship' }
13   );
14
15 sub register_relationship {
16   my ($class, $rel, $info) = @_;
17   if (my $acc_type = $info->{attrs}{accessor}) {
18     $class->add_relationship_accessor($rel => $acc_type);
19   }
20   $class->next::method($rel => $info);
21 }
22
23 sub add_relationship_accessor {
24   my ($class, $rel, $acc_type) = @_;
25
26   if ($acc_type eq 'single') {
27     quote_sub "${class}::${rel}" => sprintf(<<'EOC', perlstring $rel);
28       my $self = shift;
29
30       if (@_) {
31         $self->set_from_related( %1$s => @_ );
32         return $self->{_relationship_data}{%1$s} = $_[0];
33       }
34       elsif (exists $self->{_relationship_data}{%1$s}) {
35         return $self->{_relationship_data}{%1$s};
36       }
37       else {
38         my $rel_info = $self->result_source->relationship_info(%1$s);
39         my $cond = $self->result_source->_resolve_condition(
40           $rel_info->{cond}, %1$s, $self, %1$s
41         );
42         if ($rel_info->{attrs}->{undef_on_null_fk}){
43           return undef unless ref($cond) eq 'HASH';
44           return undef if grep { not defined $_ } values %%$cond;
45         }
46         my $val = $self->find_related( %1$s => {} );
47         return $val unless $val;  # $val instead of undef so that null-objects can go through
48
49         return $self->{_relationship_data}{%1$s} = $val;
50       }
51 EOC
52   }
53   elsif ($acc_type eq 'filter') {
54     $class->throw_exception("No such column '$rel' to filter")
55        unless $class->has_column($rel);
56
57     my $f_class = $class->relationship_info($rel)->{class};
58
59     $class->inflate_column($rel, {
60       inflate => sub {
61         my ($val, $self) = @_;
62         return $self->find_or_new_related($rel, {}, {});
63       },
64       deflate => sub {
65         my ($val, $self) = @_;
66         $self->throw_exception("'$val' isn't a $f_class") unless $val->isa($f_class);
67
68         # MASSIVE FIXME - this code assumes we pointed at the PK, but the belongs_to
69         # helper does not check any of this
70         # fixup the code a bit to make things saner, but ideally 'filter' needs to
71         # be deprecated ASAP and removed shortly after
72         # Not doing so before 0.08250 however, too many things in motion already
73         my ($pk_col, @rest) = $val->result_source->_pri_cols_or_die;
74         $self->throw_exception(
75           "Relationship '$rel' of type 'filter' can not work with a multicolumn primary key on source '$f_class'"
76         ) if @rest;
77
78         my $pk_val = $val->get_column($pk_col);
79         carp_unique (
80           "Unable to deflate 'filter'-type relationship '$rel' (related object "
81         . "primary key not retrieved), assuming undef instead"
82         ) if ( ! defined $pk_val and $val->in_storage );
83
84         return $pk_val;
85       },
86     });
87   }
88   elsif ($acc_type eq 'multi') {
89
90     quote_sub "${class}::${rel}_rs", "shift->search_related_rs( $rel => \@_ )";
91     quote_sub "${class}::add_to_${rel}", "shift->create_related( $rel => \@_ )";
92     quote_sub "${class}::${rel}", sprintf( <<'EOC', perlstring $rel );
93       DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_WANTARRAY and my $sog = DBIx::Class::_Util::fail_on_internal_wantarray;
94       shift->search_related( %s => @_ )
95 EOC
96   }
97   else {
98     $class->throw_exception("No such relationship accessor type '$acc_type'");
99   }
100
101 }
102
103 1;