From: David Kamholz Date: Sun, 18 Jun 2006 21:04:10 +0000 (+0000) Subject: InflateColumn re-refactor X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=89279e9d0fff6fe2b90d0e3624c0349403c724f7;p=dbsrgits%2FDBIx-Class-Historic.git InflateColumn re-refactor --- diff --git a/lib/DBIx/Class/InflateColumn.pm b/lib/DBIx/Class/InflateColumn.pm index 1a69269..26a8832 100644 --- a/lib/DBIx/Class/InflateColumn.pm +++ b/lib/DBIx/Class/InflateColumn.pm @@ -83,20 +83,15 @@ sub _inflated_column { return $inflate->($value, $self); } -sub _deflate_column { - my ($self, $col) = @_; - return if exists $self->{_column_data}{$col}; - my $value = $self->{_inflated_column}{$col}; - if (ref $value) { - my $info = $self->column_info($col) or - $self->throw_exception("No column info for $col"); - if (exists $info->{_inflate_info}) { - my $deflate = $info->{_inflate_info}{deflate}; - $self->throw_exception("No deflator for $col") unless defined $deflate; - $value = $deflate->($value, $self); - } - } - $self->store_column($col, $value); +sub _deflated_column { + my ($self, $col, $value) = @_; + return $value unless ref $value; # If it's not an object, don't touch it + my $info = $self->column_info($col) or + $self->throw_exception("No column info for $col"); + 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); } =head2 get_inflated_column @@ -114,7 +109,6 @@ sub get_inflated_column { my ($self, $col) = @_; $self->throw_exception("$col is not an inflated column") unless exists $self->column_info($col)->{_inflate_info}; - return $self->{_inflated_column}{$col} if exists $self->{_inflated_column}{$col}; return $self->{_inflated_column}{$col} = @@ -132,11 +126,8 @@ analogous to L. sub set_inflated_column { my ($self, $col, $obj) = @_; - my $old = $self->get_inflated_column($col); - my $ret = $self->store_inflated_column($col, $obj); - $self->{_dirty_columns}{$col} = 1 - if (defined $old ^ defined $ret) || (defined $old && $old ne $ret); - return $ret; + $self->set_column($col, $self->_deflated_column($col, $obj)); + return $self->store_inflated_column($col, $obj); } =head2 store_inflated_column @@ -150,13 +141,7 @@ as dirty. This is directly analogous to L. sub store_inflated_column { my ($self, $col, $obj) = @_; - if (ref $obj) { - delete $self->{_column_data}{$col}; - return $self->{_inflated_column}{$col} = $obj; - } else { - delete $self->{_inflated_column}{$col}; - return $self->store_column($col, $obj); - } + return $self->{_inflated_column}{$col} = $obj; } =head2 get_column @@ -169,14 +154,21 @@ when the method is invoked. sub get_column { my ($self, $col) = @_; - $self->_deflate_column($col); + use Carp; confess unless ref $self; + if (exists $self->{_inflated_column}{$col} + && !exists $self->{_column_data}{$col}) { + $self->store_column($col, $self->_deflated_column($col, $self->{_inflated_column}{$col})); + } return $self->next::method($col); } sub get_columns { my $self = shift; if (exists $self->{_inflated_column}) { - $self->_deflate_column($_) for keys %{$self->{_inflated_column}}; + foreach my $col (keys %{$self->{_inflated_column}}) { + $self->store_column($col, $self->_deflated_column($col)) + unless exists $self->{_column_data}{$col}; + } } return $self->next::method; } diff --git a/lib/DBIx/Class/Row.pm b/lib/DBIx/Class/Row.pm index 9bc86d7..3efe418 100644 --- a/lib/DBIx/Class/Row.pm +++ b/lib/DBIx/Class/Row.pm @@ -210,7 +210,7 @@ Identical to get_columns but only returns those that have been changed. sub get_dirty_columns { my $self = shift; - return map { $_ => $self->get_column($_) } + return map { $_ => $self->{_column_data}{$_} } keys %{$self->{_dirty_columns}}; } diff --git a/t/68inflate.t b/t/68inflate.t index 41cf8c8..0ce901c 100644 --- a/t/68inflate.t +++ b/t/68inflate.t @@ -11,7 +11,7 @@ my $schema = DBICTest->init_schema(); eval { require DateTime }; plan skip_all => "Need DateTime for inflation tests" if $@; -plan tests => 3; +plan tests => 4; DBICTest::Schema::CD->inflate_column( 'year', { inflate => sub { DateTime->new( year => shift ) }, @@ -24,6 +24,8 @@ my $cd = $schema->resultset("CD")->find(3); is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' ); +is( $cd->year->year, 1997, 'inflated year ok' ); + is( $cd->year->month, 1, 'inflated month ok' ); # deflate test