X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FCDBICompat%2FHasA.pm;h=647674f1bd73454cfe62a84bc038e4952ca512cd;hb=89d794d486e3c73b489817059e9c3983deebde7f;hp=e5c2cf04fdf28add03ffcdd91c3c7c14bf401377;hpb=b8e1e21f0fcd55e6e3ce987e57601b279a75b666;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/CDBICompat/HasA.pm b/lib/DBIx/Class/CDBICompat/HasA.pm index e5c2cf0..647674f 100644 --- a/lib/DBIx/Class/CDBICompat/HasA.pm +++ b/lib/DBIx/Class/CDBICompat/HasA.pm @@ -1,84 +1,46 @@ -package DBIx::Class::CDBICompat::HasA; +package # hide from PAUSE + DBIx::Class::CDBICompat::HasA; use strict; use warnings; sub has_a { - my ($self, $col, $f_class) = @_; - die "No such column ${col}" unless $self->_columns->{$col}; - eval "require $f_class"; - my ($pri, $too_many) = keys %{ $f_class->_primaries }; - die "has_a only works with a single primary key; ${f_class} has more" - if $too_many; - $self->add_relationship($col, $f_class, - { "foreign.${pri}" => "self.${col}" }, - { _type => 'has_a' } ); - $self->delete_accessor($col); - $self->mk_group_accessors('has_a' => $col); - return 1; -} - -sub get_has_a { - my ($self, $rel) = @_; - #warn $rel; - #warn join(', ', %{$self->{_column_data}}); - return $self->{_relationship_data}{$rel} - if $self->{_relationship_data}{$rel}; - return undef unless $self->get_column($rel); - #my ($pri) = (keys %{$self->_relationships->{$rel}{class}->_primaries})[0]; - return $self->{_relationship_data}{$rel} = - ($self->search_related($rel, {}, {}))[0] - || do { - my $f_class = $self->_relationships->{$rel}{class}; - my ($pri) = keys %{$f_class->_primaries}; - $f_class->new({ $pri => $self->get_column($rel) }); }; -} - -sub set_has_a { - my ($self, $rel, @rest) = @_; - my $ret = $self->store_has_a($rel, @rest); - $self->{_dirty_columns}{$rel} = 1; - return $ret; -} + my ($self, $col, $f_class, %args) = @_; + $self->throw_exception( "No such column ${col}" ) unless $self->has_column($col); + $self->ensure_class_loaded($f_class); + if ($args{'inflate'} || $args{'deflate'}) { # Non-database has_a + if (!ref $args{'inflate'}) { + my $meth = $args{'inflate'}; + $args{'inflate'} = sub { $f_class->$meth(shift); }; + } + if (!ref $args{'deflate'}) { + my $meth = $args{'deflate'}; + $args{'deflate'} = sub { shift->$meth; }; + } + $self->inflate_column($col, \%args); + return 1; + } -sub store_has_a { - my ($self, $rel, $obj) = @_; - return $self->set_column($rel, $obj) unless ref $obj; - my $rel_obj = $self->_relationships->{$rel}; - die "Can't set $rel: object $obj is not of class ".$rel_obj->{class} - unless $obj->isa($rel_obj->{class}); - $self->{_relationship_data}{$rel} = $obj; - $self->set_column($rel, ($obj->_ident_values)[0]); - return $obj; + $self->belongs_to($col, $f_class); + return 1; } -sub new { - my ($class, $attrs, @rest) = @_; - my %hasa; - foreach my $key (keys %$attrs) { - my $rt = $class->_relationships->{$key}{attrs}{_type}; - next unless $rt && $rt eq 'has_a' && ref $attrs->{$key}; - $hasa{$key} = delete $attrs->{$key}; +sub search { + my $self = shift; + my $attrs = {}; + if (@_ > 1 && ref $_[$#_] eq 'HASH') { + $attrs = { %{ pop(@_) } }; } - my $new = $class->NEXT::ACTUAL::new($attrs, @rest); - foreach my $key (keys %hasa) { - $new->store_has_a($key, $hasa{$key}); - } - return $new; -} - -sub _cond_value { - my ($self, $attrs, $key, $value) = @_; - if ( my $rel_obj = $self->_relationships->{$key} ) { - my $rel_type = $rel_obj->{attrs}{_type} || ''; - if ($rel_type eq 'has_a' && ref $value) { - die "Object $value is not of class ".$rel_obj->{class} - unless $value->isa($rel_obj->{class}); - $value = ($value->_ident_values)[0]; - #warn $value; + my $where = (@_ ? ((@_ == 1) ? ((ref $_[0] eq "HASH") ? { %{+shift} } : shift) + : {@_}) + : undef()); + if (ref $where eq 'HASH') { + foreach my $key (keys %$where) { # has_a deflation hack + $where->{$key} = ''.$where->{$key} + if eval { $where->{$key}->isa('DBIx::Class') }; } } - return $self->NEXT::ACTUAL::_cond_value($attrs, $key, $value); + $self->next::method($where, $attrs); } 1;