Initial commit
[dbsrgits/DBIx-Class-Schema-ResultSetAccessors.git] / lib / DBIx / Class / Schema / ResultSetAccessors.pm
CommitLineData
731e8b3e 1package DBIx::Class::Schema::ResultSetAccessors;
2
3use strict;
4use warnings;
5
6use String::CamelCase;
7use Lingua::EN::Inflect::Phrase;
8use Sub::Name 'subname';
9
10sub register_source {
11 my $self = shift;
12 my $moniker = $_[0];
13 my $next = $self->next::method(@_);
14
15 my $schema = ref($self) || $self;
16 my $accessor_name = exists $self->resultset_accessor_map->{$moniker}
17 ? $self->resultset_accessor_map->{$moniker}
18 : $self->resultset_accessor_name($moniker);
19 if ($schema->can($accessor_name)) {
20 $self->throw_exception(
21 "Can't create ResultSet accessor '$accessor_name'. " .
22 "Schema method with the same name already exists. " .
23 "Try overloading the name in resultset_accessor_map."
24 );
25 }
26
27 {
28 no strict 'refs';
29 no warnings 'redefine';
30 *{"${schema}::${accessor_name}"} = subname "${schema}::${accessor_name}"
31 => sub { shift->resultset($moniker) };
32 }
33
34 return $next;
35}
36
37sub resultset_accessor_map {
38 return {};
39}
40
41sub resultset_accessor_name {
42 my ($self, $moniker) = @_;
43
44 return $self->pluralize_resultset_accessor_name(
45 String::CamelCase::decamelize($moniker)
46 );
47}
48
49sub pluralize_resultset_accessor_name {
50 my $self = shift;
51 my $original = shift or return;
52
53 return join q{_}, split /\s+/,
54 Lingua::EN::Inflect::Phrase::to_PL(join q{ }, split /_/, $original);
55}
56
571; # eof
58
59__END__
60
61=head1 NAME
62
63DBIx::Class::Schema::ResultSetAccessors - Short hand ResultSet Accessors
64
65=head1 SYNOPSIS
66
67 use MyApp::Schema;
68 my $schema = MyApp::Schema->connect(...);
69
70 @artists = $schema->artists->all; # same as $schema->resultset('Artist')->all;
71
72=head1 DESCRIPTION
73
74Creates short hand accessor methods for each ResultSet. Accessor names are
75properly converted into lowercase and pluralized. E.g.
76
77 LinerNote -> liner_notes
78 Artist -> artists
79 CD -> cds
80
81=head1 METHODS
82
83=head2 resultset_accessor_map
84
85Sometimes you will not want to, or will not be able to use an auto-generated
86accessor name. A common case would be when the accessor name conflicts with a
87built in DBIx::Class::Schema method. E.g. if you name your Result class
88"Source", a pluralized version of this would be "sources", which is a built in
89method.
90
91This method allows you to redefine the names as you wish. Overload this method
92in your schema class and return a hashref map of Source => accessor names. E.g.:
93
94 # in your MyApp::Schema class
95 sub resultset_accessor_map {
96 {
97 Source => 'my_source',
98 Artist => 'my_artists',
99 }
100 }
101
102 # later in your code
103 $schema->my_source->all;
104
105=head2 resultset_accessor_name($moniker)
106
107This method is used to generate the accessor names. If you wish to create your
108own logic for generating the name, you can overload this method. The method
109takes a moniker (aka Source name) as a parameter and returns the accessor name.
110
111Internally it simply uses L<String::CamelCase> to decamelize the name and pass
112it to L</pluralize_resultset_accessor_name> method.
113
114=head2 pluralize_resultset_accessor_name($decamelized_name)
115
116If you only wish to overload the pluralization of the accessor name, in case you
117want to add support for a language other than English, then you might only want
118to overload this method. The method accepts decamelized name (e.g. liner_note)
119and returns properly pluralized version of it.
120
121=head1 SEE ALSO
122
123=over 4
124
125=item L<DBIx::Class>
126
127=item L<String::CamelCase>
128
129=item L<Lingua::EN::Inflect::Phrase>
130
131=back
132
133=head1 AUTHOR
134
135 Roman F.
136 romanf@cpan.org
137
138=head1 COPYRIGHT
139
140Copyright (c) 2011 Roman F.
141
142This program is free software; you can redistribute
143it and/or modify it under the same terms as Perl itself.
144
145The full text of the license can be found in the
146LICENSE file included with this module.
147
148=cut