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