1 package DBIx::Class::Schema::ResultSetAccessors;
6 use DBIx::Class::Carp qw(carp);
8 use Lingua::EN::Inflect::Phrase;
9 use Sub::Name 'subname';
11 our $VERSION = 0.001003;
16 my $next = $self->next::method(@_);
17 my $schema = ref($self) || $self;
20 exists $self->resultset_accessor_map->{$moniker}
21 ? $self->resultset_accessor_map->{$moniker}
22 : $self->resultset_accessor_name($moniker);
24 if ($schema->can($accessor_name)) {
26 "Can't create ResultSet accessor '$accessor_name'. " .
27 "Schema method with the same name already exists. " .
28 "Try overloading the name via resultset_accessor_map."
34 no warnings 'redefine';
35 *{"${schema}::${accessor_name}"} = subname "${schema}::${accessor_name}"
36 => sub { shift->resultset($moniker) };
42 sub resultset_accessor_map {
46 sub resultset_accessor_name {
47 my ($self, $moniker) = @_;
49 return $self->pluralize_resultset_accessor_name(
50 String::CamelCase::decamelize($moniker)
54 sub pluralize_resultset_accessor_name {
55 my ($self, $original) = @_;
57 return join '_', split /\s+/,
58 Lingua::EN::Inflect::Phrase::to_PL(join ' ', split /_/, $original);
67 DBIx::Class::Schema::ResultSetAccessors - Short hand ResultSet Accessors
72 my $schema = MyApp::Schema->connect(...);
74 @artists = $schema->artists->all; # same as $schema->resultset('Artist')->all;
78 Creates short hand accessor methods for each ResultSet. Accessor names are
79 properly converted into lowercase and pluralized. E.g.
81 LinerNote -> liner_notes
87 =head2 resultset_accessor_map
89 Sometimes you will not want to, or will not be able to use an auto-generated
90 accessor name. A common case would be when the accessor name conflicts with a
91 built in DBIx::Class::Schema method. E.g. if you name your Result class
92 "Source", a pluralized version of this would be "sources", which is a built in
95 This method allows you to redefine the names as you wish. Overload this method
96 in your schema class and return a hashref map of Source => accessor names. E.g.:
98 # in your MyApp::Schema class
99 sub resultset_accessor_map {
101 Source => 'my_sources',
102 Artist => 'my_artists',
107 $schema->my_source->all;
109 =head2 resultset_accessor_name($moniker)
111 This method is used to generate the accessor names. If you wish to create your
112 own logic for generating the name, you can overload this method. The method
113 takes a moniker (aka Source name) as a parameter and returns the accessor name.
115 Internally it simply uses L<String::CamelCase> to decamelize the name and pass
116 it to L</pluralize_resultset_accessor_name> method.
118 =head2 pluralize_resultset_accessor_name($decamelized_name)
120 If you only wish to overload the pluralization of the accessor name, in case you
121 want to add support for a language other than English, then you might only want
122 to overload this method. The method accepts decamelized name (e.g. liner_note)
123 and returns properly pluralized version of it.
131 =item L<String::CamelCase>
133 =item L<Lingua::EN::Inflect::Phrase>
144 Copyright (c) 2011 Roman F.
146 This program is free software; you can redistribute
147 it and/or modify it under the same terms as Perl itself.
149 The full text of the license can be found in the
150 LICENSE file included with this module.