The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Class::DBI::LazyInflate - Defer Inflating Of Columns Until They Are Used

SYNOPSIS

  package MyData;
  use base qw(Class::DBI);
  use Class::DBI::LazyInflate;
  use DateTime;
  use DateTime::Format::MySQL;

  __PACKAGE__->has_lazy(
    'lastmod',
    inflate => sub { DateTime::Format::MySQL->parse_datetime(shift) },
    deflate => sub { DateTime::Format::MySQL->format_datetime(shift) },
  );

  my $obj = MyData->retrieve($key); # lastmod is not inflated yet
  $obj->lastmod()->year();          # now it is.

  $obj->lastmod(DateTime->now());

DESCRIPTION

Class::DBI::LazyInflate is a utility class that allows you to create DBI columns that only inflate to an object when it is required. When a row is fetched, columns specified via has_lazy() is wrapped by Data::Lazy, such that it is inflated only when the column is actually used.

As seen in the SYNOPSIS section, one application of this class is for columns that inflate to objects that are costly to create, such as DateTime. Class::DBI::LazyInflate allows you defer materialization of such objects until when you really need it

METHODS

has_lazy($col, $class, inflate => ..., deflate => ...)

has_lazy() declares that column is to be inflated lazily, and is installed to the calling package's namespace upon call to "use Class::DBI::LazyInflate". The arguments are exactly the same has has_a().

AUTHOR

Daisuke Maki <dmaki@cpan.org>

SEE ALSO

Class::DBI Data::Lazy