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