1 package DBIx::Class::Schema::ResultSetAccessors;
6 use base 'DBIx::Class::AccessorGroup';
8 use DBIx::Class::Carp qw(carp);
10 use Lingua::EN::Inflect::Phrase;
11 use Sub::Name 'subname';
14 our $VERSION = 0.001005;
16 __PACKAGE__->mk_group_accessors(inherited => qw/
17 resultset_accessor_map
18 _track_resultset_accessors
20 __PACKAGE__->resultset_accessor_map({});
21 __PACKAGE__->_track_resultset_accessors({});
26 my $next = $self->next::method(@_);
27 my $schema = ref($self) || $self;
29 # need to track if we already ran register_source once, because
30 # it might be re-run in sub-class, like in the case of
31 # Catalyst::Model::DBIC::Schema via compose_namespaces
33 exists $self->_track_resultset_accessors->{$schema}{$moniker};
36 exists $self->resultset_accessor_map->{$moniker}
37 ? $self->resultset_accessor_map->{$moniker}
38 : $self->resultset_accessor_name($moniker);
40 if ($schema->can($accessor_name)) {
42 "Can't create ResultSet accessor '$accessor_name'. " .
43 "Schema method with the same name already exists. " .
44 "Try overloading the name via resultset_accessor_map."
50 no warnings 'redefine';
51 *{"${schema}::${accessor_name}"} = subname "${schema}::${accessor_name}"
52 => sub { shift->resultset($moniker) };
55 $self->_track_resultset_accessors->{$schema}{$moniker} = 1;
60 sub resultset_accessor_name {
61 my ($self, $moniker) = @_;
63 return $self->pluralize_resultset_accessor_name(
64 String::CamelCase::decamelize($moniker)
68 sub pluralize_resultset_accessor_name {
69 my ($self, $original) = @_;
71 return join '_', split /\s+/,
72 Lingua::EN::Inflect::Phrase::to_PL(join ' ', split /_/, $original);
81 DBIx::Class::Schema::ResultSetAccessors - Short hand ResultSet Accessors
85 # in your schema class
86 __PACKAGE__->load_components(qw/
87 Schema::ResultSetAccessors
89 __PACKAGE__->load_namespaces;
93 my $schema = MyApp::Schema->connect(...);
94 @artists = $schema->artists->all; # same as $schema->resultset('Artist')->all;
98 Creates short hand accessor methods for each ResultSet. Accessor names are
99 properly converted into lowercase and pluralized. E.g.
101 LinerNote -> liner_notes
107 =head2 resultset_accessor_map
109 Sometimes you will not want to, or will not be able to use an auto-generated
110 accessor name. A common case would be when the accessor name conflicts with a
111 built in DBIx::Class::Schema method. E.g. if you name your Result class
112 "Source", a pluralized version of this would be "sources", which is a built in
115 This method allows you to redefine the names as you wish. Overload this method
116 in your schema class and return a hashref map of Source => accessor names. E.g.:
118 # in your MyApp::Schema class
119 sub resultset_accessor_map {
121 Source => 'my_sources',
122 Artist => 'my_artists',
127 $schema->my_sources->all;
129 =head2 resultset_accessor_name($moniker)
131 This method is used to generate the accessor names. If you wish to create your
132 own logic for generating the name, you can overload this method. The method
133 takes a moniker (aka Source name) as a parameter and returns the accessor name.
135 Internally it simply uses L<String::CamelCase> to decamelize the name and pass
136 it to L</pluralize_resultset_accessor_name> method.
138 =head2 pluralize_resultset_accessor_name($decamelized_name)
140 If you only wish to overload the pluralization of the accessor name, in case you
141 want to add support for a language other than English, then you might only want
142 to overload this method. The method accepts decamelized name (e.g. liner_note)
143 and returns properly pluralized version of it.
151 =item L<String::CamelCase>
153 =item L<Lingua::EN::Inflect::Phrase>
164 Copyright (c) 2011 Roman F.
166 This program is free software; you can redistribute
167 it and/or modify it under the same terms as Perl itself.
169 The full text of the license can be found in the
170 LICENSE file included with this module.