Merge 'trunk' into 'DBIx-Class-current'
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultSetManager.pm
CommitLineData
db57afbb 1package DBIx::Class::ResultSetManager;
ed28f830 2use strict;
3use base 'DBIx::Class';
4use Class::Inspector;
5
f0750722 6__PACKAGE__->mk_classdata($_) for qw/ base_resultset_class table_resultset_class_suffix /;
570783b1 7__PACKAGE__->base_resultset_class('DBIx::Class::ResultSet');
f0750722 8__PACKAGE__->table_resultset_class_suffix('::_resultset');
ed28f830 9
10sub table {
11 my ($self,@rest) = @_;
e8861f71 12 my $ret = $self->next::method(@rest);
13 if (@rest) {
14 $self->_register_attributes;
15 $self->_register_resultset_class;
16 }
17 return $ret;
ed28f830 18}
19
20sub load_resultset_components {
21 my ($self,@comp) = @_;
22 my $resultset_class = $self->_setup_resultset_class;
23 $resultset_class->load_components(@comp);
24}
25
ed28f830 26sub _register_attributes {
27 my $self = shift;
49354ee3 28 return unless $self->can('_attr_cache');
29
ed28f830 30 my $cache = $self->_attr_cache;
31 foreach my $meth (@{Class::Inspector->methods($self) || []}) {
32 my $attrs = $cache->{$self->can($meth)};
33 next unless $attrs;
a39e84a3 34 if ($attrs->[0] eq 'ResultSet') {
ed28f830 35 no strict 'refs';
36 my $resultset_class = $self->_setup_resultset_class;
e250c046 37 *{"$resultset_class\::$meth"} = $self->can($meth);
f8d97a01 38 delete ${"${self}::"}{$meth};
ed28f830 39 }
40 }
ed28f830 41}
42
43sub _setup_resultset_class {
44 my $self = shift;
f0750722 45 my $resultset_class = $self . $self->table_resultset_class_suffix;
ed28f830 46 no strict 'refs';
47 unless (@{"$resultset_class\::ISA"}) {
570783b1 48 @{"$resultset_class\::ISA"} = ($self->base_resultset_class);
ed28f830 49 }
50 return $resultset_class;
51}
52
570783b1 53sub _register_resultset_class {
54 my $self = shift;
f0750722 55 my $resultset_class = $self . $self->table_resultset_class_suffix;
570783b1 56 no strict 'refs';
57 if (@{"$resultset_class\::ISA"}) {
58 $self->result_source_instance->resultset_class($resultset_class);
59 } else {
60 $self->result_source_instance->resultset_class($self->base_resultset_class);
61 }
62}
63
19345968 641;
65
66__END__
67
68=head1 NAME
69
70 DBIx::Class::ResultSetManager - helpful methods for managing resultset classes (EXPERIMENTAL)
71
72=head1 SYNOPSIS
73
74 # in a table class
75 __PACKAGE__->load_components(qw/ResultSetManager/);
76 __PACKAGE__->load_resultset_components(qw/AlwaysRS/);
77
78 # will be removed from the table class and inserted into a table-specific resultset class
9fa203be 79 sub foo : ResultSet { ... }
19345968 80
81=head1 DESCRIPTION
82
83This package implements two useful features for customizing resultset classes.
84C<load_resultset_components> loads components in addition to C<DBIx::Class::ResultSet>
a39e84a3 85(or whatever you set as C<base_resultset_class>). Any methods tagged with the C<ResultSet>
19345968 86attribute will be moved into a table-specific resultset class (by default called
87C<Class::_resultset>).
88
89=head1 AUTHORS
90
91David Kamholz <dkamholz@cpan.org>
92
93=head1 LICENSE
94
95You may distribute this code under the same terms as Perl itself.
96
97=cut