develooper Front page | perl.module-authors | Postings from February 2005

Class::DBI::LazyInflator

From:
Daisuke Maki
Date:
February 5, 2005 00:27
Subject:
Class::DBI::LazyInflator
Message ID:
4204700C.8040302@wafu.ne.jp
I have a module that deferes inflation of columns in Class::DBI until
when they are used... is there something like this available?

=head1 NAME

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

=head1 SYNOPSIS

  package MyData;
  use base qw(Class::DBI);
  use Class::DBI::LazyInflator;
  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());

=head1 DESCRIPTION

Class::DBI::LazyInflator 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, the column that was specified via has_lazy() is wrapped
by Class::DBI::LazyInflator, 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::LazyInflator allows you defer materialization of
such objects until when you really need it

=head1 METHODS

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

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

=head1 AUTHOR

Daisuke Maki <dmaki@cpan.org>

=head1 SEE ALSO

L<Class::DBI>

=cut



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About