1 package DBIx::Class::ResultSetManager;
4 use base 'DBIx::Class';
9 DBIx::Class::ResultSetManager - helpful methods for managing
10 resultset classes (EXPERIMENTAL)
15 __PACKAGE__->load_components(qw/ResultSetManager Core/); # note order!
16 __PACKAGE__->load_resultset_components(qw/AlwaysRS/);
18 # will be removed from the table class and inserted into a
19 # table-specific resultset class
20 sub search_by_year_desc : ResultSet {
23 my $attrs = shift || {};
24 $attrs->{order_by} = 'year DESC';
25 $self->search($cond, $attrs);
28 $rs = $schema->resultset('CD')->search_by_year_desc({ artist => 'Tool' });
32 This package implements two useful features for customizing resultset
33 classes. C<load_resultset_components> loads components in addition to
34 C<DBIx::Class::ResultSet> (or whatever you set as
35 C<base_resultset_class>). Any methods tagged with the C<ResultSet>
36 attribute will be moved into a table-specific resultset class (by
37 default called C<Class::_resultset>, but configurable via
38 C<table_resultset_class_suffix>). Most of the magic is done when you
39 call C<< __PACKAGE__->table >>.
43 __PACKAGE__->mk_classdata($_)
44 for qw/ base_resultset_class table_resultset_class_suffix /;
45 __PACKAGE__->base_resultset_class('DBIx::Class::ResultSet');
46 __PACKAGE__->table_resultset_class_suffix('::_resultset');
49 my ($self,@rest) = @_;
50 my $ret = $self->next::method(@rest);
52 $self->_register_attributes;
53 $self->_register_resultset_class;
58 sub load_resultset_components {
59 my ($self,@comp) = @_;
60 my $resultset_class = $self->_setup_resultset_class;
61 $resultset_class->load_components(@comp);
64 sub _register_attributes {
66 my $cache = $self->_attr_cache;
67 return if keys %$cache == 0;
69 foreach my $meth (@{Class::Inspector->methods($self) || []}) {
70 my $attrs = $cache->{$self->can($meth)};
72 if ($attrs->[0] eq 'ResultSet') {
74 my $resultset_class = $self->_setup_resultset_class;
75 *{"$resultset_class\::$meth"} = $self->can($meth);
76 delete ${"${self}::"}{$meth};
81 sub _setup_resultset_class {
83 my $resultset_class = $self . $self->table_resultset_class_suffix;
85 unless (@{"$resultset_class\::ISA"}) {
86 @{"$resultset_class\::ISA"} = ($self->base_resultset_class);
88 return $resultset_class;
91 sub _register_resultset_class {
93 my $resultset_class = $self . $self->table_resultset_class_suffix;
95 if (@{"$resultset_class\::ISA"}) {
96 $self->result_source_instance->resultset_class($resultset_class);
98 $self->result_source_instance->resultset_class
99 ($self->base_resultset_class);
107 David Kamholz <dkamholz@cpan.org>
111 You may distribute this code under the same terms as Perl itself.