Initial work on getting POD coverage testing working
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultSetManager.pm
CommitLineData
db57afbb 1package DBIx::Class::ResultSetManager;
ed28f830 2use strict;
bf5ecff9 3use warnings;
ed28f830 4use base 'DBIx::Class';
5use Class::Inspector;
6
75d07914 7=head1 NAME
bc0c9800 8
9 DBIx::Class::ResultSetManager - helpful methods for managing
10 resultset classes (EXPERIMENTAL)
11
12=head1 SYNOPSIS
13
14 # in a table class
15 __PACKAGE__->load_components(qw/ResultSetManager Core/); # note order!
16 __PACKAGE__->load_resultset_components(qw/AlwaysRS/);
17
18 # will be removed from the table class and inserted into a
19 # table-specific resultset class
20 sub search_by_year_desc : ResultSet {
21 my $self = shift;
22 my $cond = shift;
23 my $attrs = shift || {};
24 $attrs->{order_by} = 'year DESC';
63e9e431 25 $self->search($cond, $attrs);
bc0c9800 26 }
27
28 $rs = $schema->resultset('CD')->search_by_year_desc({ artist => 'Tool' });
29
30=head1 DESCRIPTION
31
32This package implements two useful features for customizing resultset
33classes. C<load_resultset_components> loads components in addition to
34C<DBIx::Class::ResultSet> (or whatever you set as
35C<base_resultset_class>). Any methods tagged with the C<ResultSet>
36attribute will be moved into a table-specific resultset class (by
37default called C<Class::_resultset>, but configurable via
38C<table_resultset_class_suffix>). Most of the magic is done when you
39call C<< __PACKAGE__->table >>.
40
41=cut
42
24d67825 43__PACKAGE__->mk_classdata($_)
44 for qw/ base_resultset_class table_resultset_class_suffix /;
570783b1 45__PACKAGE__->base_resultset_class('DBIx::Class::ResultSet');
f0750722 46__PACKAGE__->table_resultset_class_suffix('::_resultset');
ed28f830 47
48sub table {
49 my ($self,@rest) = @_;
e8861f71 50 my $ret = $self->next::method(@rest);
51 if (@rest) {
52 $self->_register_attributes;
75d07914 53 $self->_register_resultset_class;
e8861f71 54 }
55 return $ret;
ed28f830 56}
57
58sub load_resultset_components {
59 my ($self,@comp) = @_;
60 my $resultset_class = $self->_setup_resultset_class;
61 $resultset_class->load_components(@comp);
62}
63
ed28f830 64sub _register_attributes {
65 my $self = shift;
66 my $cache = $self->_attr_cache;
da95b45f 67 return if keys %$cache == 0;
68
ed28f830 69 foreach my $meth (@{Class::Inspector->methods($self) || []}) {
70 my $attrs = $cache->{$self->can($meth)};
71 next unless $attrs;
a39e84a3 72 if ($attrs->[0] eq 'ResultSet') {
ed28f830 73 no strict 'refs';
74 my $resultset_class = $self->_setup_resultset_class;
e250c046 75 *{"$resultset_class\::$meth"} = $self->can($meth);
f8d97a01 76 delete ${"${self}::"}{$meth};
ed28f830 77 }
78 }
ed28f830 79}
80
81sub _setup_resultset_class {
82 my $self = shift;
f0750722 83 my $resultset_class = $self . $self->table_resultset_class_suffix;
ed28f830 84 no strict 'refs';
85 unless (@{"$resultset_class\::ISA"}) {
570783b1 86 @{"$resultset_class\::ISA"} = ($self->base_resultset_class);
ed28f830 87 }
88 return $resultset_class;
89}
90
570783b1 91sub _register_resultset_class {
92 my $self = shift;
f0750722 93 my $resultset_class = $self . $self->table_resultset_class_suffix;
570783b1 94 no strict 'refs';
95 if (@{"$resultset_class\::ISA"}) {
24d67825 96 $self->result_source_instance->resultset_class($resultset_class);
570783b1 97 } else {
24d67825 98 $self->result_source_instance->resultset_class
75d07914 99 ($self->base_resultset_class);
570783b1 100 }
101}
102
19345968 1031;
104
19345968 105=head1 AUTHORS
106
107David Kamholz <dkamholz@cpan.org>
108
109=head1 LICENSE
110
111You may distribute this code under the same terms as Perl itself.
112
113=cut