BitField.pm

filter: Statementstmt 77.8% 88 / 113 Branchbran 84.2% 32 / 38 Conditioncond 65.0% 26 / 40 MC/DCmcdc 45.0% 9 / 20 Subroutinesub 93.3% 14 / 15 Podpod 0.0% 0 / 4 totaltotal 73.4% 169 / 230 CC 73 SCAR 20.3
CC
73
Cov
76%
register_column
14.6
register_column
14.6
store_column
9.1
CRAP 7.6
« ResultSet/BitField.pm ↑ Summary
1 package DBIx::Class::BitField;
2 our $VERSION = '0.13';
3  
4  
5 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 6 ▶use base 'DBIx::Class';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 35 ▶sub register_column {
Subroutine register_column: called CC=7 SCAR=26.8
Pod undocumented
13 0 ▶ my ($self, $column, $info, @rest) = @_;
Subroutine register_column: not called CC=7 SCAR=26.8
Pod undocumented
14
15 0 ▶ return $self->next::method($column, $info, @rest)
Branch: 2 branches
BranchTrueFalseTotal
unless $self->__is_bitfield($info) 0 0 0
unless $self->__is_bitfield($info) 15 20 35
16 unless($self->__is_bitfield($info));
17
18 0 ▶ $info->{accessor} ||= '_'.$column;
Condition50%2/4
$info->{"accessor"} ||= '_' . $column
l!l
00
$info->{"accessor"} ||= '_' . $column
l!l
515
MC/DC0%0/1
$info->{"accessor"} ||= '_' . $column
$info->{"accessor"}
MC/DC100%1/1
$info->{"accessor"} ||= '_' . $column
$info->{"accessor"}
Truth table0%0/2
A
A= $info->{"accessor"}
Aresult
11
00
Truth table100%2/2
A
A= $info->{"accessor"}
Aresult
11
00
19 0 $info->{default_value} = 0;
20 0 $info->{is_numeric} = 0;
21
22 0 $self->next::method($column, $info, @rest);
23
24 0 ▶ my $prefix = $info->{bitfield_prefix} || q{};
Condition50%2/4
$info->{'bitfield_prefix'} || ''
l!l
00
$info->{'bitfield_prefix'} || ''
l!l
515
MC/DC0%0/1
$info->{'bitfield_prefix'} || ''
$info->{'bitfield_prefix'}
MC/DC100%1/1
$info->{'bitfield_prefix'} || ''
$info->{'bitfield_prefix'}
Truth table0%0/2
A
A= $info->{'bitfield_prefix'}
Aresult
11
00
Truth table100%2/2
A
A= $info->{'bitfield_prefix'}
Aresult
11
00
25
26 0 my @fields = @{$info->{bitfield}};
27
28
29 {
30 0 my $i = 0;
31 6 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 0 foreach my $field (@fields) {
33 0 ▶ if($self->can($prefix.$field) && 1 == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if () 0 0 0
if () 0 50 50
Condition25%1/4
$self->can($prefix . $field) and
!ll
00
$self->can($prefix . $field) and
!ll
500
MC/DC0%0/1
$self->can($prefix . $field) and
$self->can($prefix . $field)
MC/DC0%0/1
$self->can($prefix . $field) and
$self->can($prefix . $field)
Truth table0%0/2
A
A= $self->can($prefix . $field)
Aresult
00
11
Truth table50%1/2
A
A= $self->can($prefix . $field)
Aresult
00
11
34 0 carp 'Bitfield accessor '.$prefix.$field.' cannot be created since there is an accessor of that name already';
35 0 $i++;
36 0 next;
37 }
38 0 my $bit = 2**$i;
39 0 ▶ *{$self.'::'.$prefix.$field} = sub { shift->__bitfield_item($field, $bit, $info->{accessor}, @_) };
Subroutine __ANON__: called CC=1 SCAR=0.6
40 0 $i++;
41 }
42
43 0 ▶ *{$self.'::'.$column} = sub { shift->__bitfield($column, $info->{accessor}, \@fields, @_) };
Subroutine __ANON__: called CC=1 SCAR=0.6
44 }
45
46
47 }
48  
49 sub __is_bitfield {
50 368 ▶ my ($self, $info) = @_;
Subroutine __is_bitfield: called CC=3 SCAR=11.0
51 368 ▶ return defined $info->{data_type} && $info->{data_type} =~ /^int/xsmi && ref $info->{bitfield} eq "ARRAY";
Condition83%5/6
defined $info->{'data_type'} && $info->{'data_type'} =~ /^int/imsx && ref $info->{'bitfield'} eq 'ARRAY'
!ll&&!rl&&r
1284272
defined $info->{"data_type"} and $info->{"data_type"} =~ /^int/imsx
!ll&&!rl&&r
120356
MC/DC66%2/3
defined $info->{'data_type'} && $info->{'data_type'} =~ /^int/imsx && ref $info->{'bitfield'} eq 'ARRAY'
defined $info->{"data_type"} $info->{"data_type"} =~ /^int/imsx ref $info->{'bitfield'} eq 'ARRAY'
Truth table75%3/4
A and B && C
A= defined $info->{"data_type"}B= $info->{"data_type"} =~ /^int/imsxC= ref $info->{'bitfield'} eq 'ARRAY'
ABCresult
0XX0
10X0
1100
1111
52 }
53  
54  
55 sub __is_bitfield_item {
56 112 ▶ my ($self, $column) = @_;
Subroutine __is_bitfield_item: called CC=9 SCAR=22.0
57 112 ▶ return if($self->has_column($column));# || $self->is_relationship($column));
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_column($column) 65 47 112
58 47 foreach my $c ($self->columns) {
59 134 my $info = $self->column_info($c);
60 134 ▶ next unless($self->__is_bitfield($info));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->__is_bitfield($info) 47 87 134
61 87 ▶ return $c if(grep { $_ eq $column } @{$info->{bitfield} || []});
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $column;} @{$info->{"bitfield"} or [];} 6 81 87
Condition50%1/2
$info->{'bitfield'} || []
l!l
870
MC/DC0%0/1
$info->{'bitfield'} || []
$info->{'bitfield'}
Truth table50%1/2
A
A= $info->{'bitfield'}
Aresult
11
00
62 81 ▶ my $prefix = $info->{bitfield_prefix} || '';
Condition100%2/2
$info->{'bitfield_prefix'} || ''
l!l
4041
MC/DC100%1/1
$info->{'bitfield_prefix'} || ''
$info->{'bitfield_prefix'}
Truth table100%2/2
A
A= $info->{'bitfield_prefix'}
Aresult
11
00
63 81 ▶ return $c if(grep { $prefix.$_ eq $column } @{$info->{bitfield} || []});
Branch: 1 branch
BranchTrueFalseTotal
if grep {$prefix . $_ eq $column;} @{$info->{"bitfield"} or [];} 2 79 81
Condition50%1/2
$info->{'bitfield'} || []
l!l
810
MC/DC0%0/1
$info->{'bitfield'} || []
$info->{'bitfield'}
Truth table50%1/2
A
A= $info->{'bitfield'}
Aresult
11
00
64 }
65 39 return;
66 }
67  
68 sub store_column {
69 197 ▶ my ($self, $column, $value) = @_;
Subroutine store_column: called CC=9 SCAR=22.1
Pod undocumented
70 197 my $info= $self->column_info($column);
71 197 ▶ if($self->__is_bitfield($info) && ($value !~ /^\d+$/ || int($value) ne $value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->__is_bitfield($info) and !($value =~ /^\d+$/) || int $value ne $value) 61 136 197
Condition83%5/6
$self->__is_bitfield($info) and !($value =~ /^\d+$/) || int $value ne $value
!ll&&!rl&&r
3410261
not $value =~ /^\d+$/ or int $value ne $value
l!l&&r!l&&!r
610102
MC/DC66%2/3
$self->__is_bitfield($info) and !($value =~ /^\d+$/) || int $value ne $value
$self->__is_bitfield($info) not $value =~ /^\d+$/ int $value ne $value
Truth table75%3/4
A and not(B) or C
A= $self->__is_bitfield($info)B= not $value =~ /^\d+$/C= int $value ne $value
ABCresult
0XX0
1000
11X1
1011
72 61 ▶ if(ref $value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 20 41 61
73 20 ▶ foreach my $bit (@{$value || []}) {
Condition50%1/2
$value || []
l!l
200
MC/DC0%0/1
$value || []
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
74 40 ▶ $self->can($bit) ? $self->$bit(1) : croak qq(bitfield item '$bit' does not exist);
Branch: 1 branch
BranchTrueFalseTotal
$self->can($bit) ? : 40 0 40
75 }
76 } else {
77 41 ▶ $self->can($value) ? $self->$value(1) : croak qq(bitfield item '$value' does not exist);
Branch: 1 branch
BranchTrueFalseTotal
$self->can($value) ? : 40 1 41
78 }
79 60 my $accessor = $info->{accessor};
80 60 $value = $self->$accessor;
81 }
82 196 $self->next::method($column, $value);
83 }
84  
85 sub __bitfield_item {
86 235 ▶ my ($self, $field, $bit, $accessor, $set) = @_;
Subroutine __bitfield_item: called CC=5 SCAR=16.1
87 235 ▶ my $value = $self->$accessor || 0;
Condition100%2/2
$self->$accessor || 0
l!l
21223
MC/DC100%1/1
$self->$accessor || 0
$self->$accessor
Truth table100%2/2
A
A= $self->$accessor
Aresult
11
00
88 235 ▶ return ($value | $bit) == $value ? 1 : 0 unless defined $set;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $set 145 90 235
($value | $bit) == $value ? : 144 1 145
89
90 90 ▶ $self->$accessor($set ? $value | $bit | $bit : $value - ($value & $bit));
Branch: 1 branch
BranchTrueFalseTotal
$set ? : 89 1 90
91 90 return $set;
92 }
93  
94 sub __bitfield {
95 22 ▶ my ($self, $column, $accessor, $fields) = @_;
Subroutine __bitfield: called CC=4 SCAR=13.9
96
97 22 ▶ my $value = $self->$accessor || return [];
Condition100%2/2
$self->$accessor || (return [])
l!l
211
MC/DC100%1/1
$self->$accessor || (return [])
$self->$accessor
Truth table100%2/2
A
A= $self->$accessor
Aresult
11
00
98
99 21 my @fields = ();
100 21 my $i = 0;
101 21 foreach my $field (@{$fields}) {
102 63 ▶ push(@fields, $field) if(($value | 2**$i) == $value);
Branch: 1 branch
BranchTrueFalseTotal
if ($value | 2 ** $i) == $value 42 21 63
103 63 $i++;
104 }
105
106 21 return \@fields;
107 }
108  
109 sub new {
110 39 ▶ my ($self, $data, @rest) = @_;
Subroutine new: called CC=6 SCAR=18.0
Pod undocumented
111 39 my $bits = {};
112 39 ▶ while(my ($column, $value) = each %{$data || {}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$data or {};}) 108 39 147
Condition50%1/2
$data || {}
l!l
1470
MC/DC0%0/1
$data || {}
$data
Truth table50%1/2
A
A= $data
Aresult
11
00
113 108 ▶ next unless(my $bitfield = $self->__is_bitfield_item($column));
Branch: 1 branch
BranchTrueFalseTotal
unless my $bitfield = $self->__is_bitfield_item($column) 104 4 108
114 4 $bits->{$column} = $value;
115 4 delete $data->{$column};
116 }
117 39 my $row = $self->next::method($data, @rest);
118
119 38 ▶ while(my ($column, $value) = each %{$bits || {}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$bits or {};}) 4 38 42
Condition50%1/2
$bits || {}
l!l
420
MC/DC0%0/1
$bits || {}
$bits
Truth table50%1/2
A
A= $bits
Aresult
11
00
120 4 $row->$column($value);
121 }
122
123
124 38 return $row;
125 }
126  
127 1;
128  
129  
130  
131  
132 =pod
133  
134 =head1 NAME
135  
136 DBIx::Class::BitField - Store multiple boolean fields in one integer field
137  
138 =head1 VERSION
139  
140 version 0.13
141  
142 =head1 SYNOPSIS
143  
144 package MySchema::Item;
145  
146 use base 'DBIx::Class';
147  
148 __PACKAGE__->load_components(qw(BitField Core));
149  
150 __PACKAGE__->table('item');
151  
152 __PACKAGE__->add_columns(
153 id => { data_type => 'integer' },
154 status => { data_type => 'integer',
155 bitfield => [qw(active inactive foo bar)],
156 },
157 advanced_status => { data_type => 'integer',
158 bitfield => [qw(1 2 3 4)],
159 bitfield_prefix => 'status_',
160 accessor => '_foobar',
161 is_nullable => 1,
162 },
163  
164 );
165  
166 __PACKAGE__->set_primary_key('id');
167  
168 __PACKAGE__->resultset_class('DBIx::Class::ResultSet::BitField');
169  
170 1;
171  
172 Somewhere in your code:
173  
174 my $rs = $schema->resultset('Item');
175 my $item = $rs->create({
176 status => [qw(active foo)],
177 advanced_status => [qw(status_1 status_3)],
178 });
179  
180 $item2 = $rs->create({
181 active => 1,
182 foo => 1,
183 status_1 => 1,
184 status_3 => 1,
185 });
186  
187 # $item->active == 1
188 # $item->foo == 1
189 # $item->status == ['active', 'foo']
190 # $item->_status == 5
191 # $item->status_1 == 1
192 # $item->status_3 == 1
193  
194 $item->foo(0);
195 $item->update;
196  
197 =head1 DESCRIPTION
198  
199 This module is useful if you manage data which has a lot of on/off attributes like I<active, inactive, deleted, important, etc.>.
200 If you do not want to add an extra column for each of those attributes you can easily specify them in one C<integer> column.
201  
202 A bit field is a way to store multiple bit values on one integer field.
203  
204 =for html <p>Read <a href="http://en.wikipedia.org/wiki/Bit_field">this wikipedia article</a> for more information on that topic.</p>
205  
206 The main benefit from this module is that you can add additional attributes to your result class whithout the need to
207 deploy or change the schema on the data base.
208  
209 B<This module encourages to not normalize your schema. You should consider a C<has_many> relationship to a table which holds
210 all the flags instead of this module.>
211  
212 =head2 Example
213  
214 A bit field C<status> with C<data_type> set to C<int> or C<integer> (case insensitive) and C<active, inactive, deleted> will create
215 the following accessors:
216  
217 =over
218  
219 =item C<< $row->status >>
220  
221 This is B<not> the value which is stored in the database. This accessor returns the status as an array ref.
222 The array ref is empty if no status is applied.
223  
224 You can use this method to set the value as well:
225  
226 $row->status(['active', 'inactive']);
227 # $row->status == ['active', 'inactive']
228  
229 =item C<< $row->active >>, C<< $row->inactive >>, C<< $row->deleted >>
230  
231 These accessors return either C<1> or C<0>. If you add a parameter they will act like normal column accessors by returning that value.
232  
233 my $foo = $row->active(1);
234 # $foo == 1
235 # $row->active == 1
236 # $row->status == ['active']
237  
238 =item C<< $row->_status >>
239  
240 This accessor will hold the internal integer representation of the bit field.
241  
242 $row->status(['active', 'inactive']);
243 # $row->_status == 3
244  
245 You can change the name of the accessor via the C<accessor> attribute:
246  
247 __PACKAGE__->add_columns(
248 status => { data_type => 'integer',
249 bitfield => [qw(active inactive deleted)],
250 accessor => '_status_accessor',
251 },
252 );
253  
254 =back
255  
256 =head2 ResultSet operations
257  
258 In order to use result set operations like C<search> or C<update> you need to set the result set class to
259 C<DBIx::Class::ResultSet::BitField> or to a class which inherits from it.
260  
261 __PACKAGE__->resultset_class('DBIx::Class::ResultSet::BitField');
262  
263 =head3 update
264  
265 $rs->update({ status => ['active'] });
266  
267 This will update the status of all items in the result to C<active>. This is done in a single SQL query.
268  
269 =head3 search_bitfield
270  
271 To search a result set for a specific value of the bitfield use C<search_bitfield>.
272  
273 You can either make a OR search:
274  
275 my $new_rs = $rs->search_bitfield([ status2 => 1, status3 => 1 ]);
276  
277 or AND:
278  
279 my $new_rs = $rs->search_bitfield({ status2 => 1, status3 => 1 });
280  
281 This method uses bitwise operators in SQL. Depending on your database it is possible to create an index so
282 the search is as fast as using a single boolean column.
283 =head1 AUTHOR
284  
285 Moritz Onken <onken@netcubed.de>
286  
287 =head1 COPYRIGHT AND LICENSE
288  
289 This software is Copyright (c) 2009 by Moritz Onken.
290  
291 This is free software, licensed under:
292  
293 The (three-clause) BSD License
294  
295 =cut
296  
297  
298  
299 __END__
« ResultSet/BitField.pm ↑ Summary