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