<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package DBIx::Class::InflateColumn;

use strict;
use warnings;

use base 'DBIx::Class::Row';
use SQL::Abstract 'is_literal_value';
use namespace::clean;

=head1 NAME

DBIx::Class::InflateColumn - Automatically create references from column data

=head1 SYNOPSIS

  # In your table classes
  __PACKAGE__-&gt;inflate_column('column_name', {
    inflate =&gt; sub {
      my ($raw_value_from_db, $result_object) = @_;
      ...
    },
    deflate =&gt; sub {
      my ($inflated_value_from_user, $result_object) = @_;
      ...
    },
  });

=head1 DESCRIPTION

This component translates column data into references, i.e. "inflating"
the column data. It also "deflates" references into an appropriate format
for the database.

It can be used, for example, to automatically convert to and from
L&lt;DateTime&gt; objects for your date and time fields. There's a
convenience component to actually do that though, try
L&lt;DBIx::Class::InflateColumn::DateTime&gt;.

It will handle all types of references except scalar references. It
will not handle scalar values, these are ignored and thus passed
through to L&lt;SQL::Abstract&gt;. This is to allow setting raw values to
"just work". Scalar references are passed through to the database to
deal with, to allow such settings as C&lt; \'year + 1'&gt; and C&lt; \'DEFAULT' &gt;
to work.

If you want to filter plain scalar values and replace them with
something else, see L&lt;DBIx::Class::FilterColumn&gt;.

=head1 METHODS

=head2 inflate_column

Instruct L&lt;DBIx::Class&gt; to inflate the given column.

In addition to the column name, you must provide C&lt;inflate&gt; and
C&lt;deflate&gt; methods. The C&lt;inflate&gt; method is called when you access
the field, while the C&lt;deflate&gt; method is called when the field needs
to used by the database.

For example, if you have a table C&lt;events&gt; with a timestamp field
named C&lt;insert_time&gt;, you could inflate the column in the
corresponding table class using something like:

    __PACKAGE__-&gt;inflate_column('insert_time', {
        inflate =&gt; sub {
          my ($insert_time_raw_value, $event_result_object) = @_;
          DateTime-&gt;from_epoch( epoch =&gt; $insert_time_raw_value );
        },
        deflate =&gt; sub {
          my ($insert_time_dt_object, $event_result_object) = @_;
          $insert_time_dt_object-&gt;epoch;
        },
    });

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 result object itself.

In this example, calls to an event's C&lt;insert_time&gt; accessor return a
L&lt;DateTime&gt; object. This L&lt;DateTime&gt; 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&lt;DBIx::Class::DateTime::Epoch&gt;

=cut

sub inflate_column {
  my ($self, $col, $attrs) = @_;

  my $colinfo = $self-&gt;column_info($col);

  $self-&gt;throw_exception("InflateColumn can not be used on a column with a declared FilterColumn filter")
    if defined $colinfo-&gt;{_filter_info} and $self-&gt;isa('DBIx::Class::FilterColumn');

  $self-&gt;throw_exception("No such column $col to inflate")
    unless $self-&gt;has_column($col);
  $self-&gt;throw_exception("inflate_column needs attr hashref")
    unless ref $attrs eq 'HASH';
  $colinfo-&gt;{_inflate_info} = $attrs;
  my $acc = $colinfo-&gt;{accessor};
  $self-&gt;mk_group_accessors('inflated_column' =&gt; [ (defined $acc ? $acc : $col), $col]);
  return 1;
}

sub _inflated_column {
  my ($self, $col, $value) = @_;

  return $value if (
    ! defined $value # NULL is NULL is NULL
      or
    is_literal_value($value) #that would be a not-yet-reloaded literal update
  );

  my $info = $self-&gt;result_source-&gt;column_info($col)
    or $self-&gt;throw_exception("No column info for $col");

  return $value unless exists $info-&gt;{_inflate_info};

  return (
    $info-&gt;{_inflate_info}{inflate}
      ||
    $self-&gt;throw_exception("No inflator found for '$col'")
  )-&gt;($value, $self);
}

sub _deflated_column {
  my ($self, $col, $value) = @_;

  ## Deflate any refs except for literals, pass through plain values
  return $value if (
    ! length ref $value
      or
    is_literal_value($value)
  );

  my $info = $self-&gt;result_source-&gt;column_info($col) or
    $self-&gt;throw_exception("No column info for $col");

  return $value unless exists $info-&gt;{_inflate_info};

  return (
    $info-&gt;{_inflate_info}{deflate}
      ||
    $self-&gt;throw_exception("No deflator found for '$col'")
  )-&gt;($value, $self);
}

=head2 get_inflated_column

  my $val = $obj-&gt;get_inflated_column($col);

Fetch a column value in its inflated state.  This is directly
analogous to L&lt;DBIx::Class::Row/get_column&gt; in that it only fetches a
column already retrieved from the database, and then inflates it.
Throws an exception if the column requested is not an inflated column.

=cut

sub get_inflated_column {
  my ($self, $col) = @_;

  $self-&gt;throw_exception("$col is not an inflated column")
    unless exists $self-&gt;result_source-&gt;column_info($col)-&gt;{_inflate_info};

  # we take care of keeping things in sync
  return $self-&gt;{_inflated_column}{$col}
    if exists $self-&gt;{_inflated_column}{$col};

  my $val = $self-&gt;get_column($col);

  return $self-&gt;{_inflated_column}{$col} = $self-&gt;_inflated_column($col, $val);
}

=head2 set_inflated_column

  my $copy = $obj-&gt;set_inflated_column($col =&gt; $val);

Sets a column value from an inflated value.  This is directly
analogous to L&lt;DBIx::Class::Row/set_column&gt;.

=cut

sub set_inflated_column {
  my ($self, $col, $value) = @_;

  # pass through deflated stuff
  if (! length ref $value or is_literal_value($value)) {
    $self-&gt;set_column($col, $value);
    delete $self-&gt;{_inflated_column}{$col};
  }
  # need to call set_column with the deflate cycle so that
  # relationship caches are nuked if any
  # also does the compare-for-dirtyness and change tracking dance
  else {
    $self-&gt;set_column($col, $self-&gt;_deflated_column($col, $value));
    $self-&gt;{_inflated_column}{$col} = $value;
  }

  return $value;
}

=head2 store_inflated_column

  my $copy = $obj-&gt;store_inflated_column($col =&gt; $val);

Sets a column value from an inflated value without marking the column
as dirty. This is directly analogous to L&lt;DBIx::Class::Row/store_column&gt;.

=cut

sub store_inflated_column {
  my ($self, $col, $value) = @_;

  if (! length ref $value or is_literal_value($value)) {
    delete $self-&gt;{_inflated_column}{$col};
    $self-&gt;store_column($col =&gt; $value);
  }
  else {
    delete $self-&gt;{_column_data}{$col};
    $self-&gt;{_inflated_column}{$col} = $value;
  }

  return $value;
}

=head1 SEE ALSO

=over 4

=item L&lt;DBIx::Class::Core&gt; - This component is loaded as part of the
      C&lt;core&gt; L&lt;DBIx::Class&gt; components; generally there is no need to
      load it directly

=back

=head1 FURTHER QUESTIONS?

Check the list of L&lt;additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT&gt;.

=head1 COPYRIGHT AND LICENSE

This module is free software L&lt;copyright|DBIx::Class/COPYRIGHT AND LICENSE&gt;
by the L&lt;DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS&gt;. You can
redistribute it and/or modify it under the same terms as the
L&lt;DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE&gt;.

=cut

1;
</pre></body></html>