1 package DBIx::Class::ResultSetManager;
4 use base 'DBIx::Class';
9 DBIx::Class::ResultSetManager - helpful methods for managing resultset
10 classes (EXPERIMENTAL)
15 __PACKAGE__->load_components(qw/ResultSetManager Core/); # note order!
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->search($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');
49 Stacks on top of the normal L<DBIx::Class> C<table> method. Any
50 methods tagged with the C<ResultSet> attribute will be moved into a
51 table-specific resultset class (by default called
52 C<Class::_resultset>, but configurable via
53 C<table_resultset_class_suffix>). The magic for this is done within
54 this C<< __PACKAGE__->table >> call.
59 my ($self,@rest) = @_;
60 my $ret = $self->next::method(@rest);
62 $self->_register_attributes;
63 $self->_register_resultset_class;
68 =head2 load_resultset_components
70 C<load_resultset_components> loads components in addition to
71 C<DBIx::Class::ResultSet> (or whatever you set as
72 C<base_resultset_class>).
76 sub load_resultset_components {
77 my ($self,@comp) = @_;
78 my $resultset_class = $self->_setup_resultset_class;
79 $resultset_class->load_components(@comp);
82 sub _register_attributes {
84 my $cache = $self->_attr_cache;
85 return if keys %$cache == 0;
87 foreach my $meth (@{Class::Inspector->methods($self) || []}) {
88 my $attrs = $cache->{$self->can($meth)};
90 if ($attrs->[0] eq 'ResultSet') {
92 my $resultset_class = $self->_setup_resultset_class;
93 *{"$resultset_class\::$meth"} = $self->can($meth);
94 delete ${"${self}::"}{$meth};
99 sub _setup_resultset_class {
101 my $resultset_class = $self . $self->table_resultset_class_suffix;
103 unless (@{"$resultset_class\::ISA"}) {
104 @{"$resultset_class\::ISA"} = ($self->base_resultset_class);
106 return $resultset_class;
109 sub _register_resultset_class {
111 my $resultset_class = $self . $self->table_resultset_class_suffix;
113 if (@{"$resultset_class\::ISA"}) {
114 $self->result_source_instance->resultset_class($resultset_class);
116 $self->result_source_instance->resultset_class
117 ($self->base_resultset_class);
125 David Kamholz <dkamholz@cpan.org>
129 You may distribute this code under the same terms as Perl itself.