added ResultSource::Table::Cached
David Kamholz [Tue, 7 Feb 2006 22:46:57 +0000 (22:46 +0000)]
lib/DBIx/Class/ResultSource/Table/Cached.pm [new file with mode: 0644]

diff --git a/lib/DBIx/Class/ResultSource/Table/Cached.pm b/lib/DBIx/Class/ResultSource/Table/Cached.pm
new file mode 100644 (file)
index 0000000..ebf40b4
--- /dev/null
@@ -0,0 +1,41 @@
+package DBIx::Class::ResultSource::Table::Cached;
+
+use Scalar::Util qw/weaken/;
+
+use base qw/DBIx::Class/;
+__PACKAGE__->load_components(qw/ResultSource::Table/);
+
+sub resultset {
+  my $self = shift;
+  return $self->{_resultset} ||= do {
+      my $rs = $self->next::method;
+      weaken $rs->result_source;
+      $rs;
+  };
+}
+
+1;
+
+__END__
+
+=head1 NAME 
+
+DBIx::Class::ResultSource::Table::Cached - Table object that caches its own resultset
+
+=head1 DESCRIPTION
+
+This is a modified version of L<DBIx::Class::ResultSource::Table> that caches
+its resultset, so when you call $schema->resultset('Foo') it does not 
+re-instantiate the resultset each time. In pathological cases this may not
+work correctly, e.g. if you change important attributes of the result source
+object.
+
+=head1 AUTHORS
+
+David Kamholz <dkamholz@cpan.org>
+
+=head1 LICENSE
+
+You may distribute this code under the same terms as Perl itself.
+
+=cut