Factored common cdbi rel features out into Relationship:: packages
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / CDBICompat / HasA.pm
1 package DBIx::Class::CDBICompat::HasA;
2
3 use strict;
4 use warnings;
5
6 sub has_a {
7   my ($self, $col, $f_class, %args) = @_;
8   $self->throw( "No such column ${col}" ) unless $self->_columns->{$col};
9   eval "require $f_class";
10   if ($args{'inflate'} || $args{'deflate'}) {
11     if (!ref $args{'inflate'}) {
12       my $meth = $args{'inflate'};
13       $args{'inflate'} = sub { $f_class->$meth(shift); };
14     }
15     if (!ref $args{'deflate'}) {
16       my $meth = $args{'deflate'};
17       $args{'deflate'} = sub { shift->$meth; };
18     }
19     $self->inflate_column($col, \%args);
20     return 1;
21   }
22   my ($pri, $too_many) = keys %{ $f_class->_primaries };
23   $self->throw( "has_a only works with a single primary key; ${f_class} has more" )
24     if $too_many;
25   $self->add_relationship($col, $f_class,
26                             { "foreign.${pri}" => "self.${col}" },
27                             { _type => 'has_a', accessor => 'filter' } );
28   return 1;
29 }
30
31 1;