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