From: Matt S Trout Date: Tue, 2 Aug 2005 04:12:20 +0000 (+0000) Subject: Refactored HasA to use InflateColumn X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9f300b1bcbbaef6d0a3e6fffb37e05119bd2c8cd;p=dbsrgits%2FDBIx-Class-Historic.git Refactored HasA to use InflateColumn --- diff --git a/lib/DBIx/Class/CDBICompat/ColumnCase.pm b/lib/DBIx/Class/CDBICompat/ColumnCase.pm index 05ab20f..4e65117 100644 --- a/lib/DBIx/Class/CDBICompat/ColumnCase.pm +++ b/lib/DBIx/Class/CDBICompat/ColumnCase.pm @@ -17,7 +17,7 @@ sub _register_columns { sub has_a { my ($class, $col, @rest) = @_; $class->NEXT::ACTUAL::has_a(lc($col), @rest); - $class->mk_group_accessors('has_a' => $col); + $class->mk_group_accessors('inflated_column' => $col); return 1; } @@ -28,19 +28,19 @@ sub has_many { lc($f_key) ), @rest); } -sub get_has_a { +sub get_inflated_column { my ($class, $get, @rest) = @_; - return $class->NEXT::ACTUAL::get_has_a(lc($get), @rest); + return $class->NEXT::ACTUAL::get_inflated_column(lc($get), @rest); } -sub store_has_a { +sub store_inflated_column { my ($class, $set, @rest) = @_; - return $class->NEXT::ACTUAL::store_has_a(lc($set), @rest); + return $class->NEXT::ACTUAL::store_inflated_column(lc($set), @rest); } -sub set_has_a { +sub set_inflated_column { my ($class, $set, @rest) = @_; - return $class->NEXT::ACTUAL::set_has_a(lc($set), @rest); + return $class->NEXT::ACTUAL::set_inflated_column(lc($set), @rest); } sub get_column { diff --git a/lib/DBIx/Class/CDBICompat/HasA.pm b/lib/DBIx/Class/CDBICompat/HasA.pm index 43f7609..fe85214 100644 --- a/lib/DBIx/Class/CDBICompat/HasA.pm +++ b/lib/DBIx/Class/CDBICompat/HasA.pm @@ -13,75 +13,16 @@ sub has_a { $self->add_relationship($col, $f_class, { "foreign.${pri}" => "self.${col}" }, { _type => 'has_a' } ); - $self->mk_group_accessors('has_a' => $col); + $self->inflate_column($col, + { inflate => sub { + my ($val, $self) = @_; + return ($self->search_related($col, {}, {}))[0] + || $f_class->new({ $pri => $val }); }, + deflate => sub { + my ($val, $self) = @_; + $self->throw("$val isn't a $f_class") unless $val->isa($f_class); + return ($val->_ident_values)[0] } } ); 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; -} - -sub store_has_a { - my ($self, $rel, $obj) = @_; - unless (ref $obj) { - delete $self->{_relationship_data}{$rel}; - return $self->store_column($rel, $obj); - } - my $rel_obj = $self->_relationships->{$rel}; - $self->throw( "Can't set $rel: object $obj is not of class ".$rel_obj->{class} ) - unless $obj->isa($rel_obj->{class}); - $self->{_relationship_data}{$rel} = $obj; - #warn "Storing $obj: ".($obj->_ident_values)[0]; - $self->store_column($rel, ($obj->_ident_values)[0]); - return $obj; -} - -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}; - } - 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) { - $self->throw( "Object $value is not of class ".$rel_obj->{class} ) - unless $value->isa($rel_obj->{class}); - $value = ($value->_ident_values)[0]; - #warn $value; - } - } - return $self->NEXT::ACTUAL::_cond_value($attrs, $key, $value); -} - 1; diff --git a/lib/DBIx/Class/InflateColumn.pm b/lib/DBIx/Class/InflateColumn.pm index 27ac2db..eea026a 100644 --- a/lib/DBIx/Class/InflateColumn.pm +++ b/lib/DBIx/Class/InflateColumn.pm @@ -14,6 +14,7 @@ sub inflate_column { sub _inflate_column_value { my ($self, $col, $value) = @_; + return $value unless defined $value; # NULL is NULL is NULL return $value unless exists $self->_columns->{$col}{_inflate_info}{inflate}; my $inflate = $self->_columns->{$col}{_inflate_info}{inflate}; return $inflate->($value, $self); @@ -53,9 +54,11 @@ sub store_inflated_column { delete $self->{_inflated_column}{$col}; return $self->store_column($col, $obj); } + my $deflated = $self->_deflate_column_value($col, $obj); + # Do this now so we don't store if it's invalid $self->{_inflated_column}{$col} = $obj; #warn "Storing $obj: ".($obj->_ident_values)[0]; - $self->store_column($col, $self->_deflate_column_value($col, $obj)); + $self->store_column($col, $deflated); return $obj; }