LookupColumn/Auto.pm

filter: Statementstmt 94.4% 85 / 90 Branchbran 82.1% 23 / 28 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 12 / 12 Podpod 100% 1 / 1 totaltotal 89.7% 122 / 136 CC 20 SCAR 18.3
CC
20
Cov
90%
add_lookups
10.0
_guess_field_name
7.1
_target2lookups
7.0
CRAP 6.2
↑ Summary LookupColumn/Manager.pm »
1 package DBIx::Class::LookupColumn::Auto;
2 {
3 10 $DBIx::Class::LookupColumn::Auto::VERSION = '0.10';
4 }
5  
6 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 DBIx::Class::LookupColumn::Auto - A dbic component for installing LookupColumn relations on a whole schema at once.
12  
13 =cut
14  
15  
16 10 ▶use base qw(DBIx::Class);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 10 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 10 ▶use Smart::Comments -ENV;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 10 ▶use Hash::Merge::Simple qw/merge/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 10 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 10 ▶use DBIx::Class::LookupColumn::LookupColumnComponent;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24  
25 =head1 SYNOPSIS
26  
27 package MySchema;
28  
29 __PACKAGE__->load_components( qw/LookupColumn::Auto/ );
30  
31 my @tables = __PACKAGE__->sources; # get all table names
32
33 my @candidates = grep { ! /Type$/ } @tables; # tables that do NOT end with Type
34 my @lookups = grep { /Type$/ } @tables; # tables that DO end with Type == the Lookup Tables !
35  
36 __PACKAGE__->add_lookups(
37 targets => \@candidates,
38 lookups => \@lookups,
39
40 # function that will generate the relation names: here we build it from the Lookup Table
41 relation_name_builder => sub{
42 my ( $class, %args) = @_;
43 $args{lookup} =~ /^(.+)Type$/; # remove the end (Type) from the Lookup table name
44 lc( $1 );
45 },
46 # function that gives the name of the column that holds the definitions/values: here it is always 'name'
47 lookup_field_name_builder => sub { 'name' }
48 );
49  
50  
51 =head1 DESCRIPTION
52  
53 This component automates the addition of the B<Lookup> (see L<DBIx::Class::LookupColumn/Lookup Tables>) relations to a whole set of tables.
54  
55 Given a set of potential target tables (the tables on which to add the Lookup relations), and a set of Lookup tables,
56 the component will select all the I<belongs_to> relations defined in the target tables pointing to a Lookup table present in the set
57 and add a Lookup relation automatically.
58  
59 It is also possible to add accessors manuall by doing a copy/paste of the code diplayed with the verbose option (See L<add_lookups>).
60  
61  
62 =head1 METHODS
63  
64 =head2 add_lookups
65  
66 __PACKAGE__->add_lookups( { targets => [], lookups => [], relation_name_builder? => sub {}, lookup_field_name_builder? => sub {}, verbose? => boolean } )
67  
68 This will iterate through the set of B<targets> tables on all B<belongs_to> relations pointing to a table included in B<lookups>
69 and add a corresponding relation.
70  
71 B<Arguments (hash keys) >:
72  
73 =over 4
74  
75 =item targets
76  
77 An ArrayRef of the names of the tables on which to detect and install the Lookup relations.
78  
79 =item lookups
80  
81 An ArrayRef of the names of the Lookup tables.
82  
83 =item relation_name_builder?
84  
85 Optional. FuncRef for building the accessors base name. By default the name of the Lookup table in small caps.
86 Arguments (hash keys) : { target => ?, lookup => ?, foreign_key => ? }.
87  
88 =item lookup_field_name_builder?
89  
90 Optional. FuncRef for specifying the concerned column name in the Lookup table. By default the first I<varchar> type column in the Lookup table.
91  
92 =item verbose?
93  
94 Optional. Boolean for displaying the code for adding a Lookup relation. Copy/paste it the right place of your code. By default set to false, then non-verbose.
95
96 =back
97  
98  
99  
100 =cut
101  
102 14 ▶sub add_lookups {
Subroutine add_lookups: called CC=10 SCAR=23.0
Pod documented
103  
104 14 my ( $class, %args ) = @_;
105
106
107 14 ▶ my $targets_array_ref = exists ( $args{targets} ) ? $args{targets} : confess 'targets arg is missing';
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'targets'} ? : 13 1 14
108 13 ▶ my $lookups_array_ref = exists ( $args{lookups} ) ? $args{lookups} : confess 'lookups arg is missing';
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'lookups'} ? : 12 1 13
109
110 12 my $options = {};
111 12 ▶ if ( exists ( $args{relation_name_builder} ) ) { $options->{relation_name_builder} = $args{relation_name_builder} ;}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"relation_name_builder"}) 9 3 12
112 12 ▶ if ( exists ( $args{lookup_field_name_builder})){ $options->{lookup_field_name_builder} = $args{lookup_field_name_builder};}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"lookup_field_name_builder"}) 9 3 12
113 12 ▶ if ( exists ( $args{verbose} ) ) { $options->{verbose} = $args{verbose} ;}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"verbose"}) 3 9 12
114
115 12 my $defaults = {
116 relation_name_builder => \&_guess_relation_name,
117 lookup_field_name_builder => \&_guess_field_name,
118 verbose => 0
119 };
120  
121  
122 12 my $params = merge $defaults, $options;
123  
124 12 my $verbose = $params->{verbose};
125
126 12 my $target2lkp_hash_ref = $class->_target2lookups( $targets_array_ref, $lookups_array_ref );
127
128 #### target2lookups returned: $target2lkp_hash_ref
129
130 12 my ( $target, $fk2lkp_hash_ref);
131 12 ▶ while ( ( $target, $fk2lkp_hash_ref ) = each ( %$target2lkp_hash_ref ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$target2lkp_hash_ref) 12 10 22
132 12 ▶ if($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 3 9 12
133 3 warn "adding to package $target\n";
134 3 warn "__PACKAGE__->load_components(LookupColumn)\n";
135 }
136 12 foreach my $fk (keys %$fk2lkp_hash_ref) {
137
138 16 my $lookup = $fk2lkp_hash_ref->{$fk};
139
140 my @args = (
141 $params->{relation_name_builder}->( $class, target => $target, lookup => $lookup, foreign_key => $fk ),
142 $fk, $lookup,
143 {
144 16 field_name => $params->{lookup_field_name_builder}->( $class, target => $target, lookup => $lookup, foreign_key => $fk )
145 }
146 );
147  
148 16 ▶ if($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 4 12 16
149 4 my $s = Dumper(\@args);
150 4 $s =~ s/^[^\[]*\[(.+)\];.*/$1/s;
151 4 warn "__PACKAGE__->add_lookup($s)\n" ;
152 }
153 16 DBIx::Class::LookupColumn::LookupColumnComponent::add_lookup( $class->class( $target), @args );
154 }
155 }
156 }
157  
158  
159 12 ▶sub _target2lookups {
Subroutine _target2lookups: called CC=7 SCAR=19.5
160 12 my ( $class, $targets_array_ref, $lookups_array_ref ) = @_;
161
162 12 my %lookups = map { ($class->class( $_ ), $_) } @$lookups_array_ref;
163
164 12 my %relationships;
165 12 foreach my $target ( @$targets_array_ref ) {
166 #### processing target table: $target
167 15 my $target_class = $class->class( $target );
168
169 15 foreach my $rel ($target_class->relationships) {
170 #### processing relation : $rel
171 34 my $info = $target_class->relationship_info($rel);
172
173 #### relationship_info: $info
174
175 34 ▶ next unless exists $lookups{$info->{source}}; # is the relation to a lookup
Branch: 1 branch
BranchTrueFalseTotal
unless exists $lookups{$info->{"source"}} 8 26 34
176
177 26 my @fk_columns = keys %{$info->{attrs}->{fk_columns}};
178 26 ▶ next if @fk_columns > 1; # if multiple foreign keys, not a belongs_to ?
Branch: 1 branch
BranchTrueFalseTotal
if @fk_columns > 1 0 26 26
179
180 26 ▶ unless (@fk_columns) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@fk_columns) 8 18 26
181 ### skipping relation because there is no foreign key, for table and relation: $target, $rel
182 8 next;
183 }
184 18 my $fk = shift @fk_columns;
185
186 18 ▶ next unless $info->{attrs}->{accessor} eq 'single'; # heuristic to detect belongs_to relation
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"attrs"}{"accessor"} eq "single" 0 18 18
187
188 18 $relationships{$target}->{$fk} = $lookups{$info->{source}};
189 }
190 }
191
192 12 return \%relationships;
193 }
194  
195  
196  
197  
198 5 ▶sub _guess_relation_name{
Subroutine _guess_relation_name: called CC=1 SCAR=0.0
199 5 my ( $class, %args ) = @_;
200 5 return lc( $args{lookup});
201 }
202  
203  
204
205  
206 5 ▶sub _guess_field_name {
Subroutine _guess_field_name: called CC=5 SCAR=19.5
207 5 my ( $class, %args ) = @_;
208
209 5 my $schema = $class;
210 5 my $lookup = $args{lookup};
211
212 5 my @columns = $schema->source( $lookup )->columns;
213 5 my @primary_columns = $schema->source( $lookup )->primary_columns;
214 5 my @columns_without_primary_keys = grep{ !($_ ~~ @primary_columns) } @columns;
215 5 my $guessed_field;
216
217 # classic lookup table with only two columns
218 5 ▶ if ( @columns == 2 && @columns_without_primary_keys == 1){
Branch: 1 branch
BranchTrueFalseTotal
if (@columns == 2 and @columns_without_primary_keys == 1) { } 5 0 5
Condition33%1/3
@columns == 2 and @columns_without_primary_keys == 1
!ll&&!rl&&r
005
MC/DC0%0/2
@columns == 2 and @columns_without_primary_keys == 1
@columns == 2 @columns_without_primary_keys == 1
Truth table33%1/3
A and B
A= @columns == 2B= @columns_without_primary_keys == 1
ABresult
0X0
100
111
219 5 $guessed_field = shift @columns_without_primary_keys;
220 }
221 # lookup table with more than two columns
222 else{
223 0 foreach my $column ( @columns_without_primary_keys ){
224 0 my $column_metas = $schema->source( $lookup )->column_info( $column );
225
226 0 ▶ if ( $column_metas->{data_type} =~ /varchar/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($column_metas->{"data_type"} =~ /varchar/) 0 0 0
227 #select the first varchar column
228 0 $guessed_field = $column;
229 0 last;
230 }
231 }
232 }
233 5 return $guessed_field;
234 }
235  
236  
237  
238  
239 =head1 AUTHORS
240  
241 Karl Forner <karl.forner@gmail.com>
242  
243 Thomas Rubattel <rubattel@cpan.org>
244  
245  
246 =head1 BUGS
247  
248 Please report any bugs or feature requests to C<bug-dbix-class-lookupcolumn-auto at rt.cpan.org>, or through
249 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBIx-Class-LookupColumn-Auto>. I will be notified, and then you'll
250 automatically be notified of progress on your bug as I make changes.
251  
252  
253 =head1 SUPPORT
254  
255 You can find documentation for this module with the perldoc command.
256  
257 perldoc DBIx::Class::LookupColumn::Auto
258  
259  
260 You can also look for information at:
261  
262 =over 4
263  
264 =item * RT: CPAN's request tracker (report bugs here)
265  
266 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Class-LookupColumn-Auto>
267  
268 =item * AnnoCPAN: Annotated CPAN documentation
269  
270 L<http://annocpan.org/dist/DBIx-Class-LookupColumn-Auto>
271  
272 =item * CPAN Ratings
273  
274 L<http://cpanratings.perl.org/d/DBIx-Class-LookupColumn-Auto>
275  
276 =item * Search CPAN
277  
278 L<http://search.cpan.org/dist/DBIx-Class-LookupColumn-Auto/>
279  
280 =back
281  
282  
283  
284 =head1 LICENCE AND COPYRIGHT
285  
286 Copyright 2012 Karl Forner and Thomas Rubattel, All Rights Reserved.
287  
288 This program is free software; you can redistribute it and/or modify it
289 under the terms as Perl itself.
290  
291 =cut
292  
293 10 1; # End of DBIx::Class::LookupColumn::Auto
↑ Summary LookupColumn/Manager.pm »