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

filter: Statementstmt 52.3% 68 / 130 Branchbran 21.4% 6 / 28 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 47.6% 10 / 21 Podpod 100% 5 / 5 totaltotal 45.8% 89 / 194 CC 22 SCAR 38.7
CC
22
Cov
45%
get_column_data
156.0
_display_date
12.0
register_relationships_column_data
7.0
CRAP 47.8
↑ Summary Build »
1 package DBIx::Class::Result::ColumnData;
2  
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 DBIx::Class::Result::ColumnData - Result::ColumnData component for DBIx::Class
10  
11 This module is used to extract column data only from a data object base on DBIx::Class::Core
12  
13 It defined relationships methods to extract columns data only of relationships
14  
15 =head1 VERSION
16  
17 Version 0.15
18  
19 =cut
20  
21 5 our $VERSION = '0.15';
22  
23  
24 =head1 SYNOPSIS
25  
26 in your DBIx::Class::Core base class declare Result::ColumnData component
27  
28 Package::Schema::Result::MyClass;
29  
30 use strict;
31 use warning;
32  
33 __PACKAGE__->load_component(qw/ ... Result::DataColumn /);
34  
35 #Declare here associations before register_relationships_column_data
36 __PACKAGE__->belongs_to(...);
37 __PACKAGE__->has_many(...);
38  
39 __PACKAGE__->register_relationships_column_data();
40  
41 you will use get_column_data functions on instance of MyClass
42  
43 $my_class->get_column_data
44 $my_class->I<relationships>_column_data
45  
46 you can also hide some columns with parameter hide_field on columns definition
47  
48 __PACKAGE__->add_columns("field_to_hide", {.... hide_field => 1});
49  
50 =head2 columns_data
51  
52 columns_data is decrecated, use get_column_data
53  
54 =cut
55  
56 sub columns_data
57 {
58 0 ▶ carp "columns_data is decrecated, use get_column_data";
Subroutine columns_data: not called CC=1 SCAR=6.9
Pod documented
59 0 my $obj = shift;
60  
61 0 $obj->get_column_data(@_);
62 }
63  
64 =head2 get_column_data
65  
66 return only column_data from an object DBIx::Class::Core without hide_field
67  
68 =cut
69  
70 sub get_column_data
71 {
72 0 ▶ my ($obj, $options) = @_;
Subroutine get_column_data: not called CC=12 SCAR=50.5
Pod documented
73 0 my $rh_data;
74 0 my $class = ref $obj;
75 0 my @columns;
76 0 ▶ if (defined $options->{columns} && ref $options->{columns} eq 'ARRAY' ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{'columns'} and ref $options->{'columns'} eq 'ARRAY') { } 0 0 0
Condition0%0/3
defined $options->{"columns"} and ref $options->{"columns"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $options->{"columns"} and ref $options->{"columns"} eq "ARRAY"
defined $options->{"columns"} ref $options->{"columns"} eq "ARRAY"
Truth table0%0/3
A and B
A= defined $options->{"columns"}B= ref $options->{"columns"} eq "ARRAY"
ABresult
0X0
100
111
77 0 @columns = @{$options->{columns}};
78 }
79 else {
80 0 @columns = $class->columns;
81 }
82  
83 0 foreach my $key (@columns)
84 {
85 0 ▶ unless ($options->{with_all_fields})
Branch: 1 branch
BranchTrueFalseTotal
unless ($options->{"with_all_fields"}) 0 0 0
86 {
87 0 ▶ next if ($class->column_info($key)->{hide_field});
Branch: 1 branch
BranchTrueFalseTotal
if $class->column_info($key)->{"hide_field"} 0 0 0
88 }
89 0 ▶ if (ref($obj->get_column($key)) eq 'DateTime')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj->get_column($key) eq 'DateTime') { } 0 0 0
90 {
91 0 $rh_data->{$key} = $obj->_display_date($key) ;
92 }
93 else
94 {
95 0 $rh_data->{$key} = $obj->get_column($key);
96 }
97 }
98 0 ▶ if ($obj->isa('DBIx::Class::Result::VirtualColumns'))
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->isa("DBIx::Class::Result::VirtualColumns")) 0 0 0
99 {
100 #TODO : tests
101 0 ▶ while (my ($virtual_column, $virtual_column_info) = each %{$class->_virtual_columns} )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$class->_virtual_columns;}) 0 0 0
102 {
103 0 ▶ if ( ref $virtual_column_info->{set_virtual_column} eq 'CODE')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $virtual_column_info->{"set_virtual_column"} eq "CODE") 0 0 0
104 {
105 0 $virtual_column_info->{set_virtual_column}->($obj);
106 }
107 0 $rh_data->{$virtual_column} = $obj->$virtual_column;
108 }
109 }
110  
111 0 ▶ if ($obj->isa('DBIx::Class::Result::Validation') && defined($obj->result_errors))
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->isa("DBIx::Class::Result::Validation") and defined $obj->result_errors) 0 0 0
Condition0%0/3
$obj->isa("DBIx::Class::Result::Validation") and defined $obj->result_errors
!ll&&!rl&&r
000
MC/DC0%0/2
$obj->isa("DBIx::Class::Result::Validation") and defined $obj->result_errors
$obj->isa("DBIx::Class::Result::Validation") defined $obj->result_errors
Truth table0%0/3
A and B
A= $obj->isa("DBIx::Class::Result::Validation")B= defined $obj->result_errors
ABresult
0X0
100
111
112 {
113 0 $rh_data->{result_errors} = $obj->result_errors;
114 }
115 0 return $rh_data;
116 }
117  
118 =head2 get_all_column_data
119  
120 return only column_data from an object DBIx::Class::Core with hide_field
121  
122 =cut
123  
124 sub get_all_column_data
125 {
126 0 ▶ my $obj = shift;
Subroutine get_all_column_data: not called CC=1 SCAR=6.9
Pod documented
127 0 my $options = {with_all_fields => 1};
128 0 return $obj->get_column_data($options);
129 }
130  
131 sub _display_date
132 {
133 0 ▶ my ($obj, $key) = @_;
Subroutine _display_date: not called CC=3 SCAR=24.8
134 0 my $class = ref $obj;
135 0 ▶ return $obj->$key->ymd if $class->column_info($key)->{data_type} eq 'date';
Branch: 1 branch
BranchTrueFalseTotal
if $class->column_info($key)->{"data_type"} eq "date" 0 0 0
136 0 ▶ return $obj->$key->ymd.' '.$obj->$key->hms if $class->column_info($key)->{data_type} eq 'datetime';
Branch: 1 branch
BranchTrueFalseTotal
if $class->column_info($key)->{"data_type"} eq "datetime" 0 0 0
137 0 return '';
138 }
139  
140 =head2 register_relationships_columns_data
141  
142 register_relationships_columns_data is decrecated, use register_relationships_column_data
143  
144 =cut
145  
146 sub register_relationships_columns_data
147 {
148 0 ▶ carp "register_relationships_columns_data is decrecated, use register_relationships_column_data";
Subroutine register_relationships_columns_data: not called CC=1 SCAR=6.9
Pod documented
149 0 my $class = shift;
150  
151 0 $class->register_relationships_column_data(@_);
152 }
153  
154 =head2 register_relationships_column_data
155  
156 declare functions for each relationship on canva : I<relationship>_column_data which return a hash columns data for a single relationship and an list of hash columns data for multi relationships
157  
158 Package::Schema::Result::Keyboard->belongs_to( computer => "Package::Schema::Result::Computer", computer_id);
159 Package::Schema::Result::Keyboard->has_many( keys => "Package::Schema::Result::Key", keyboard_id);
160  
161 register_relationships_column_data generate instance functions for Keyboard object
162  
163 $keybord->keys_column_data()
164  
165 # return
166 # [
167 # { id => 1, value => 'A', azerty_position => 1},
168 # { id => 2, value => 'B', azerty_position => 25},
169 # ....
170 # ];
171  
172 $keybord->cumputer_column_data()
173  
174 # return
175 # { id => 1, os => 'ubuntu' };
176  
177 =cut
178  
179 sub register_relationships_column_data {
180 7 ▶ my ($class) = @_;
Subroutine register_relationships_column_data: called CC=6 SCAR=19.4
Pod documented
181 7 foreach my $relation ($class->relationships())
182 {
183 8 my $relation_type = $class->relationship_info($relation)->{attrs}->{accessor};
184 8 ▶ if ($relation_type eq 'single')
Branch: 1 branch
BranchTrueFalseTotal
if ($relation_type eq "single") 2 6 8
185 {
186 2 my $method_name = $relation.'_column_data';
187 my $method_code = sub {
188  
189 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
190 0 my $relobject = $self->$relation;
191 0 ▶ return $relobject->get_column_data() if defined $relobject;
Branch: 1 branch
BranchTrueFalseTotal
if defined $relobject 0 0 0
192 0 return undef;
193 2 };
194 {
195 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
196 2 *{"${class}::${method_name}"} = $method_code;
197 }
198 2 my $old_method_name = $relation.'_columns_data';
199 my $old_method_code = sub {
200 0 ▶ carp "$old_method_name is decrecated, use $method_name";
Subroutine __ANON__: not called CC=1 SCAR=6.9
201 0 my $class = shift;
202  
203 0 return $class->$method_name(@_);
204 2 };
205 {
206 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
207 2 *{"${class}::${old_method_name}"} = $old_method_code;
208 }
209 }
210 8 ▶ if ($relation_type eq 'multi')
Branch: 1 branch
BranchTrueFalseTotal
if ($relation_type eq "multi") 6 2 8
211 {
212 6 my $method_name = $relation.'_column_data';
213 my $method_code = sub {
214  
215 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
216 0 my @relobjects = $self->$relation;
217 0 my @relobjects_column_data = ();
218 0 foreach my $relobject (@relobjects)
219 {
220 0 push @relobjects_column_data, $relobject->get_column_data();
221 }
222 0 return @relobjects_column_data;
223 6 };
224 {
225 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
226 6 *{"${class}::${method_name}"} = $method_code;
227 }
228 6 my $old_method_name = $relation.'_columns_data';
229 my $old_method_code = sub {
230 0 ▶ carp "$old_method_name is decrecated, use $method_name";
Subroutine __ANON__: not called CC=1 SCAR=6.9
231 0 my $class = shift;
232  
233 0 return $class->$method_name(@_);
234 6 };
235 {
236 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
237 6 *{"${class}::${old_method_name}"} = $old_method_code;
238 }
239  
240 }
241 }
242 7 ▶ if ($class->isa('DBIx::Class::IntrospectableM2M'))
Branch: 1 branch
BranchTrueFalseTotal
if ($class->isa("DBIx::Class::IntrospectableM2M")) 3 4 7
243 {
244 3 foreach my $m2m_rel (keys(%{$class->_m2m_metadata}))
245 {
246 2 my $relation = $class->_m2m_metadata->{$m2m_rel}->{accessor};
247 2 my $method_name = $relation.'_column_data';
248 my $method_code = sub {
249  
250 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
251 0 my @relobjects = $self->$relation;
252 0 my @relobjects_column_data = ();
253 0 foreach my $relobject (@relobjects)
254 {
255 0 push @relobjects_column_data, $relobject->get_column_data();
256 }
257 0 return @relobjects_column_data;
258 2 };
259 {
260 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
261 2 *{"${class}::${method_name}"} = $method_code;
262 }
263 2 my $old_method_name = $relation.'_columns_data';
264 my $old_method_code = sub {
265 0 ▶ carp "$old_method_name is decrecated, use $method_name";
Subroutine __ANON__: not called CC=1 SCAR=6.9
266 0 my $class = shift;
267  
268 0 return $class->$method_name(@_);
269 2 };
270 {
271 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
272 2 *{"${class}::${old_method_name}"} = $old_method_code;
273 }
274  
275 }
276 }
277 }
278  
279 =head1 AUTHOR
280  
281 Nicolas Oudard, <nicolas@oudard.org>
282  
283 =head1 BUGS
284  
285 Please report any bugs or feature requests to C<bug-dbix-class-result-columndata at rt.cpan.org>, or through
286 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBIx-Class-Result-ColumnData>. I will be notified, and then you'll
287 automatically be notified of progress on your bug as I make changes.
288  
289  
290 =head1 SUPPORT
291  
292 You can find documentation for this module with the perldoc command.
293  
294 perldoc DBIx::Class::Result::ColumnData
295  
296  
297 =head1 LICENSE AND COPYRIGHT
298  
299 Copyright 2010 Nicolas Oudard.
300  
301 This program is free software; you can redistribute it and/or modify it
302 under the terms of either: the GNU General Public License as published
303 by the Free Software Foundation; or the Artistic License.
304  
305 See http://dev.perl.org/licenses/ for more information.
306  
307 =cut
308  
309 5 1; # End of DBIx::Class::Result::ColumnData
↑ Summary Build »