1 package DBIx::Class::ResultSetManager;
3 use base 'DBIx::Class';
8 DBIx::Class::ResultSetManager - helpful methods for managing
9 resultset classes (EXPERIMENTAL)
14 __PACKAGE__->load_components(qw/ResultSetManager Core/); # note order!
15 __PACKAGE__->load_resultset_components(qw/AlwaysRS/);
17 # will be removed from the table class and inserted into a
18 # table-specific resultset class
19 sub search_by_year_desc : ResultSet {
22 my $attrs = shift || {};
23 $attrs->{order_by} = 'year DESC';
24 $self->next::method($cond, $attrs);
27 $rs = $schema->resultset('CD')->search_by_year_desc({ artist => 'Tool' });
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 >>.
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');
48 my ($self,@rest) = @_;
49 my $ret = $self->next::method(@rest);
51 $self->_register_attributes;
52 $self->_register_resultset_class;
57 sub load_resultset_components {
58 my ($self,@comp) = @_;
59 my $resultset_class = $self->_setup_resultset_class;
60 $resultset_class->load_components(@comp);
63 sub _register_attributes {
65 my $cache = $self->_attr_cache;
66 return if keys %$cache == 0;
68 foreach my $meth (@{Class::Inspector->methods($self) || []}) {
69 my $attrs = $cache->{$self->can($meth)};
71 if ($attrs->[0] eq 'ResultSet') {
73 my $resultset_class = $self->_setup_resultset_class;
74 *{"$resultset_class\::$meth"} = $self->can($meth);
75 delete ${"${self}::"}{$meth};
80 sub _setup_resultset_class {
82 my $resultset_class = $self . $self->table_resultset_class_suffix;
84 unless (@{"$resultset_class\::ISA"}) {
85 @{"$resultset_class\::ISA"} = ($self->base_resultset_class);
87 return $resultset_class;
90 sub _register_resultset_class {
92 my $resultset_class = $self . $self->table_resultset_class_suffix;
94 if (@{"$resultset_class\::ISA"}) {
95 $self->result_source_instance->resultset_class($resultset_class);
97 $self->result_source_instance->resultset_class
98 ($self->base_resultset_class);
106 David Kamholz <dkamholz@cpan.org>
110 You may distribute this code under the same terms as Perl itself.