From: Peter Rabbitson Date: Mon, 2 Jun 2014 12:05:45 +0000 (+0200) Subject: Rename some variables and reformat the FC/IC codepaths for clarity X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5ae153d7a25b664e5dc33824a7efd690ac9786b5;p=dbsrgits%2FDBIx-Class-Historic.git Rename some variables and reformat the FC/IC codepaths for clarity Zero functional changes --- diff --git a/lib/DBIx/Class/FilterColumn.pm b/lib/DBIx/Class/FilterColumn.pm index 687c278..c48b6c1 100644 --- a/lib/DBIx/Class/FilterColumn.pm +++ b/lib/DBIx/Class/FilterColumn.pm @@ -66,13 +66,17 @@ sub get_filtered_column { my $val = $self->get_column($col); - return $self->{_filtered_column}{$col} = $self->_column_from_storage($col, $val); + return $self->{_filtered_column}{$col} = $self->_column_from_storage( + $col, $val + ); } sub get_column { my ($self, $col) = @_; if (exists $self->{_filtered_column}{$col}) { - return $self->{_column_data}{$col} ||= $self->_column_to_storage ($col, $self->{_filtered_column}{$col}); + return $self->{_column_data}{$col} ||= $self->_column_to_storage ( + $col, $self->{_filtered_column}{$col} + ); } return $self->next::method ($col); @@ -83,8 +87,9 @@ sub get_columns { my $self = shift; foreach my $col (keys %{$self->{_filtered_column}||{}}) { - $self->{_column_data}{$col} ||= $self->_column_to_storage ($col, $self->{_filtered_column}{$col}) - if exists $self->{_filtered_column}{$col}; + $self->{_column_data}{$col} ||= $self->_column_to_storage ( + $col, $self->{_filtered_column}{$col} + ) if exists $self->{_filtered_column}{$col}; } $self->next::method (@_); @@ -117,36 +122,37 @@ sub set_filtered_column { } sub update { - my ($self, $attrs, @rest) = @_; + my ($self, $data, @rest) = @_; - foreach my $key (keys %{$attrs||{}}) { + foreach my $col (keys %{$data||{}}) { if ( - $self->has_column($key) + $self->has_column($col) && - exists $self->column_info($key)->{_filter_info} + exists $self->column_info($col)->{_filter_info} ) { - $self->set_filtered_column($key, delete $attrs->{$key}); + $self->set_filtered_column($col, delete $data->{$col}); # FIXME update() reaches directly into the object-hash # and we may *not* have a filtered value there - thus # the void-ctx filter-trigger - $self->get_column($key) unless exists $self->{_column_data}{$key}; + $self->get_column($col) unless exists $self->{_column_data}{$col}; } } - return $self->next::method($attrs, @rest); + return $self->next::method($data, @rest); } sub new { - my ($class, $attrs, @rest) = @_; - my $source = $attrs->{-result_source} + my ($class, $data, @rest) = @_; + my $source = $data->{-result_source} or $class->throw_exception('Sourceless rows are not supported with DBIx::Class::FilterColumn'); - my $obj = $class->next::method($attrs, @rest); - foreach my $key (keys %{$attrs||{}}) { - if ($obj->has_column($key) && - exists $obj->column_info($key)->{_filter_info} ) { - $obj->set_filtered_column($key, $attrs->{$key}); + my $obj = $class->next::method($data, @rest); + + foreach my $col (keys %{$data||{}}) { + if ($obj->has_column($col) && + exists $obj->column_info($col)->{_filter_info} ) { + $obj->set_filtered_column($col, $data->{$col}); } } diff --git a/lib/DBIx/Class/InflateColumn.pm b/lib/DBIx/Class/InflateColumn.pm index 23bc4c9..d84af86 100644 --- a/lib/DBIx/Class/InflateColumn.pm +++ b/lib/DBIx/Class/InflateColumn.pm @@ -111,10 +111,11 @@ sub _inflated_column { return $value unless exists $info->{_inflate_info}; - my $inflate = $info->{_inflate_info}{inflate}; - $self->throw_exception("No inflator for $col") unless defined $inflate; - - return $inflate->($value, $self); + return ( + $info->{_inflate_info}{inflate} + || + $self->throw_exception("No inflator found for '$col'") + )->($value, $self); } sub _deflated_column { @@ -132,10 +133,11 @@ sub _deflated_column { return $value unless exists $info->{_inflate_info}; - my $deflate = $info->{_inflate_info}{deflate}; - $self->throw_exception("No deflator for $col") unless defined $deflate; - - return $deflate->($value, $self); + return ( + $info->{_inflate_info}{deflate} + || + $self->throw_exception("No deflator found for '$col'") + )->($value, $self); } =head2 get_inflated_column @@ -151,8 +153,11 @@ Throws an exception if the column requested is not an inflated column. sub get_inflated_column { my ($self, $col) = @_; + $self->throw_exception("$col is not an inflated column") unless exists $self->column_info($col)->{_inflate_info}; + + # we take care of keeping things in sync return $self->{_inflated_column}{$col} if exists $self->{_inflated_column}{$col}; @@ -173,15 +178,16 @@ analogous to L. =cut sub set_inflated_column { - my ($self, $col, $inflated) = @_; - $self->set_column($col, $self->_deflated_column($col, $inflated)); + my ($self, $col, $value) = @_; + + $self->set_column($col, $self->_deflated_column($col, $value)); - if (length ref $inflated and ! is_literal_value($inflated) ) { - $self->{_inflated_column}{$col} = $inflated; + if (length ref $value and ! is_literal_value($value) ) { + $self->{_inflated_column}{$col} = $value; } else { delete $self->{_inflated_column}{$col}; } - return $inflated; + return $value; } =head2 store_inflated_column @@ -194,18 +200,18 @@ as dirty. This is directly analogous to L. =cut sub store_inflated_column { - my ($self, $col, $inflated) = @_; + my ($self, $col, $value) = @_; - if (is_literal_value($inflated)) { + if (is_literal_value($value)) { delete $self->{_inflated_column}{$col}; - $self->store_column($col => $inflated); + $self->store_column($col => $value); } else { delete $self->{_column_data}{$col}; - $self->{_inflated_column}{$col} = $inflated; + $self->{_inflated_column}{$col} = $value; } - return $inflated; + return $value; } =head1 SEE ALSO diff --git a/lib/DBIx/Class/Row.pm b/lib/DBIx/Class/Row.pm index 05d19a5..9127c94 100644 --- a/lib/DBIx/Class/Row.pm +++ b/lib/DBIx/Class/Row.pm @@ -662,12 +662,20 @@ To retrieve all loaded column values as a hash, use L. sub get_column { my ($self, $column) = @_; $self->throw_exception( "Can't fetch data as class method" ) unless ref $self; - return $self->{_column_data}{$column} if exists $self->{_column_data}{$column}; + + return $self->{_column_data}{$column} + if exists $self->{_column_data}{$column}; + if (exists $self->{_inflated_column}{$column}) { - return $self->store_column($column, - $self->_deflated_column($column, $self->{_inflated_column}{$column})); + # deflate+return cycle + return $self->store_column($column, $self->_deflated_column( + $column, $self->{_inflated_column}{$column} + )); } - $self->throw_exception( "No such column '${column}'" ) unless $self->has_column($column); + + $self->throw_exception( "No such column '${column}'" ) + unless $self->has_column($column); + return undef; } @@ -693,8 +701,12 @@ database (or set locally). sub has_column_loaded { my ($self, $column) = @_; $self->throw_exception( "Can't call has_column data as class method" ) unless ref $self; - return 1 if exists $self->{_inflated_column}{$column}; - return exists $self->{_column_data}{$column}; + + return ( + exists $self->{_inflated_column}{$column} + or + exists $self->{_column_data}{$column} + ) ? 1 : 0; } =head2 get_columns @@ -719,6 +731,7 @@ See L to get the inflated values. sub get_columns { my $self = shift; if (exists $self->{_inflated_column}) { + # deflate cycle for each inflation, including filter rels foreach my $col (keys %{$self->{_inflated_column}}) { unless (exists $self->{_column_data}{$col}) { @@ -920,15 +933,14 @@ sub set_column { my ($self, $column, $new_value) = @_; my $had_value = $self->has_column_loaded($column); - my ($old_value, $in_storage) = ($self->get_column($column), $self->in_storage) - if $had_value; + my $old_value = $self->get_column($column); $new_value = $self->store_column($column, $new_value); my $dirty = $self->{_dirty_columns}{$column} || - $in_storage # no point tracking dirtyness on uninserted data + $self->in_storage # no point tracking dirtyness on uninserted data ? ! $self->_eq_column_values ($column, $old_value, $new_value) : 1 ; @@ -963,7 +975,7 @@ sub set_column { $had_value and # no storage - no storage-value - $in_storage + $self->in_storage and # no value already stored (multiple changes before commit to storage) ! exists $self->{_column_data_in_storage}{$column} @@ -1074,6 +1086,7 @@ sub set_inflated_columns { if (ref $upd->{$key}) { my $info = $self->relationship_info($key); my $acc_type = $info->{attrs}{accessor} || ''; + if ($acc_type eq 'single') { my $rel_obj = delete $upd->{$key}; $self->set_from_related($key => $rel_obj); diff --git a/t/relationship/core.t b/t/relationship/core.t index 46e655c..c611142 100644 --- a/t/relationship/core.t +++ b/t/relationship/core.t @@ -227,7 +227,7 @@ is( $twokey->fourkeys_to_twokeys->count, 0, my $undef_artist_cd = $schema->resultset("CD")->new_result({ 'title' => 'badgers', 'year' => 2007 }); -is($undef_artist_cd->has_column_loaded('artist'), '', 'FK not loaded'); +ok(! $undef_artist_cd->has_column_loaded('artist'), 'FK not loaded'); is($undef_artist_cd->search_related('artist')->count, 0, '0=1 search when FK does not exist and object not yet in db'); lives_ok { $undef_artist_cd->related_resultset('artist')->new({name => 'foo'});