DBIx/Class/FilterColumn.pm

filter: Statementstmt 100% 69 / 69 Branchbran 68.0% 34 / 50 Conditioncond 50.0% 9 / 18 MC/DCmcdc 25.0% 3 / 12 Subroutinesub 100% 13 / 13 Podpod 100% 8 / 8 totaltotal 80.0% 136 / 170 CC 35 SCAR 16.7
CC
35
Cov
82%
filter_column
8.2
new
6.9
update
6.6
CRAP 5.3
« DBIx/Class/InflateColumn.pm ↑ Summary DBIx/Class/Relationship/HasOne.pm »
1 package DBIx::Class::FilterColumn;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use base qw/DBIx::Class::Row/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 sub filter_column {
8 5 ▶ my ($self, $col, $attrs) = @_;
Subroutine filter_column: called CC=8 SCAR=21.1
Pod documented
9  
10 5 my $colinfo = $self->column_info($col);
11  
12 $self->throw_exception('FilterColumn does not work with InflateColumn')
13 if $self->isa('DBIx::Class::InflateColumn') &&
14 5 ▶ defined $colinfo->{_inflate_info};
Branch: 1 branch
BranchTrueFalseTotal
if $self->isa("DBIx::Class::InflateColumn") and defined $colinfo->{"_inflate_info"} 1 4 5
Condition66%2/3
$self->isa("DBIx::Class::InflateColumn") and defined $colinfo->{"_inflate_info"}
!ll&&!rl&&r
041
MC/DC50%1/2
$self->isa("DBIx::Class::InflateColumn") and defined $colinfo->{"_inflate_info"}
$self->isa("DBIx::Class::InflateColumn") defined $colinfo->{"_inflate_info"}
Truth table66%2/3
A and B
A= $self->isa("DBIx::Class::InflateColumn")B= defined $colinfo->{"_inflate_info"}
ABresult
0X0
100
111
15  
16 4 ▶ $self->throw_exception("No such column $col to filter")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_column($col) 0 4 4
17 unless $self->has_column($col);
18  
19 4 ▶ $self->throw_exception('filter_column expects a hashref of filter specifications')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $attrs eq "HASH" 0 4 4
20 unless ref $attrs eq 'HASH';
21  
22 $self->throw_exception('An invocation of filter_column() must specify either a filter_from_storage or filter_to_storage')
23 4 ▶ unless $attrs->{filter_from_storage} || $attrs->{filter_to_storage};
Branch: 1 branch
BranchTrueFalseTotal
unless $attrs->{"filter_from_storage"} or $attrs->{"filter_to_storage"} 1 3 4
Condition100%3/3
$attrs->{"filter_from_storage"} or $attrs->{"filter_to_storage"}
l!l&&r!l&&!r
211
MC/DC100%2/2
$attrs->{"filter_from_storage"} or $attrs->{"filter_to_storage"}
$attrs->{"filter_from_storage"} $attrs->{"filter_to_storage"}
Truth table100%3/3
A or B
A= $attrs->{"filter_from_storage"}B= $attrs->{"filter_to_storage"}
ABresult
1X1
011
000
24  
25 3 $colinfo->{_filter_info} = $attrs;
26 3 my $acc = $colinfo->{accessor};
27 3 ▶ $self->mk_group_accessors(filtered_column => [ (defined $acc ? $acc : $col), $col]);
Branch: 1 branch
BranchTrueFalseTotal
defined $acc ? : 0 3 3
28 3 return 1;
29 }
30  
31 sub _column_from_storage {
32 8 ▶ my ($self, $col, $value) = @_;
Subroutine _column_from_storage: called CC=5 SCAR=16.6
33  
34 8 ▶ return $value unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 8 8
35  
36 8 ▶ my $info = $self->column_info($col)
Branch: 1 branch
BranchTrueFalseTotal
my $info = $self->column_info($col) or $self->throw_exception("No column info for $col") 0 8 8
37 or $self->throw_exception("No column info for $col");
38  
39 8 ▶ return $value unless exists $info->{_filter_info};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $info->{"_filter_info"} 0 8 8
40  
41 8 my $filter = $info->{_filter_info}{filter_from_storage};
42  
43 8 ▶ return defined $filter ? $self->$filter($value) : $value;
Branch: 1 branch
BranchTrueFalseTotal
defined $filter ? : 7 1 8
44 }
45  
46 sub _column_to_storage {
47 9 ▶ my ($self, $col, $value) = @_;
Subroutine _column_to_storage: called CC=4 SCAR=14.1
48  
49 9 ▶ my $info = $self->column_info($col) or
Branch: 1 branch
BranchTrueFalseTotal
my $info = $self->column_info($col) or $self->throw_exception("No column info for $col") 0 9 9
50 $self->throw_exception("No column info for $col");
51  
52 9 ▶ return $value unless exists $info->{_filter_info};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $info->{"_filter_info"} 0 9 9
53  
54 9 my $unfilter = $info->{_filter_info}{filter_to_storage};
55  
56 9 ▶ return defined $unfilter ? $self->$unfilter($value) : $value;
Branch: 1 branch
BranchTrueFalseTotal
defined $unfilter ? : 7 2 9
57 }
58  
59 sub get_filtered_column {
60 14 ▶ my ($self, $col) = @_;
Subroutine get_filtered_column: called CC=3 SCAR=11.0
Pod documented
61  
62 $self->throw_exception("$col is not a filtered column")
63 14 ▶ unless exists $self->column_info($col)->{_filter_info};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->column_info($col)->{"_filter_info"} 0 14 14
64  
65 return $self->{_filtered_column}{$col}
66 14 ▶ if exists $self->{_filtered_column}{$col};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_filtered_column"}{$col} 6 8 14
67  
68 8 my $val = $self->get_column($col);
69  
70 8 return $self->{_filtered_column}{$col} = $self->_column_from_storage($col, $val);
71 }
72  
73 sub get_column {
74 34 ▶ my ($self, $col) = @_;
Subroutine get_column: called CC=3 SCAR=11.3
Pod documented
75 34 ▶ if (exists $self->{_filtered_column}{$col}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_filtered_column"}{$col}) 8 26 34
76 8 ▶ return $self->{_column_data}{$col} ||= $self->_column_to_storage ($col, $self->{_filtered_column}{$col});
Condition33%1/3
$self->{"_column_data"}{$col} ||= $self->_column_to_storage($col, $self->{"_filtered_column"}{$col})
l!l&&r!l&&!r
800
MC/DC0%0/2
$self->{"_column_data"}{$col} ||= $self->_column_to_storage($col, $self->{"_filtered_column"}{$col})
$self->{"_column_data"}{$col} $self->_column_to_storage($col, $self->{"_filtered_column"}{$col})
Truth table33%1/3
A ||= B
A= $self->{"_column_data"}{$col}B= $self->_column_to_storage($col, $self->{"_filtered_column"}{$col})
ABresult
1X1
011
000
77 }
78  
79 26 return $self->next::method ($col);
80 }
81  
82 # sadly a separate codepath in Row.pm ( used by insert() )
83 sub get_columns {
84 5 ▶ my $self = shift;
Subroutine get_columns: called CC=5 SCAR=17.8
Pod documented
85  
86 5 ▶ foreach my $col (keys %{$self->{_filtered_column}||{}}) {
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_filtered_column"} or {} 0 5 5
87 $self->{_column_data}{$col} ||= $self->_column_to_storage ($col, $self->{_filtered_column}{$col})
88 5 ▶ if exists $self->{_filtered_column}{$col};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_filtered_column"}{$col} 5 0 5
Condition33%1/3
$self->{"_column_data"}{$col} ||= $self->_column_to_storage($col, $self->{"_filtered_column"}{$col})
l!l&&r!l&&!r
500
MC/DC0%0/2
$self->{"_column_data"}{$col} ||= $self->_column_to_storage($col, $self->{"_filtered_column"}{$col})
$self->{"_column_data"}{$col} $self->_column_to_storage($col, $self->{"_filtered_column"}{$col})
Truth table33%1/3
A ||= B
A= $self->{"_column_data"}{$col}B= $self->_column_to_storage($col, $self->{"_filtered_column"}{$col})
ABresult
1X1
011
000
89 }
90  
91 5 $self->next::method (@_);
92 }
93  
94 sub store_column {
95 15 ▶ my ($self, $col) = (shift, @_);
Subroutine store_column: called CC=1 SCAR=0.0
Pod documented
96  
97 # blow cache
98 15 delete $self->{_filtered_column}{$col};
99  
100 15 $self->next::method(@_);
101 }
102  
103 sub set_filtered_column {
104 10 ▶ my ($self, $col, $filtered) = @_;
Subroutine set_filtered_column: called CC=3 SCAR=11.0
Pod documented
105  
106 # do not blow up the cache via set_column unless necessary
107 # (filtering may be expensive!)
108 10 ▶ if (exists $self->{_filtered_column}{$col}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_filtered_column"}{$col}) 7 3 10
109 return $filtered
110 7 ▶ if ($self->_eq_column_values ($col, $filtered, $self->{_filtered_column}{$col} ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_eq_column_values($col, $filtered, $self->{"_filtered_column"}{$col}) 1 6 7
111  
112 6 $self->make_column_dirty ($col); # so the comparison won't run again
113 }
114  
115 9 $self->set_column($col, $self->_column_to_storage($col, $filtered));
116  
117 9 return $self->{_filtered_column}{$col} = $filtered;
118 }
119  
120 sub update {
121 5 ▶ my ($self, $attrs, @rest) = @_;
Subroutine update: called CC=6 SCAR=18.8
Pod documented
122  
123 5 ▶ foreach my $key (keys %{$attrs||{}}) {
Branch: 1 branch
BranchTrueFalseTotal
$attrs or {} 4 1 5
124 1 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_column($key) and exists $self->column_info($key)->{"_filter_info"}) 1 0 1
Condition33%1/3
$self->has_column($key) and exists $self->column_info($key)->{"_filter_info"}
!ll&&!rl&&r
001
MC/DC0%0/2
$self->has_column($key) and exists $self->column_info($key)->{"_filter_info"}
$self->has_column($key) exists $self->column_info($key)->{"_filter_info"}
Truth table33%1/3
A and B
A= $self->has_column($key)B= exists $self->column_info($key)->{"_filter_info"}
ABresult
0X0
100
111
125 $self->has_column($key)
126 &&
127 exists $self->column_info($key)->{_filter_info}
128 ) {
129 1 $self->set_filtered_column($key, delete $attrs->{$key});
130  
131 # FIXME update() reaches directly into the object-hash
132 # and we may *not* have a filtered value there - thus
133 # the void-ctx filter-trigger
134 1 ▶ $self->get_column($key) unless exists $self->{_column_data}{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_column_data"}{$key} 0 1 1
135 }
136 }
137  
138 5 return $self->next::method($attrs, @rest);
139 }
140  
141 sub new {
142 2 ▶ my ($class, $attrs, @rest) = @_;
Subroutine new: called CC=6 SCAR=19.3
Pod documented
143 my $source = $attrs->{-result_source}
144 2 ▶ or $class->throw_exception('Sourceless rows are not supported with DBIx::Class::FilterColumn');
Branch: 1 branch
BranchTrueFalseTotal
my $source = $attrs->{"-result_source"} or $class->throw_exception("Sourceless rows are not supported with DBIx::Class::FilterColumn") 0 2 2
145  
146 2 my $obj = $class->next::method($attrs, @rest);
147 2 ▶ foreach my $key (keys %{$attrs||{}}) {
Branch: 1 branch
BranchTrueFalseTotal
$attrs or {} 0 2 2
148 2 ▶ if ($obj->has_column($key) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->has_column($key) and exists $obj->column_info($key)->{"_filter_info"}) 2 0 2
Condition33%1/3
$obj->has_column($key) and exists $obj->column_info($key)->{"_filter_info"}
!ll&&!rl&&r
002
MC/DC0%0/2
$obj->has_column($key) and exists $obj->column_info($key)->{"_filter_info"}
$obj->has_column($key) exists $obj->column_info($key)->{"_filter_info"}
Truth table33%1/3
A and B
A= $obj->has_column($key)B= exists $obj->column_info($key)->{"_filter_info"}
ABresult
0X0
100
111
149 exists $obj->column_info($key)->{_filter_info} ) {
150 2 $obj->set_filtered_column($key, $attrs->{$key});
151 }
152 }
153  
154 2 return $obj;
155 }
156  
157 2 1;
158  
159 =head1 NAME
160  
161 DBIx::Class::FilterColumn - Automatically convert column data
162  
163 =head1 SYNOPSIS
164  
165 In your Schema or DB class add "FilterColumn" to the top of the component list.
166  
167 __PACKAGE__->load_components(qw( FilterColumn ... ));
168  
169 Set up filters for the columns you want to convert.
170  
171 __PACKAGE__->filter_column( money => {
172 filter_to_storage => 'to_pennies',
173 filter_from_storage => 'from_pennies',
174 });
175  
176 sub to_pennies { $_[1] * 100 }
177  
178 sub from_pennies { $_[1] / 100 }
179  
180 1;
181  
182  
183 =head1 DESCRIPTION
184  
185 This component is meant to be a more powerful, but less DWIM-y,
186 L<DBIx::Class::InflateColumn>. One of the major issues with said component is
187 that it B<only> works with references. Generally speaking anything that can
188 be done with L<DBIx::Class::InflateColumn> can be done with this component.
189  
190 =head1 METHODS
191  
192 =head2 filter_column
193  
194 __PACKAGE__->filter_column( colname => {
195 filter_from_storage => 'method'|\&coderef,
196 filter_to_storage => 'method'|\&coderef,
197 })
198  
199 This is the method that you need to call to set up a filtered column. It takes
200 exactly two arguments; the first being the column name the second being a hash
201 reference with C<filter_from_storage> and C<filter_to_storage> set to either
202 a method name or a code reference. In either case the filter is invoked as:
203  
204 $result->$filter_specification ($value_to_filter)
205  
206 with C<$filter_specification> being chosen depending on whether the
207 C<$value_to_filter> is being retrieved from or written to permanent
208 storage.
209  
210 If a specific directional filter is not specified, the original value will be
211 passed to/from storage unfiltered.
212  
213 =head2 get_filtered_column
214  
215 $obj->get_filtered_column('colname')
216  
217 Returns the filtered value of the column
218  
219 =head2 set_filtered_column
220  
221 $obj->set_filtered_column(colname => 'new_value')
222  
223 Sets the filtered value of the column
224  
225 =head1 EXAMPLE OF USE
226  
227 Some databases have restrictions on values that can be passed to
228 boolean columns, and problems can be caused by passing value that
229 perl considers to be false (such as C<undef>).
230  
231 One solution to this is to ensure that the boolean values are set
232 to something that the database can handle - such as numeric zero
233 and one, using code like this:-
234  
235 __PACKAGE__->filter_column(
236 my_boolean_column => {
237 filter_to_storage => sub { $_[1] ? 1 : 0 },
238 }
239 );
240  
241 In this case the C<filter_from_storage> is not required, as just
242 passing the database value through to perl does the right thing.
« DBIx/Class/InflateColumn.pm ↑ Summary DBIx/Class/Relationship/HasOne.pm »