Daisuke Maki > Class-DBI-LazyInflate-0.06 > Class::DBI::LazyInflate

Download:
Class-DBI-LazyInflate-0.06.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.06   Source  

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', 'DateTime',
    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());

  # Specify another Class::DBI type as a column:
  __PACKAGE__->has_lazy(cdbi_field => 'MyData');

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 as has_a().

If inflate is not specified, it will lazily perform the default inflate procedure that Class::DBI uses.

AUTHOR ^

Copyright (c) 2004-2005 Daisuke Maki <dmaki@cpan.org>

SEE ALSO ^

Class::DBI Data::Lazy

For DateTime objects, you may also be interested in DateTime::LazyInit.

syntax highlighting: