Helper/Row/JoinTable.pm

filter: Statementstmt 95.3% 82 / 86 Branchbran 62.5% 5 / 8 Conditioncond 61.9% 13 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 100% 17 / 17 Podpod 100% 7 / 7 totaltotal 81.0% 124 / 153 CC 14 SCAR 14.7
CC
14
Cov
87%
_defaults
9.2
_add_join_column
6.4
_pluralize
2.0
CRAP 4.3
« Helper/Row/ProxyResultSetUpdate.pm ↑ Summary Helper/ResultSet/DateMethods1.pm »
1 package DBIx::Class::Helper::Row::JoinTable;
2 54 $DBIx::Class::Helper::Row::JoinTable::VERSION = '2.032002';
3 # ABSTRACT: Easily set up join tables with DBIx::Class
4  
5 54 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 54 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 54 ▶use parent 'DBIx::Class::Row';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 54 ▶use DBIx::Class::Helpers::Util 'get_namespace_parts';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 54 ▶use Lingua::EN::Inflect ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 54 ▶use String::CamelCase ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 54 ▶use DBIx::Class::Candy::Exports;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 54 export_methods [qw(
16 join_table
17 generate_primary_key
18 generate_has_manys
19 generate_many_to_manys
20 generate_relationships
21 set_table
22 add_join_columns
23 )];
24  
25 sub _pluralize {
26 216 ▶ my $self = shift;
Subroutine _pluralize: called CC=2 SCAR=7.1
27 216 ▶ my $original = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $original = shift() or return 0 216 216
28 216 return join q{_}, split /\s+/,
29 Lingua::EN::Inflect::PL(join q{ }, split /_/, $original);
30 }
31  
32 sub _defaults {
33 432 ▶ my ($self, $params) = @_;
Subroutine _defaults: called CC=8 SCAR=22.2
34  
35 432 ▶ $params->{namespace} ||= [ get_namespace_parts($self) ]->[0];
Condition66%2/3
$params->{"namespace"} ||= [get_namespace_parts($self)]->[0]
l!l&&r!l&&!r
3241080
MC/DC0%0/2
$params->{"namespace"} ||= [get_namespace_parts($self)]->[0]
$params->{"namespace"} [get_namespace_parts($self)]->[0]
Truth table66%2/3
A ||= B
A= $params->{"namespace"}B= [get_namespace_parts($self)]->[0]
ABresult
1X1
011
000
36 432 ▶ $params->{left_method} ||= String::CamelCase::decamelize($params->{left_class});
Condition66%2/3
$params->{"left_method"} ||= String::CamelCase::decamelize($params->{"left_class"})
l!l&&r!l&&!r
378540
MC/DC0%0/2
$params->{"left_method"} ||= String::CamelCase::decamelize($params->{"left_class"})
$params->{"left_method"} String::CamelCase::decamelize($params->{"left_class"})
Truth table66%2/3
A ||= B
A= $params->{"left_method"}B= String::CamelCase::decamelize($params->{"left_class"})
ABresult
1X1
011
000
37 432 ▶ $params->{right_method} ||= String::CamelCase::decamelize($params->{right_class});
Condition33%1/3
$params->{"right_method"} ||= String::CamelCase::decamelize($params->{"right_class"})
l!l&&r!l&&!r
43200
MC/DC0%0/2
$params->{"right_method"} ||= String::CamelCase::decamelize($params->{"right_class"})
$params->{"right_method"} String::CamelCase::decamelize($params->{"right_class"})
Truth table33%1/3
A ||= B
A= $params->{"right_method"}B= String::CamelCase::decamelize($params->{"right_class"})
ABresult
1X1
011
000
38 432 ▶ $params->{self_method} ||= String::CamelCase::decamelize($self);
Condition66%2/3
$params->{"self_method"} ||= String::CamelCase::decamelize($self)
l!l&&r!l&&!r
378540
MC/DC0%0/2
$params->{"self_method"} ||= String::CamelCase::decamelize($self)
$params->{"self_method"} String::CamelCase::decamelize($self)
Truth table66%2/3
A ||= B
A= $params->{"self_method"}B= String::CamelCase::decamelize($self)
ABresult
1X1
011
000
39 432 ▶ $params->{left_method_plural} ||= $self->_pluralize($params->{left_method});
Condition66%2/3
$params->{"left_method_plural"} ||= $self->_pluralize($params->{"left_method"})
l!l&&r!l&&!r
378540
MC/DC0%0/2
$params->{"left_method_plural"} ||= $self->_pluralize($params->{"left_method"})
$params->{"left_method_plural"} $self->_pluralize($params->{"left_method"})
Truth table66%2/3
A ||= B
A= $params->{"left_method_plural"}B= $self->_pluralize($params->{"left_method"})
ABresult
1X1
011
000
40 432 ▶ $params->{right_method_plural} ||= $self->_pluralize($params->{right_method});
Condition66%2/3
$params->{"right_method_plural"} ||= $self->_pluralize($params->{"right_method"})
l!l&&r!l&&!r
378540
MC/DC0%0/2
$params->{"right_method_plural"} ||= $self->_pluralize($params->{"right_method"})
$params->{"right_method_plural"} $self->_pluralize($params->{"right_method"})
Truth table66%2/3
A ||= B
A= $params->{"right_method_plural"}B= $self->_pluralize($params->{"right_method"})
ABresult
1X1
011
000
41 432 ▶ $params->{self_method_plural} ||= $self->_pluralize($params->{self_method});
Condition66%2/3
$params->{"self_method_plural"} ||= $self->_pluralize($params->{"self_method"})
l!l&&r!l&&!r
3241080
MC/DC0%0/2
$params->{"self_method_plural"} ||= $self->_pluralize($params->{"self_method"})
$params->{"self_method_plural"} $self->_pluralize($params->{"self_method"})
Truth table66%2/3
A ||= B
A= $params->{"self_method_plural"}B= $self->_pluralize($params->{"self_method"})
ABresult
1X1
011
000
42  
43 432 return $params;
44 }
45  
46 sub join_table {
47 108 ▶ my ($self, $params) = @_;
Subroutine join_table: called CC=1 SCAR=0.0
Pod documented
48  
49 108 $self->set_table($params);
50 108 $self->add_join_columns($params);
51 108 $self->generate_relationships($params);
52 108 $self->generate_primary_key($params);
53 }
54  
55 sub generate_primary_key {
56 108 ▶ my ($self, $params) = @_;
Subroutine generate_primary_key: called CC=1 SCAR=0.0
Pod documented
57  
58 108 $self->_defaults($params);
59 108 $self->set_primary_key("$params->{left_method}_id", "$params->{right_method}_id");
60 }
61  
62 sub generate_has_manys {
63 54 ▶ my ($self, $params) = @_;
Subroutine generate_has_manys: called CC=1 SCAR=0.0
Pod documented
64  
65 54 $params = $self->_defaults($params);
66 "$params->{namespace}::$params->{left_class}"->has_many(
67 $params->{self_method} =>
68 54 $self,
69 "$params->{left_method}_id"
70 );
71  
72 "$params->{namespace}::$params->{right_class}"->has_many(
73 $params->{self_method} =>
74 54 $self,
75 "$params->{right_method}_id"
76 );
77 }
78  
79 sub generate_many_to_manys {
80 54 ▶ my ($self, $params) = @_;
Subroutine generate_many_to_manys: called CC=1 SCAR=0.0
Pod documented
81 54 $params = $self->_defaults($params);
82  
83 "$params->{namespace}::$params->{left_class}"->many_to_many(
84 $params->{right_method_plural} =>
85 $params->{self_method},
86 $params->{right_method}
87 54 );
88  
89 "$params->{namespace}::$params->{right_class}"->many_to_many(
90 $params->{left_method_plural} =>
91 $params->{self_method},
92 $params->{left_method}
93 54 );
94 }
95  
96 sub generate_relationships {
97 108 ▶ my ($self, $params) = @_;
Subroutine generate_relationships: called CC=1 SCAR=0.0
Pod documented
98  
99 108 $params = $self->_defaults($params);
100 $self->belongs_to(
101 $params->{left_method} =>
102 108 "$params->{namespace}::$params->{left_class}",
103 "$params->{left_method}_id"
104 );
105 $self->belongs_to(
106 $params->{right_method} =>
107 108 "$params->{namespace}::$params->{right_class}",
108 "$params->{right_method}_id"
109 );
110 }
111  
112 sub set_table {
113 108 ▶ my ($self, $params) = @_;
Subroutine set_table: called CC=1 SCAR=0.0
Pod documented
114  
115 108 $self->table("$params->{left_class}_$params->{right_class}");
116 }
117  
118 sub _add_join_column {
119 216 ▶ my ($self, $params) = @_;
Subroutine _add_join_column: called CC=6 SCAR=18.5
120  
121 216 my $class = $params->{class};
122 216 my $method = $params->{method};
123  
124 216 my $default = {
125 data_type => 'integer',
126 is_nullable => 0,
127 is_numeric => 1,
128 };
129  
130 216 $self->ensure_class_loaded($class);
131 216 my @datas = qw{is_nullable extra data_type size is_numeric};
132  
133 my @class_column_info = (
134 map {
135 216 my $info = $class->column_info($_);
136 216 my $result = {};
137 216 my $defined = undef;
138 216 for (@datas) {
139 1080 ▶ if (defined $info->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $info->{$_}) 324 756 1080
140 324 $defined = 1;
141 324 $result->{$_} = $info->{$_};
142 }
143 }
144 216 ▶ $result = $default unless $defined;
Branch: 1 branch
BranchTrueFalseTotal
unless $defined 0 216 216
145 216 $result;
146 } $class->primary_columns
147 );
148  
149 216 ▶ if (@class_column_info == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@class_column_info == 1) { } 216 0 216
150 216 $self->add_columns(
151 "${method}_id" => $class_column_info[0],
152 );
153 } else {
154 0 my $i = 0;
155 0 for (@class_column_info) {
156 0 $i++;
157 0 $self->add_columns(
158 "${method}_${i}_id" => $_
159 );
160 }
161 }
162 }
163  
164 sub add_join_columns {
165 108 ▶ my ($self, $params) = @_;
Subroutine add_join_columns: called CC=1 SCAR=0.0
Pod documented
166  
167 108 $params = $self->_defaults($params);
168  
169 108 my $l_class = "$params->{namespace}::$params->{left_class}";
170 108 my $r_class = "$params->{namespace}::$params->{right_class}";
171  
172 $self->_add_join_column({
173 class => $l_class,
174 method => $params->{left_method}
175 108 });
176  
177 $self->_add_join_column({
178 class => $r_class,
179 method => $params->{right_method}
180 108 });
181 }
182  
183 54 1;
184  
185 __END__
186  
187 =pod
188  
189 =head1 NAME
190  
191 DBIx::Class::Helper::Row::JoinTable - Easily set up join tables with DBIx::Class
192  
193 =head1 SYNOPSIS
194  
195 package MyApp::Schema::Result::Foo_Bar;
196  
197 __PACKAGE__->load_components(qw{Helper::Row::JoinTable Core});
198  
199 __PACKAGE__->join_table({
200 left_class => 'Foo',
201 left_method => 'foo',
202 right_class => 'Bar',
203 right_method => 'bar',
204 });
205  
206 # the above is the same as:
207  
208 __PACKAGE__->table('Foo_Bar');
209 __PACKAGE__->add_columns(
210 foo_id => {
211 data_type => 'integer',
212 is_nullable => 0,
213 is_numeric => 1,
214 },
215 bar_id => {
216 data_type => 'integer',
217 is_nullable => 0,
218 is_numeric => 1,
219 },
220 );
221  
222 $self->set_primary_key(qw{foo_id bar_id});
223  
224 __PACKAGE__->belongs_to( foo => 'MyApp::Schema::Result::Foo' 'foo_id');
225 __PACKAGE__->belongs_to( bar => 'MyApp::Schema::Result::Bar' 'bar_id');
226  
227 or with L<DBIx::Class::Candy>:
228  
229 package MyApp::Schema::Result::Foo_Bar;
230  
231 use DBIx::Class::Candy -components => ['Helper::Row::JoinTable'];
232  
233 join_table {
234 left_class => 'Foo',
235 left_method => 'foo',
236 right_class => 'Bar',
237 right_method => 'bar',
238 };
239  
240 =head1 METHODS
241  
242 All the methods take a configuration hashref that looks like the following:
243  
244 {
245 left_class => 'Foo',
246 left_method => 'foo', # see NOTE
247 left_method_plural => 'foos', # see NOTE, not required, used for
248 # many_to_many rel name in right_class
249 # which is not generated by default
250 right_class => 'Bar',
251 right_method => 'bar', # see NOTE
252 right_method_plural => 'bars', # see NOTE, not required, used for
253 # many_to_many rel name in left_class
254 # which is not generated by default
255 namespace => 'MyApp', # default is guessed via *::Foo
256 self_method => 'foobars', # not required, used for setting the name of the
257 # join table's relationship in a has_many
258 # which is not generated by default
259 }
260  
261 =head2 join_table
262  
263 This is the method that you probably want. It will set your table, add
264 columns, set the primary key, and set up the relationships.
265  
266 =head2 add_join_columns
267  
268 Adds two non-nullable integer fields named C<"${left_method}_id"> and
269 C<"${right_method}_id"> respectively.
270  
271 =head2 generate_has_manys
272  
273 Installs methods into C<left_class> and C<right_class> to get to the join table.
274 The methods will be named what's passed into the configuration hashref as
275 C<self_method>.
276  
277 =head2 generate_many_to_manys
278  
279 Installs many_to_many methods into C<left_class> and C<right_class>. The
280 methods will be named what's passed into the configuration hashref as
281 C<left_method_plural> for the C<right_class> and C<right_method_plural> for the
282 C<left_class>.
283  
284 =head2 generate_primary_key
285  
286 Sets C<"${left_method}_id"> and C<"${right_method}_id"> to be the primary key.
287  
288 =head2 generate_relationships
289  
290 This adds relationships to C<"${namespace}::Schema::Result::$left_class"> and
291 C<"${namespace}::Schema::Result::$left_class"> respectively.
292  
293 =head2 set_table
294  
295 This method sets the table to "${left_class}_${right_class}".
296  
297 =head1 CANDY EXPORTS
298  
299 If used in conjunction with L<DBIx::Class::Candy> this component will export:
300  
301 =over
302  
303 =item join_table
304  
305 =item generate_primary_key
306  
307 =item generate_has_manys
308  
309 =item generate_many_to_manys
310  
311 =item generate_relationships
312  
313 =item set_table
314  
315 =item add_join_columns
316  
317 =back
318  
319 =head2 NOTE
320  
321 This module uses L<String::CamelCase> to default the method names and uses
322 L<Lingua::EN::Inflect> for pluralization.
323  
324 =head1 CHANGES BETWEEN RELEASES
325  
326 =head2 Changes since 0.*
327  
328 Originally this module would use
329  
330 data_type => 'integer',
331 is_nullable => 0,
332 is_numeric => 1,
333  
334 for all joining columns. It now infers C<data_type>, C<is_nullable>,
335 C<is_numeric>, and C<extra> from the foreign tables.
336  
337 =head1 AUTHOR
338  
339 Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
340  
341 =head1 COPYRIGHT AND LICENSE
342  
343 This software is copyright (c) 2016 by Arthur Axel "fREW" Schmidt.
344  
345 This is free software; you can redistribute it and/or modify it under
346 the same terms as the Perl 5 programming language system itself.
347  
348 =cut
« Helper/Row/ProxyResultSetUpdate.pm ↑ Summary Helper/ResultSet/DateMethods1.pm »