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