blib/lib/DBIx/Class/Result/ExternalAttribute.pm

filter: Statementstmt 98.5% 69 / 70 Branchbran 71.4% 10 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 100% 7 / 7 totaltotal 95.1% 97 / 102 CC 16 SCAR 11.5
CC
16
Cov
94%
rh_klass_attribute_column
4.0
prepare_params_with_attribute
4.0
update
4.0
CRAP 3.2
↑ Summary
1 package DBIx::Class::Result::ExternalAttribute;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 DBIx::Class::Result::ExternalAttribute - The great new DBIx::Class::Result::ExternalAttribute!
10  
11 =head1 VERSION
12  
13 Version 0.06
14  
15 =cut
16  
17 # version
18 3 our $VERSION = '0.06';
19  
20 # use base
21 3 ▶use base qw/ DBIx::Class Class::Accessor::Grouped /;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 3 my $rh_klass_attribute_column = {};
24  
25 =head1 SYNOPSIS
26  
27 use attached model to store attribute.
28  
29 for example artist result:
30  
31 package t::app::Main::Result::Artist;
32 use base qw/DBIx::Class::Core/;
33 __PACKAGE__->table('artist');
34 __PACKAGE__->add_columns(
35 "id",
36 { data_type => "integer", is_auto_increment => 1, is_nullable => 0 },
37 "name",
38 { data_type => "varchar",
39 default_value => "",
40 is_nullable => 0,
41 size => 255
42 });
43 __PACKAGE__->set_primary_key('id');
44  
45 __PACKAGE__->load_components(qw/ Result::ExternalAttribute Result::ColumnData /);
46 __PACKAGE__->init_external_attribute(
47 artist_attribute =>
48 't::app::Main::Result::ArtistAttribute',
49 'artist_id'
50 );
51 __PACKAGE__->register_relationships_column_data();
52  
53 use a artist attribute result:
54  
55 package t::app::Main::Result::ArtistAttribute;
56 use base qw/DBIx::Class::Core/;
57 __PACKAGE__->table('artist_attribute');
58 __PACKAGE__->add_columns(
59 "artist_id",
60 { data_type => "integer", is_nullable => 0 },
61 "year_old",
62 { data_type => "integer", is_nullable => 1});
63 __PACKAGE__->set_primary_key('artist_id');
64 __PACKAGE__->load_components(qw/ Result::ColumnData /);
65 __PACKAGE__->belongs_to( artist => "t::app::Main::Result::Artist", 'artist_id');
66  
67 1;
68  
69 with this configuration, you can call methods:
70  
71 $artist->get_column_data => get only columns of artist result
72  
73 $artist->get_column_data_with_attribute => get columns of Artist and ArtistAttribute result except artist_id
74  
75 #update with artist attributes
76 $artist->update({name => "Me", year_old => 15});
77  
78 #create with artist attributes
79 my $rh = t::app::Main::Result::Artist->prepare_params_with_attribute({name => "Me", year_old => 15});
80 $schema->resultset('Artist')->create($rh);
81  
82 =head1 EXPORT
83  
84 A list of functions that can be exported. You can delete this section
85 if you don't export anything, such as for a purely object-oriented module.
86  
87 =head1 SUBROUTINES/METHODS
88  
89 =head2 rh_klass_attribute_column
90  
91 accessor to init_external_attrinute HASH configuration
92  
93 =cut
94  
95 12 ▶sub rh_klass_attribute_column {
Subroutine rh_klass_attribute_column: called CC=4 SCAR=14.0
Pod documented
96 12 my ( $klass, $rel, $klass_attribute_column, $external_key ) = @_;
97 12 ▶ croak "this function must be call with a class" unless defined $klass;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $klass 0 12 12
98 12 ▶ if ( @_ == 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 4) 1 11 12
99 1 return $rh_klass_attribute_column->{$klass}->{$rel}->{'columns'} =
100 $klass_attribute_column;
101 0 return $rh_klass_attribute_column->{$klass}->{$rel}->{'external_key'} =
102 $external_key;
103 }
104 11 ▶ if ( @_ == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 5 6 11
105 5 return $rh_klass_attribute_column->{$klass}->{$rel};
106 }
107 else {
108 6 return $rh_klass_attribute_column->{$klass};
109 }
110 }
111  
112 =head2 init_external_attribute
113  
114 init function, declare has one relationships
115  
116 =cut
117  
118 1 ▶sub init_external_attribute {
Subroutine init_external_attribute: called CC=3 SCAR=11.0
Pod documented
119 1 my ( $klass, $rel, $klass_attribute, $external_key ) = @_;
120  
121 # declare has_one relation
122 1 $klass->has_one( $rel, $klass_attribute, $external_key );
123  
124 # create accessor for each column
125 # $klass->mk_group_accessors(simple => $klass_attribute->columns);
126 #init hash of attribute associate object
127 1 my @columns;
128 1 foreach my $col ( $klass_attribute->columns ) {
129 2 ▶ push( @columns, $col ) unless $col eq $external_key;
Branch: 1 branch
BranchTrueFalseTotal
unless $col eq $external_key 1 1 2
130 }
131 1 $klass->rh_klass_attribute_column( $rel, \@columns, $external_key );
132 }
133  
134 =head2 columns_data_with_attribute
135  
136 columns_data_with_external_attribute is deprecated, please use get_column_data_with_attribute
137  
138 =cut
139  
140 1 ▶sub columns_data_with_attribute {
Subroutine columns_data_with_attribute: called CC=1 SCAR=0.0
Pod documented
141 1 carp "columns_data_with_external_attribute is deprecated, please use get_column_data_with_attribute";
142 1 my $self = shift;
143 1 $self->get_column_data_with_attribute(@_);
144 }
145  
146 =head2 get_column_data_with_attribute
147  
148 extract column_data with attribute column
149  
150 =cut
151  
152 3 ▶sub get_column_data_with_attribute {
Subroutine get_column_data_with_attribute: called CC=4 SCAR=13.9
Pod documented
153 3 my $self = shift;
154 3 my $klass = ref $self;
155 3 my $rh_result = $self->get_column_data();
156 3 foreach my $rel_attr ( keys %{ $klass->rh_klass_attribute_column } ) {
157 3 my $rel_object = $self->$rel_attr;
158 3 ▶ next unless defined $rel_object;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rel_object 0 3 3
159 3 my $rh_result_attribute = $self->$rel_attr->get_column_data();
160 3 foreach my $col ( @{ $klass->rh_klass_attribute_column($rel_attr)->{'columns'} } ) {
161 3 $rh_result->{$col} = $rh_result_attribute->{$col};
162 }
163 }
164  
165 3 return $rh_result;
166 }
167  
168 =head2 prepare_params_with_attribute
169  
170 prepare params for creation with attributes
171  
172 =cut
173  
174 1 ▶sub prepare_params_with_attribute {
Subroutine prepare_params_with_attribute: called CC=4 SCAR=13.9
Pod documented
175 1 my ( $klass, $rh_fields ) = @_;
176 1 foreach my $rel ( keys %{ $klass->rh_klass_attribute_column } ) {
177 1 foreach my $col ( @{ $klass->rh_klass_attribute_column($rel)->{'columns'} } ) {
178 1 ▶ if ( defined $rh_fields->{$col} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rh_fields->{$col}) 1 0 1
179 1 $rh_fields->{$rel}->{$col} = $rh_fields->{$col};
180 1 delete $rh_fields->{$col};
181 }
182 }
183 }
184 1 return $rh_fields;
185 }
186  
187 =head2 update
188  
189 overdefinition of update function
190  
191 =cut
192  
193 1 ▶sub update {
Subroutine update: called CC=4 SCAR=13.9
Pod documented
194 1 my ( $self, $rh_fields ) = @_;
195 1 my $klass = ref $self;
196 1 foreach my $rel ( keys %{ $klass->rh_klass_attribute_column } ) {
197 1 foreach my $col ( @{ $klass->rh_klass_attribute_column($rel)->{'columns'} } ) {
198 1 ▶ if ( defined $rh_fields->{$col} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rh_fields->{$col}) 1 0 1
199 1 $self->$rel->$col( $rh_fields->{$col} );
200 1 delete $rh_fields->{$col};
201 }
202 }
203 }
204 1 return $self->next::method($rh_fields);
205 }
206  
207 =head2 insert
208  
209 overdefinition of update function
210  
211 =cut
212 sub insert
213 1 ▶{
Subroutine insert: called CC=2 SCAR=6.9
Pod documented
214 1 my ( $self, @args ) = @_;
215 1 my $klass = ref $self;
216 1 $self->next::method(@args);
217 1 foreach my $rel ( keys %{ $klass->rh_klass_attribute_column } ) {
218 1 $self->find_or_create_related($rel, {});
219 }
220 1 return $self;
221 }
222  
223 =head1 AUTHOR
224  
225 Nicolas Oudard, C<< <nicolas at oudard.org> >>
226  
227 =head1 BUGS
228  
229 Please report any bugs or feature requests to C<bug-dbix-class-result-externalattribute at rt.cpan.org>, or through
230 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBIx-Class-Result-ExternalAttribute>. I will be notified, and then you'll
231 automatically be notified of progress on your bug as I make changes.
232  
233  
234  
235  
236 =head1 SUPPORT
237  
238 You can find documentation for this module with the perldoc command.
239  
240 perldoc DBIx::Class::Result::ExternalAttribute
241  
242  
243 You can also look for information at:
244  
245 =over 4
246  
247 =item * RT: CPAN's request tracker
248  
249 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Class-Result-ExternalAttribute>
250  
251 =item * AnnoCPAN: Annotated CPAN documentation
252  
253 L<http://annocpan.org/dist/DBIx-Class-Result-ExternalAttribute>
254  
255 =item * CPAN Ratings
256  
257 L<http://cpanratings.perl.org/d/DBIx-Class-Result-ExternalAttribute>
258  
259 =item * Search CPAN
260  
261 L<http://search.cpan.org/dist/DBIx-Class-Result-ExternalAttribute/>
262  
263 =back
264  
265  
266 =head1 ACKNOWLEDGEMENTS
267  
268  
269 =head1 LICENSE AND COPYRIGHT
270  
271 Copyright 2011 Nicolas Oudard.
272  
273 This program is free software; you can redistribute it and/or modify it
274 under the terms of either: the GNU General Public License as published
275 by the Free Software Foundation; or the Artistic License.
276  
277 See http://dev.perl.org/licenses/ for more information.
278  
279  
280 =cut
281  
282 3 1; # End of DBIx::Class::Result::ExternalAttribute
283  
↑ Summary