78461c9047d762653f3f6136b015bb7d2aa49b5d
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultSetManager.pm
1 package DBIx::Class::ResultSetManager;
2 use strict;
3 use warnings;
4 use base 'DBIx::Class';
5 use Class::Inspector;
6
7 =head1 NAME
8
9 DBIx::Class::ResultSetManager - helpful methods for managing resultset
10 classes (EXPERIMENTAL)
11
12 =head1 SYNOPSIS
13
14   # in a table class
15   __PACKAGE__->load_components(qw/ResultSetManager Core/); # note order!
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->search($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 =head2 table
48
49 Stacks on top of the normal L<DBIx::Class> C<table> method.  Any
50 methods tagged with the C<ResultSet> attribute will be moved into a
51 table-specific resultset class (by default called
52 C<Class::_resultset>, but configurable via
53 C<table_resultset_class_suffix>).  The magic for this is done within
54 this C<< __PACKAGE__->table >> call.
55
56 =cut
57
58 sub table {
59     my ($self,@rest) = @_;
60     my $ret = $self->next::method(@rest);
61     if (@rest) {
62         $self->_register_attributes;
63         $self->_register_resultset_class;
64     }
65     return $ret;
66 }
67
68 =head2 load_resultset_components
69
70 C<load_resultset_components> loads components in addition to
71 C<DBIx::Class::ResultSet> (or whatever you set as
72 C<base_resultset_class>).
73
74 =cut
75
76 sub load_resultset_components {
77     my ($self,@comp) = @_;
78     my $resultset_class = $self->_setup_resultset_class;
79     $resultset_class->load_components(@comp);
80 }
81
82 sub _register_attributes {
83     my $self = shift;
84     my $cache = $self->_attr_cache;
85     return if keys %$cache == 0;
86
87     foreach my $meth (@{Class::Inspector->methods($self) || []}) {
88         my $attrs = $cache->{$self->can($meth)};
89         next unless $attrs;
90         if ($attrs->[0] eq 'ResultSet') {
91             no strict 'refs';
92             my $resultset_class = $self->_setup_resultset_class;
93             *{"$resultset_class\::$meth"} = $self->can($meth);
94             delete ${"${self}::"}{$meth};
95         }
96     }
97 }
98
99 sub _setup_resultset_class {
100     my $self = shift;
101     my $resultset_class = $self . $self->table_resultset_class_suffix;
102     no strict 'refs';
103     unless (@{"$resultset_class\::ISA"}) {
104         @{"$resultset_class\::ISA"} = ($self->base_resultset_class);
105     }
106     return $resultset_class;
107 }
108
109 sub _register_resultset_class {
110     my $self = shift;
111     my $resultset_class = $self . $self->table_resultset_class_suffix;
112     no strict 'refs';
113     if (@{"$resultset_class\::ISA"}) {
114         $self->result_source_instance->resultset_class($resultset_class);
115     } else {
116         $self->result_source_instance->resultset_class
117           ($self->base_resultset_class);
118     }
119 }
120
121 1;
122
123 =head1 AUTHORS
124
125 David Kamholz <dkamholz@cpan.org>
126
127 =head1 LICENSE
128
129 You may distribute this code under the same terms as Perl itself.
130
131 =cut