1 package DBIx::Class::Schema::ResultSetAccessors;
7 use Lingua::EN::Inflect::Phrase;
8 use Sub::Name 'subname';
10 our $VERSION = 0.001000;
15 my $next = $self->next::method(@_);
16 my $schema = ref($self) || $self;
19 exists $self->resultset_accessor_map->{$moniker}
20 ? $self->resultset_accessor_map->{$moniker}
21 : $self->resultset_accessor_name($moniker);
23 if ($schema->can($accessor_name)) {
24 $self->throw_exception(
25 "Can't create ResultSet accessor '$accessor_name'. " .
26 "Schema method with the same name already exists. " .
27 "Try overloading the name via resultset_accessor_map."
33 no warnings 'redefine';
34 *{"${schema}::${accessor_name}"} = subname "${schema}::${accessor_name}"
35 => sub { shift->resultset($moniker) };
41 sub resultset_accessor_map {
45 sub resultset_accessor_name {
46 my ($self, $moniker) = @_;
48 return $self->pluralize_resultset_accessor_name(
49 String::CamelCase::decamelize($moniker)
53 sub pluralize_resultset_accessor_name {
54 my ($self, $original) = @_;
56 return join '_', split /\s+/,
57 Lingua::EN::Inflect::Phrase::to_PL(join ' ', split /_/, $original);
66 DBIx::Class::Schema::ResultSetAccessors - Short hand ResultSet Accessors
71 my $schema = MyApp::Schema->connect(...);
73 @artists = $schema->artists->all; # same as $schema->resultset('Artist')->all;
77 Creates short hand accessor methods for each ResultSet. Accessor names are
78 properly converted into lowercase and pluralized. E.g.
80 LinerNote -> liner_notes
86 =head2 resultset_accessor_map
88 Sometimes you will not want to, or will not be able to use an auto-generated
89 accessor name. A common case would be when the accessor name conflicts with a
90 built in DBIx::Class::Schema method. E.g. if you name your Result class
91 "Source", a pluralized version of this would be "sources", which is a built in
94 This method allows you to redefine the names as you wish. Overload this method
95 in your schema class and return a hashref map of Source => accessor names. E.g.:
97 # in your MyApp::Schema class
98 sub resultset_accessor_map {
100 Source => 'my_source',
101 Artist => 'my_artists',
106 $schema->my_source->all;
108 =head2 resultset_accessor_name($moniker)
110 This method is used to generate the accessor names. If you wish to create your
111 own logic for generating the name, you can overload this method. The method
112 takes a moniker (aka Source name) as a parameter and returns the accessor name.
114 Internally it simply uses L<String::CamelCase> to decamelize the name and pass
115 it to L</pluralize_resultset_accessor_name> method.
117 =head2 pluralize_resultset_accessor_name($decamelized_name)
119 If you only wish to overload the pluralization of the accessor name, in case you
120 want to add support for a language other than English, then you might only want
121 to overload this method. The method accepts decamelized name (e.g. liner_note)
122 and returns properly pluralized version of it.
130 =item L<String::CamelCase>
132 =item L<Lingua::EN::Inflect::Phrase>
143 Copyright (c) 2011 Roman F.
145 This program is free software; you can redistribute
146 it and/or modify it under the same terms as Perl itself.
148 The full text of the license can be found in the
149 LICENSE file included with this module.