File Coverage

blib/lib/DBIx/Class/Result/ColumnData.pm
Criterion Covered Total %
statement 66 128 51.5
branch 6 26 23.0
condition 0 6 0.0
subroutine 10 21 47.6
pod 5 5 100.0
total 87 186 46.7


line stmt bran cond sub pod time code
1             package DBIx::Class::Result::ColumnData;
2              
3 5     5   493677 use warnings;
  5         8  
  5         143  
4 5     5   19 use strict;
  5         20  
  5         115  
5 5     5   20 use Carp;
  5         11  
  5         2643  
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.16
18              
19             =cut
20              
21             our $VERSION = '0.16';
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     0 1 0 carp "columns_data is decrecated, use get_column_data";
59 0         0 my $obj = shift;
60              
61 0         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     0 1 0 my ($obj, $options) = @_;
73 0         0 my $rh_data;
74 0         0 my $class = ref $obj;
75 0         0 my @columns;
76 0 0 0     0 if (defined $options->{columns} && ref $options->{columns} eq 'ARRAY' ){
77 0         0 @columns = @{$options->{columns}};
  0         0  
78             }
79             else {
80 0         0 @columns = $class->columns;
81             }
82              
83 0         0 foreach my $key (@columns)
84             {
85 0 0       0 unless ($options->{with_all_fields})
86             {
87 0 0       0 next if ($class->column_info($key)->{hide_field});
88             }
89 0 0       0 if (ref($obj->get_column($key)) eq 'DateTime')
90             {
91 0         0 $rh_data->{$key} = $obj->_display_date($key) ;
92             }
93             else
94             {
95 0         0 $rh_data->{$key} = $obj->get_column($key);
96             }
97             }
98 0 0       0 if ($obj->isa('DBIx::Class::VirtualColumns'))
99             {
100             #TODO : tests
101 0         0 while (my ($virtual_column, $virtual_column_info) = each %{$class->_virtual_columns} )
  0         0  
102             {
103 0 0       0 if ( ref $virtual_column_info->{set_virtual_column} eq 'CODE')
104             {
105 0         0 $virtual_column_info->{set_virtual_column}->($obj);
106             }
107 0         0 $rh_data->{$virtual_column} = $obj->$virtual_column;
108             }
109             }
110              
111 0 0 0     0 if ($obj->isa('DBIx::Class::Result::Validation') && defined($obj->result_errors))
112             {
113 0         0 $rh_data->{result_errors} = $obj->result_errors;
114             }
115 0         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     0 1 0 my $obj = shift;
127 0         0 my $options = {with_all_fields => 1};
128 0         0 return $obj->get_column_data($options);
129             }
130              
131             sub _display_date
132             {
133 0     0   0 my ($obj, $key) = @_;
134 0         0 my $class = ref $obj;
135 0 0       0 return $obj->$key->ymd if $class->column_info($key)->{data_type} eq 'date';
136 0 0       0 return $obj->$key->ymd.' '.$obj->$key->hms if $class->column_info($key)->{data_type} eq 'datetime';
137 0         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     0 1 0 carp "register_relationships_columns_data is decrecated, use register_relationships_column_data";
149 0         0 my $class = shift;
150              
151 0         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 12     12 1 31947 my ($class) = @_;
181 12         407 foreach my $relation ($class->relationships())
182             {
183 16         2233 my $relation_type = $class->relationship_info($relation)->{attrs}->{accessor};
184 16 100       1757 if ($relation_type eq 'single')
185             {
186 7         14 my $method_name = $relation.'_column_data';
187             my $method_code = sub {
188              
189 0     0   0 my $self = shift;
190 0         0 my $relobject = $self->$relation;
191 0 0       0 return $relobject->get_column_data() if defined $relobject;
192 0         0 return undef;
193 7         25 };
194             {
195 5     5   24 no strict 'refs';
  5         7  
  5         491  
  7         11  
196 7         10 *{"${class}::${method_name}"} = $method_code;
  7         37  
197             }
198 7         12 my $old_method_name = $relation.'_columns_data';
199             my $old_method_code = sub {
200 0     0   0 carp "$old_method_name is decrecated, use $method_name";
201 0         0 my $class = shift;
202              
203 0         0 return $class->$method_name(@_);
204 7         21 };
205             {
206 5     5   22 no strict 'refs';
  5         7  
  5         549  
  7         9  
207 7         8 *{"${class}::${old_method_name}"} = $old_method_code;
  7         30  
208             }
209             }
210 16 100       38 if ($relation_type eq 'multi')
211             {
212 9         19 my $method_name = $relation.'_column_data';
213             my $method_code = sub {
214              
215 0     0   0 my $self = shift;
216 0         0 my @relobjects = $self->$relation;
217 0         0 my @relobjects_column_data = ();
218 0         0 foreach my $relobject (@relobjects)
219             {
220 0         0 push @relobjects_column_data, $relobject->get_column_data();
221             }
222 0         0 return @relobjects_column_data;
223 9         34 };
224             {
225 5     5   23 no strict 'refs';
  5         7  
  5         427  
  9         13  
226 9         11 *{"${class}::${method_name}"} = $method_code;
  9         48  
227             }
228 9         11 my $old_method_name = $relation.'_columns_data';
229             my $old_method_code = sub {
230 0     0   0 carp "$old_method_name is decrecated, use $method_name";
231 0         0 my $class = shift;
232              
233 0         0 return $class->$method_name(@_);
234 9         26 };
235             {
236 5     5   24 no strict 'refs';
  5         7  
  5         667  
  9         9  
237 9         11 *{"${class}::${old_method_name}"} = $old_method_code;
  9         47  
238             }
239              
240             }
241             }
242 12 100       279 if ($class->isa('DBIx::Class::IntrospectableM2M'))
243             {
244 3         3 foreach my $m2m_rel (keys(%{$class->_m2m_metadata}))
  3         76  
245             {
246 1         29 my $relation = $class->_m2m_metadata->{$m2m_rel}->{accessor};
247 1         11 my $method_name = $relation.'_column_data';
248             my $method_code = sub {
249              
250 0     0   0 my $self = shift;
251 0         0 my @relobjects = $self->$relation;
252 0         0 my @relobjects_column_data = ();
253 0         0 foreach my $relobject (@relobjects)
254             {
255 0         0 push @relobjects_column_data, $relobject->get_column_data();
256             }
257 0         0 return @relobjects_column_data;
258 1         5 };
259             {
260 5     5   23 no strict 'refs';
  5         6  
  5         453  
  1         1  
261 1         1 *{"${class}::${method_name}"} = $method_code;
  1         5  
262             }
263 1         7 my $old_method_name = $relation.'_columns_data';
264             my $old_method_code = sub {
265 0     0   0 carp "$old_method_name is decrecated, use $method_name";
266 0         0 my $class = shift;
267              
268 0         0 return $class->$method_name(@_);
269 1         4 };
270             {
271 5     5   24 no strict 'refs';
  5         9  
  5         367  
  1         2  
272 1         1 *{"${class}::${old_method_name}"} = $old_method_code;
  1         6  
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             1; # End of DBIx::Class::Result::ColumnData