Commit | Line | Data |
731e8b3e |
1 | package DBIx::Class::Schema::ResultSetAccessors; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use String::CamelCase; |
7 | use Lingua::EN::Inflect::Phrase; |
8 | use Sub::Name 'subname'; |
9 | |
10 | sub 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 | |
37 | sub resultset_accessor_map { |
38 | return {}; |
39 | } |
40 | |
41 | sub resultset_accessor_name { |
42 | my ($self, $moniker) = @_; |
43 | |
44 | return $self->pluralize_resultset_accessor_name( |
45 | String::CamelCase::decamelize($moniker) |
46 | ); |
47 | } |
48 | |
49 | sub 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 | |
57 | 1; # eof |
58 | |
59 | __END__ |
60 | |
61 | =head1 NAME |
62 | |
63 | DBIx::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 | |
74 | Creates short hand accessor methods for each ResultSet. Accessor names are |
75 | properly 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 | |
85 | Sometimes you will not want to, or will not be able to use an auto-generated |
86 | accessor name. A common case would be when the accessor name conflicts with a |
87 | built 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 |
89 | method. |
90 | |
91 | This method allows you to redefine the names as you wish. Overload this method |
92 | in 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 | |
107 | This method is used to generate the accessor names. If you wish to create your |
108 | own logic for generating the name, you can overload this method. The method |
109 | takes a moniker (aka Source name) as a parameter and returns the accessor name. |
110 | |
111 | Internally it simply uses L<String::CamelCase> to decamelize the name and pass |
112 | it to L</pluralize_resultset_accessor_name> method. |
113 | |
114 | =head2 pluralize_resultset_accessor_name($decamelized_name) |
115 | |
116 | If you only wish to overload the pluralization of the accessor name, in case you |
117 | want to add support for a language other than English, then you might only want |
118 | to overload this method. The method accepts decamelized name (e.g. liner_note) |
119 | and 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 | |
140 | Copyright (c) 2011 Roman F. |
141 | |
142 | This program is free software; you can redistribute |
143 | it and/or modify it under the same terms as Perl itself. |
144 | |
145 | The full text of the license can be found in the |
146 | LICENSE file included with this module. |
147 | |
148 | =cut |