Rename some variables and reformat the FC/IC codepaths for clarity
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / InflateColumn.pm
index e9de5da..d84af86 100644 (file)
@@ -2,9 +2,10 @@ package DBIx::Class::InflateColumn;
 
 use strict;
 use warnings;
-use Scalar::Util qw/blessed/;
 
-use base qw/DBIx::Class::Row/;
+use base 'DBIx::Class::Row';
+use DBIx::Class::_Util 'is_literal_value';
+use namespace::clean;
 
 =head1 NAME
 
@@ -12,11 +13,17 @@ DBIx::Class::InflateColumn - Automatically create references from column data
 
 =head1 SYNOPSIS
 
-    # In your table classes
-    __PACKAGE__->inflate_column('column_name', {
-        inflate => sub { ... },
-        deflate => sub { ... },
-    });
+  # In your table classes
+  __PACKAGE__->inflate_column('column_name', {
+    inflate => sub {
+      my ($raw_value_from_db, $result_object) = @_;
+      ...
+    },
+    deflate => sub {
+      my ($inflated_value_from_user, $result_object) = @_;
+      ...
+    },
+  });
 
 =head1 DESCRIPTION
 
@@ -55,20 +62,25 @@ named C<insert_time>, you could inflate the column in the
 corresponding table class using something like:
 
     __PACKAGE__->inflate_column('insert_time', {
-        inflate => sub { DateTime::Format::Pg->parse_datetime(shift); },
-        deflate => sub { DateTime::Format::Pg->format_datetime(shift); },
+        inflate => sub {
+          my ($insert_time_raw_value, $event_result_object) = @_;
+          DateTime->from_epoch( epoch => $insert_time_raw_value );
+        },
+        deflate => sub {
+          my ($insert_time_dt_object, $event_result_object) = @_;
+          $insert_time_dt_object->epoch;
+        },
     });
 
-(Replace L<DateTime::Format::Pg> with the appropriate module for your
-database, or consider L<DateTime::Format::DBI>.)
-
 The coderefs you set for inflate and deflate are called with two parameters,
-the first is the value of the column to be inflated/deflated, the second is the
-row object itself. Thus you can call C<< ->result_source->schema->storage->dbh >> in your inflate/defalte subs, to feed to L<DateTime::Format::DBI>.
+the first is the value of the column to be inflated/deflated, the second is
+the result object itself.
 
 In this example, calls to an event's C<insert_time> accessor return a
-L<DateTime> object. This L<DateTime> object is later "deflated" when
-used in the database layer.
+L<DateTime> object. This L<DateTime> object is later "deflated" back
+to the integer epoch representation when used in the database layer.
+For a much more thorough handling of the above example, please see
+L<DBIx::Class::DateTime::Epoch>
 
 =cut
 
@@ -77,9 +89,8 @@ sub inflate_column {
 
   my $colinfo = $self->column_info($col);
 
-  $self->throw_exception("InflateColumn does not work with FilterColumn")
-    if $self->isa('DBIx::Class::FilterColumn') &&
-      defined $colinfo->{_filter_info};
+  $self->throw_exception("InflateColumn can not be used on a column with a declared FilterColumn filter")
+    if defined $colinfo->{_filter_info} and $self->isa('DBIx::Class::FilterColumn');
 
   $self->throw_exception("No such column $col to inflate")
     unless $self->has_column($col);
@@ -94,25 +105,39 @@ sub inflate_column {
 sub _inflated_column {
   my ($self, $col, $value) = @_;
   return $value unless defined $value; # NULL is NULL is NULL
+
   my $info = $self->column_info($col)
     or $self->throw_exception("No column info for $col");
+
   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 {
   my ($self, $col, $value) = @_;
-#  return $value unless ref $value && blessed($value); # If it's not an object, don't touch it
-  ## Leave scalar refs (ala SQL::Abstract literal SQL), untouched, deflate all other refs
-  return $value unless (ref $value && ref($value) ne 'SCALAR');
+
+  ## Deflate any refs except for literals, pass through plain values
+  return $value if (
+    ! length ref $value
+      or
+    is_literal_value($value)
+  );
+
   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);
+
+  return (
+    $info->{_inflate_info}{deflate}
+      ||
+    $self->throw_exception("No deflator found for '$col'")
+  )->($value, $self);
 }
 
 =head2 get_inflated_column
@@ -128,13 +153,17 @@ 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};
 
   my $val = $self->get_column($col);
-  return $val if ref $val eq 'SCALAR';  #that would be a not-yet-reloaded sclarref update
+
+  return $val if is_literal_value($val);  #that would be a not-yet-reloaded literal update
 
   return $self->{_inflated_column}{$col} = $self->_inflated_column($col, $val);
 }
@@ -149,15 +178,16 @@ analogous to L<DBIx::Class::Row/set_column>.
 =cut
 
 sub set_inflated_column {
-  my ($self, $col, $inflated) = @_;
-  $self->set_column($col, $self->_deflated_column($col, $inflated));
-#  if (blessed $inflated) {
-  if (ref $inflated && ref($inflated) ne 'SCALAR') {
-    $self->{_inflated_column}{$col} = $inflated;
+  my ($self, $col, $value) = @_;
+
+  $self->set_column($col, $self->_deflated_column($col, $value));
+
+  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
@@ -170,15 +200,18 @@ as dirty. This is directly analogous to L<DBIx::Class::Row/store_column>.
 =cut
 
 sub store_inflated_column {
-  my ($self, $col, $inflated) = @_;
-#  unless (blessed $inflated) {
-  unless (ref $inflated && ref($inflated) ne 'SCALAR') {
-      delete $self->{_inflated_column}{$col};
-      $self->store_column($col => $inflated);
-      return $inflated;
+  my ($self, $col, $value) = @_;
+
+  if (is_literal_value($value)) {
+    delete $self->{_inflated_column}{$col};
+    $self->store_column($col => $value);
   }
-  delete $self->{_column_data}{$col};
-  return $self->{_inflated_column}{$col} = $inflated;
+  else {
+    delete $self->{_column_data}{$col};
+    $self->{_inflated_column}{$col} = $value;
+  }
+
+  return $value;
 }
 
 =head1 SEE ALSO