Commit | Line | Data |
db57afbb |
1 | package DBIx::Class::ResultSetManager; |
ed28f830 |
2 | use strict; |
3 | use base 'DBIx::Class'; |
4 | use Class::Inspector; |
5 | |
bc0c9800 |
6 | =head1 NAME |
7 | |
8 | DBIx::Class::ResultSetManager - helpful methods for managing |
9 | resultset classes (EXPERIMENTAL) |
10 | |
11 | =head1 SYNOPSIS |
12 | |
13 | # in a table class |
14 | __PACKAGE__->load_components(qw/ResultSetManager Core/); # note order! |
15 | __PACKAGE__->load_resultset_components(qw/AlwaysRS/); |
16 | |
17 | # will be removed from the table class and inserted into a |
18 | # table-specific resultset class |
19 | sub search_by_year_desc : ResultSet { |
20 | my $self = shift; |
21 | my $cond = shift; |
22 | my $attrs = shift || {}; |
23 | $attrs->{order_by} = 'year DESC'; |
24 | $self->next::method($cond, $attrs); |
25 | } |
26 | |
27 | $rs = $schema->resultset('CD')->search_by_year_desc({ artist => 'Tool' }); |
28 | |
29 | =head1 DESCRIPTION |
30 | |
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 >>. |
39 | |
40 | =cut |
41 | |
24d67825 |
42 | __PACKAGE__->mk_classdata($_) |
43 | for qw/ base_resultset_class table_resultset_class_suffix /; |
570783b1 |
44 | __PACKAGE__->base_resultset_class('DBIx::Class::ResultSet'); |
f0750722 |
45 | __PACKAGE__->table_resultset_class_suffix('::_resultset'); |
ed28f830 |
46 | |
47 | sub table { |
48 | my ($self,@rest) = @_; |
e8861f71 |
49 | my $ret = $self->next::method(@rest); |
50 | if (@rest) { |
51 | $self->_register_attributes; |
52 | $self->_register_resultset_class; |
53 | } |
54 | return $ret; |
ed28f830 |
55 | } |
56 | |
57 | sub load_resultset_components { |
58 | my ($self,@comp) = @_; |
59 | my $resultset_class = $self->_setup_resultset_class; |
60 | $resultset_class->load_components(@comp); |
61 | } |
62 | |
ed28f830 |
63 | sub _register_attributes { |
64 | my $self = shift; |
65 | my $cache = $self->_attr_cache; |
da95b45f |
66 | return if keys %$cache == 0; |
67 | |
ed28f830 |
68 | foreach my $meth (@{Class::Inspector->methods($self) || []}) { |
69 | my $attrs = $cache->{$self->can($meth)}; |
70 | next unless $attrs; |
a39e84a3 |
71 | if ($attrs->[0] eq 'ResultSet') { |
ed28f830 |
72 | no strict 'refs'; |
73 | my $resultset_class = $self->_setup_resultset_class; |
e250c046 |
74 | *{"$resultset_class\::$meth"} = $self->can($meth); |
f8d97a01 |
75 | delete ${"${self}::"}{$meth}; |
ed28f830 |
76 | } |
77 | } |
ed28f830 |
78 | } |
79 | |
80 | sub _setup_resultset_class { |
81 | my $self = shift; |
f0750722 |
82 | my $resultset_class = $self . $self->table_resultset_class_suffix; |
ed28f830 |
83 | no strict 'refs'; |
84 | unless (@{"$resultset_class\::ISA"}) { |
570783b1 |
85 | @{"$resultset_class\::ISA"} = ($self->base_resultset_class); |
ed28f830 |
86 | } |
87 | return $resultset_class; |
88 | } |
89 | |
570783b1 |
90 | sub _register_resultset_class { |
91 | my $self = shift; |
f0750722 |
92 | my $resultset_class = $self . $self->table_resultset_class_suffix; |
570783b1 |
93 | no strict 'refs'; |
94 | if (@{"$resultset_class\::ISA"}) { |
24d67825 |
95 | $self->result_source_instance->resultset_class($resultset_class); |
570783b1 |
96 | } else { |
24d67825 |
97 | $self->result_source_instance->resultset_class |
98 | ($self->base_resultset_class); |
570783b1 |
99 | } |
100 | } |
101 | |
19345968 |
102 | 1; |
103 | |
19345968 |
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 |