LookupColumn/LookupColumnComponent.pm

filter: Statementstmt 87.9% 73 / 83 Branchbran 50.0% 7 / 14 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 88.8% 16 / 18 Podpod 100% 1 / 1 totaltotal 84.0% 100 / 119 CC 11 SCAR 9.3
CC
11
Cov
83%
add_lookup
7.7
__ANON__
2.1
is_departmenttype
2.1
CRAP 2.5
« LookupColumn/Manager.pm ↑ Summary LookupColumn.pm »
1 package DBIx::Class::LookupColumn::LookupColumnComponent;
2 {
3 10 $DBIx::Class::LookupColumn::LookupColumnComponent::VERSION = '0.10';
4 }
5  
6 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 DBIx::Class::LookupColumn::LookupColumnComponent - A dbic component for building accessors for a lookup table.
12  
13 =cut
14  
15 15 ▶use base qw(DBIx::Class);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 15 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 15 ▶use Class::MOP;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 15 ▶use Smart::Comments -ENV;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 15 ▶use Hash::Merge::Simple qw/merge/;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 15 ▶use DBIx::Class::LookupColumn::Manager;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23  
24  
25  
26 =head1 SYNOPSIS
27  
28 # ===== use the component in your table definition =====
29 package MySchema::Result::User;
30 __PACKAGE__->load_components( B<qw/LookupColumn/> );>
31 __PACKAGE__->table("user");
32 __PACKAGE__->add_columns( "user_id",{}, "name", {}, "user_type_id", {} );
33 __PACKAGE__->set_primary_key("user_id");
34 __PACKAGE__->add_lookup( 'type', 'user_type_id', 'UserType' );
35  
36 # === use the generated accessors ===
37  
38 $user->type; # fetches the type value (e.g. 'Administrator') directly from the cache
39  
40 # checks that 'Administrator' is a valid value, get its id, and tests if it matches $user->user_type_id
41 $user->is_type('Administrator');
42
43 # checks that 'User' is a valid value, get its id, and sets it as $user->user_type_id
44 $user->set_type('User');
45  
46 =head1 DESCRIPTION
47  
48 This is the actual implementation of L<DBIx::Class::LookupColumn>, that is why you can and should use C<'LookupColumn'>
49 instead of C<'LookupColumn::LookupColumnComponent'> in the C<load_components> function call.
50  
51  
52 This module generates convenient methods (accessors) for accessing data in a B<Lookup table> (see L<DBIx::Class::LookupColumn/Lookup Tables>.
53 It uses L<DBIx::Class::LookupColumn::Manager> to cache and store the entire lookup tables in memory.
54  
55  
56 =head1 METHODS
57  
58 =head2 add_lookup
59  
60 add_lookup( $relation_name, $foreign_key, $lookup_table, \%options?)
61  
62 Add a Lookup relation from a Table to a B<Lookup table> from a foreign key by generating
63 new accessors and setters.
64 The relation is defined by its B<name> ( C<$relation_name> ), the B<foreign key> and the
65 B<Lookup table>.
66  
67 It will add three methods to the class: see L</GENERATED METHODS>.
68  
69  
70 B<Arguments>:
71  
72 =over 4
73  
74 =item $relation_name
75  
76 The name of the relation, used for making default names for the generated methods.
77  
78 =item $foreign_key
79  
80 the foreign key column in the table on which to add the lookup relation.
81  
82 =item $lookup_table
83  
84 The Lookup table, on which the foreign key points.
85  
86 =item \%options?
87  
88 An optional HashRef, with the following keys:
89  
90 =over 8
91  
92 =item name_accessor
93  
94 the name of the generated accessor, defaults to C<${relation_name}>
95  
96 =item name_setter
97  
98 the name of the generated setter, defaults to C<set_${relation_name}>
99  
100 =item name_checker
101  
102 the name of the generated method (checker), defaults to C<is_${relation_name}>
103  
104  
105 =back
106  
107 B<Example>:
108  
109 MySchema::Result::User->add_lookup( 'permission', 'permission_type_id', 'PermissionType',
110 {name_accessor => 'get_the_permission',
111 name_setter => 'set_the_permission,
112 name_checker => 'is_the_permission'
113 }
114 );
115  
116 Will add methods C<get_the_permission>, C<set_the_permission> and C<is_the_permission>
117 in MySchema::Result::User.
118  
119 =back
120  
121  
122 =head1 GENERATED METHODS
123  
124 =head2 name_accessor
125  
126 Return the value/definition/name in the target lookup table, storing the whole
127 looup table in the cache if not already done.
128  
129 B<Example>:
130  
131 print User->find($user_id)->type; # 'Administrator'
132  
133 =head2 name_setter
134  
135 Set the foreign key in the instance to point to the given value/definition/name.
136  
137 B<Example>:
138  
139 User->find($user_id)->set_type('Guest')
140  
141 =head2 name_checker
142  
143 Test if the lookup value of the row instance points to the same value as the argument.
144  
145 B<Example>:
146  
147 User->find($user_id)->is_type('Guest')
148  
149 Returns true if the value in the Lookup Table UserType associated with the key User->find($user_id)->user_type_id
150 is equals to 'Guest'.
151  
152 =cut
153  
154  
155  
156  
157 21 ▶sub add_lookup {
Subroutine add_lookup: called CC=7 SCAR=20.5
Pod documented
158 21 my ( $class, $relname, $foreign_key, $lookup_table, $options ) = @_;
159
160 #### add_lookup relation_name, foreign_key, lookup_table, options: $relname, $foreign_key, $lookup_table, $options
161
162 # as it suggests $options is an optional argument
163 21 ▶ $options ||= {};
Condition100%2/2
$options ||= {}
l!l
174
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
164
165 21 my $defaults = {
166 name_accessor => $relname,
167 name_setter => "set_$relname",
168 name_checker => "is_$relname",
169 field_name => 'name',
170 };
171
172 21 my $params = merge $defaults, $options;
173
174 21 my $field_name = $params->{field_name};
175
176 32 ▶ my $fetch_id_by_name = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
177 32 my ($self, $name) = @_;
178 32 DBIx::Class::LookupColumn::Manager->FETCH_ID_BY_NAME( $self->result_source->schema, $lookup_table, $field_name, $name);
179 21 };
180
181 21 ▶ my $meta = Class::MOP::Class->initialize($class) or die;
Branch: 1 branch
BranchTrueFalseTotal
my $meta = "Class::MOP::Class"->initialize($class) or die 21 0 21
182 # test if not already present
183 21 foreach my $method ( @$params{qw/name_accessor name_setter name_checker/} ) {
184 55 ▶ confess "ERROR: method $method already defined"
Branch: 1 branch
BranchTrueFalseTotal
if $meta->get_method($method) 4 51 55
185 if $meta->get_method($method);
186 }
187
188 17 my $is_immutable = $meta->is_immutable();
189  
190 17 ▶ $meta->make_mutable if $is_immutable;
Branch: 1 branch
BranchTrueFalseTotal
if $is_immutable 0 17 17
191
192 12 ▶ $meta->add_method( $params->{name_accessor}, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine permission: called CC=1 SCAR=0.0
Subroutine get_the_permission: called CC=1 SCAR=0.0
193 0 my $self = shift; # $self isa Row
194 12 my $schema = $self->result_source->schema;
195 12 return DBIx::Class::LookupColumn::Manager->FETCH_NAME_BY_ID( $schema, $lookup_table, $field_name, $self->get_column($foreign_key) );
196 17 });
197
198
199 4 ▶ $meta->add_method( $params->{name_setter}, sub {
Subroutine __ANON__: called CC=1 SCAR=1.4
Subroutine set_the_permission: not called CC=1 SCAR=1.4
Subroutine set_department: not called CC=1 SCAR=1.4
200 0 my ($self, $new_name) = @_;
201 0 my $schema = $self->result_source->schema;
202 0 my $id = $fetch_id_by_name->( $self, $new_name );
203 0 $self->set_column($foreign_key, $id);
204 17 });
205
206  
207 10 ▶ $meta->add_method( $params->{name_checker}, sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
Subroutine is_permission: called CC=2 SCAR=7.2
208 10 my ($self, $name) = @_; # $self isa Row
209 10 my $schema = $self->result_source->schema;
210 10 my $id = $self->get_column( $foreign_key );
211 10 ▶ return unless defined $id;
Branch: 3 branches
BranchTrueFalseTotal
unless defined $id 0 10 10
unless defined $id 0 17 17
unless defined $id 0 0 0
212 10 return $fetch_id_by_name->( $self, $name ) eq $id;
213 17 });
214
215 # was set as immutable then reset as such
216 17 ▶ $meta->make_immutable() if $is_immutable;
Branch: 1 branch
BranchTrueFalseTotal
if $is_immutable 0 17 17
217 }
218  
219  
220 =head1 AUTHORS
221  
222 Karl Forner <karl.forner@gmail.com>
223  
224 Thomas Rubattel <rubattel@cpan.org>
225  
226  
227 =head1 BUGS
228  
229 Please report any bugs or feature requests to C<bug-dbix-class-lookupcolumn-lookupcolumncomponent at rt.cpan.org>, or through
230 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBIx-Class-LookupColumn-LookupColumnComponent>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
231  
232  
233 =head1 SUPPORT
234  
235 You can find documentation for this module with the perldoc command.
236  
237 perldoc DBIx::Class::LookupColumn::LookupColumnComponent
238  
239  
240 You can also look for information at:
241  
242 =over 4
243  
244 =item * RT: CPAN's request tracker (report bugs here)
245  
246 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Class-LookupColumn-LookupColumnComponent>
247  
248 =item * AnnoCPAN: Annotated CPAN documentation
249  
250 L<http://annocpan.org/dist/DBIx-Class-LookupColumn-LookupColumnComponent>
251  
252 =item * CPAN Ratings
253  
254 L<http://cpanratings.perl.org/d/DBIx-Class-LookupColumn-LookupColumnComponent>
255  
256 =item * Search CPAN
257  
258 L<http://search.cpan.org/dist/DBIx-Class-LookupColumn-LookupColumnComponent/>
259  
260 =back
261  
262  
263 =head1 LICENCE AND COPYRIGHT
264  
265 Copyright 2012 Karl Forner and Thomas Rubattel, All Rights Reserved.
266  
267 This program is free software; you can redistribute it and/or modify it
268 under the terms as Perl itself.
269  
270 =cut
271  
272 10 1; # End of DBIx::Class::LookupColumn::LookupColumnComponent
« LookupColumn/Manager.pm ↑ Summary LookupColumn.pm »