blib/lib/DBIx/Class/Numeric.pm

filter: Statementstmt 94.2% 82 / 87 Branchbran 89.2% 25 / 28 Conditioncond 76.9% 10 / 13 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 80.0% 20 / 25 Podpod 33.3% 1 / 3 totaltotal 87.2% 143 / 164 CC 22 SCAR 14.2
CC
22
Cov
91%
_restrict_numeric
10.0
insert
6.2
numeric_columns
6.1
CRAP 4.1
↑ Summary
1 =head1 NAME
2  
3 DBIx::Class::Numeric - helper methods for numeric columns
4  
5 =head1 SYNOPSIS
6  
7 package MyApp::Schema::SomeTable;
8  
9 use base 'DBIx::Class';
10  
11 __PACKAGE__->load_components(qw/Numeric Core/); # Load the Numeric component
12 # Don't forget to load it *before* Core!
13 __PACKAGE__->add_columns(
14 qw/primary_id some_string num_col1 num_col2 restricted bounded lower_bound upper_bound/
15 );
16  
17 # Define 'simple' numeric cols, these will have some extra accessors & mutators
18 # created
19 __PACKAGE__->numeric_columns(qw/num_col1 num_col2/);
20
21 # Define min and max values for a column
22 __PACKAGE__->numeric_columns(restricted => {min_value => 10, max_value => 30});
23
24 # Define a column that's bound by the value of others
25 __PACKAGE__->numeric_columns(bounded => {lower_bound_col => 'lower_bound', upper_bound_col => 'upper_bound'});
26  
27 # ... meanwhile, after reading a record from the DB
28  
29 $row->increase_num_col1(5); # Add 5 to num_col1
30  
31 $row->decrease_num_col2(9); # Subtract 9 from num_col2
32  
33 $row->adjust_num_col1(-5); # Subtract 5 from num_col1
34 # (can be positive or negative, as can increase/decrease...
35 # adjust is just a clearer name...)
36  
37 $row->increment_num_col1; # Increment num_col1
38  
39 $row->decrement_num_col2; # Decrement num_col2
40
41 $row->restricted(40); # restricted col will be set to '30' since that's the max
42
43 $row->lower_bound(5);
44 $row->bounded(10); # bounded will be set to '5', since its lower bound was set to 5
45  
46 =head1 DESCRIPTION
47  
48 A DBIx::Class component that adds some extra accessors / mutators to any numeric columns
49 you define. Additionally, columns can have max and min values defined, or be bound to the
50 values of other columns in the table.
51  
52 =head1 METHODS
53  
54 =head2 numeric_columns(@cols)
55  
56 Call this method as you would add_columns(), and pass it a list of columns that are numeric. Note,
57 you need to pass the column names to add_columns() *and* numeric_columns().
58  
59 Any columns in this list will have extra accessors / mutators defined (see below).
60  
61 If the item in the list after a column name is a hashref, the hashref will define the arguments for
62 that numeric column. (If the next item's not a hashref, it's assumed to be the next column - you can
63 mix and match columns with and without arguments in the same call to numeric_colums().
64  
65 The valid keys in the argument hashref are:
66  
67 =over 4
68  
69 =item min_value / max_value
70  
71 These two keys define the minimum and/or maximum value of the column. If you attempt to set the column
72 to a value outside this range, it will be set to that min or max value accordingly.
73  
74 =item lower_bound_col / upper_bound_col
75  
76 If either of these are set to the name of a column in the same table, the numeric column will be
77 restricted in the same way as a min or max value, except the min/max value will be defined by the
78 value of the column specified.
79  
80 If the value of the lower or upper bound column changes, the bounded column won't be affected, until
81 its value is set. Eg. if your bounded column is currently 5, and you set it's lower_bound_col to
82 8 the bounded col won't change, even though it's below the minimum value. If you were to (eg) increment
83 the column, it would then be set to 8.
84  
85 =back
86  
87 =over
88  
89 =item WARNING
90  
91 Little (if any) validation is done on the list of cols passed to numeric_columns(). You could easily
92 pass it non-existant column names, etc. (This may be improved in a later release).
93  
94 In particular, no check is made to see if you are using incompatible combinations of min/max_value
95 and lower/uppper_bound_col (e.g. both a min_value and a lower_bound_col). Doing this is unsupported,
96 and may be prevented in the future (even thought it might 'kind of' work at the moment). You're free
97 to use compatible combinations, though, eg. a min_value and an upper_bound_col.
98  
99 =back
100  
101 =head2 increase_*, decrease_*, increment_*, decrement_*, adjust_*
102  
103 These 5 methods are added to your schema class for each column you pass to numeric_cols(). E.g. if
104 you have a numeric column called 'foo', you will automagically get methods called increment_foo(),
105 decrement_foo(), etc. They are fairly self-explanatory, with the possible exception of 'adjust_*'.
106 You can pass it either a positive or negative value to adjust the value of the column accordingly.
107  
108 =head1 AUTHOR
109  
110 Sam Crawley (Mutant) - mutant dot nz at gmail dot com
111  
112 =head1 LICENSE
113  
114 You may distribute this code under the same terms as Perl itself.
115  
116 =cut
117  
118 package DBIx::Class::Numeric;
119  
120 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
121 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
122  
123 3 our $VERSION = '0.004';
124  
125 3 ▶use base qw(DBIx::Class Class::Accessor::Grouped);
Subroutine BEGIN: called CC=1 SCAR=0.0
126  
127 3 __PACKAGE__->mk_group_accessors('inherited', '_numeric_col_def');
128  
129 3 ▶use Sub::Name ();
Subroutine BEGIN: called CC=1 SCAR=0.0
130  
131 sub numeric_columns {
132 7 ▶ my $self = shift;
Subroutine numeric_columns: called CC=6 SCAR=18.0
Pod documented
133 7 my @cols = @_;
134
135 7 my $count = 0;
136 7 my %def;
137  
138 7 foreach my $col (@cols) {
139 24 ▶ next if ref $col eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $col eq "HASH" 10 14 24
140
141 14 my $args = {};
142 14 ▶ if (ref $cols[$count+1] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cols[$count + 1] eq "HASH") 10 4 14
143 10 $args = $cols[$count+1];
144 }
145 14 $def{$col} = $args;
146
147 my %methods = (
148 adjust => sub {
149 1 ▶ _adjust($col, @_);
Subroutine __ANON__: called CC=1 SCAR=1.2
Subroutine adjust_lower: not called CC=1 SCAR=1.2
150 },
151 increase => sub {
152 1 ▶ _increase($col, @_);
Subroutine increase_bar: called CC=1 SCAR=1.2
Subroutine increase_lower: not called CC=1 SCAR=1.2
153 },
154 decrease => sub {
155 1 ▶ _decrease($col, @_);
Subroutine decrease_foo: called CC=1 SCAR=1.2
Subroutine decrease_both: not called CC=1 SCAR=1.2
156 },
157 increment => sub {
158 1 ▶ _increment($col, @_);
Subroutine __ANON__: called CC=1 SCAR=1.2
Subroutine increment_lower: not called CC=1 SCAR=1.2
159 },
160 decrement => sub {
161 1 ▶ _decrement($col, @_);
Subroutine decrement_foo: called CC=1 SCAR=1.2
Subroutine decrement_simple: not called CC=1 SCAR=1.2
162 }
163 14 );
164
165 14 ▶ while (my ($method_name, $subref) = each %methods) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %methods) 70 14 84
166 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
167 3 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
168
169 70 my $name = join '::', $self, "${method_name}_$col";
170 70 *$name = Sub::Name::subname($name, $subref);
171 }
172 }
173 continue {
174 24 $count++;
175 }
176
177 7 my $existing = $self->_numeric_col_def;
178 7 ▶ %def = (%$existing, %def) if $existing;
Branch: 1 branch
BranchTrueFalseTotal
if $existing 2 5 7
179
180 7 $self->_numeric_col_def(\%def);
181 }
182  
183 sub _increase {
184 5 ▶ my $col = shift;
Subroutine _increase: called CC=3 SCAR=11.4
185 5 my $self = shift;
186 5 my $increase = shift;
187  
188 5 ▶ $self->set_column($col, ($self->get_column($col) || 0) + ($increase || 0));
Condition50%2/4
$self->get_column($col) || 0
l!l
50
$increase || 0
l!l
50
MC/DC0%0/1
$self->get_column($col) || 0
$self->get_column($col)
MC/DC0%0/1
$increase || 0
$increase
Truth table50%1/2
A
A= $self->get_column($col)
Aresult
11
00
Truth table50%1/2
A
A= $increase
Aresult
11
00
189 }
190  
191 sub _decrease {
192 2 ▶ _increase($_[0], $_[1], -$_[2]);
Subroutine _decrease: called CC=1 SCAR=0.0
193 }
194  
195 sub _increment {
196 1 ▶ _increase($_[0], $_[1], 1);
Subroutine _increment: called CC=1 SCAR=0.0
197 }
198  
199 sub _decrement {
200 1 ▶ _decrease($_[0], $_[1], 1);
Subroutine _decrement: called CC=1 SCAR=0.0
201 }
202  
203 sub _adjust {
204 1 ▶ _increase(@_);
Subroutine _adjust: called CC=1 SCAR=0.0
205 }
206  
207 sub set_column {
208 12 ▶ my $self = shift;
Subroutine set_column: called CC=1 SCAR=0.0
Pod undocumented
209 12 my $column = shift;
210 12 my $new_val = shift;
211
212 12 $new_val = $self->_restrict_numeric($column, $new_val);
213  
214 12 return $self->next::method( $column, $new_val, @_ );
215 }
216  
217 sub insert {
218 2 ▶ my $self = shift;
Subroutine insert: called CC=6 SCAR=18.2
Pod undocumented
219
220 2 ▶ if (my $def = $self->_numeric_col_def) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $def = $self->_numeric_col_def) 2 0 2
221 2 foreach my $column (keys %$def) {
222 6 ▶ next unless $def->{$column} && %{ $def->{$column} };
Branch: 1 branch
BranchTrueFalseTotal
unless $def->{$column} and %{$def->{$column};} 1 5 6
Condition66%2/3
$def->{$column} and %{$def->{$column};}
!ll&&!rl&&r
015
MC/DC50%1/2
$def->{$column} and %{$def->{$column};}
$def->{$column} %{$def->{$column};}
Truth table66%2/3
A and B
A= $def->{$column}B= %{$def->{$column};}
ABresult
0X0
100
111
223
224 5 my $val = $self->get_column($column);
225
226 5 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 5 5
227
228 5 $self->set_column($column, $self->_restrict_numeric($column, $val));
229 }
230 }
231
232 2 return $self->next::method( @_ );
233 }
234  
235 sub _restrict_numeric {
236 17 ▶ my $self = shift;
Subroutine _restrict_numeric: called CC=10 SCAR=23.0
237 17 my $column = shift;
238 17 my $new_val = shift;
239
240 17 my $def = $self->_numeric_col_def;
241  
242 17 ▶ if ($def) {
Branch: 1 branch
BranchTrueFalseTotal
if ($def) 17 0 17
243 17 ▶ if (defined $def->{$column}{min_value} && $new_val < $def->{$column}{min_value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $def->{$column}{"min_value"} and $new_val < $def->{$column}{"min_value"}) 1 16 17
Condition100%3/3
defined $def->{$column}{"min_value"} and $new_val < $def->{$column}{"min_value"}
!ll&&!rl&&r
1421
MC/DC100%2/2
defined $def->{$column}{"min_value"} and $new_val < $def->{$column}{"min_value"}
defined $def->{$column}{"min_value"} $new_val < $def->{$column}{"min_value"}
Truth table100%3/3
A and B
A= defined $def->{$column}{"min_value"}B= $new_val < $def->{$column}{"min_value"}
ABresult
0X0
100
111
244 1 $new_val = $def->{$column}{min_value};
245 }
246 17 ▶ if (defined $def->{$column}{max_value} && $new_val > $def->{$column}{max_value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $def->{$column}{"max_value"} and $new_val > $def->{$column}{"max_value"}) 3 14 17
Condition100%3/3
defined $def->{$column}{"max_value"} and $new_val > $def->{$column}{"max_value"}
!ll&&!rl&&r
1133
MC/DC100%2/2
defined $def->{$column}{"max_value"} and $new_val > $def->{$column}{"max_value"}
defined $def->{$column}{"max_value"} $new_val > $def->{$column}{"max_value"}
Truth table100%3/3
A and B
A= defined $def->{$column}{"max_value"}B= $new_val > $def->{$column}{"max_value"}
ABresult
0X0
100
111
247 3 $new_val = $def->{$column}{max_value};
248 }
249 17 ▶ if (defined $def->{$column}{upper_bound_col}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $def->{$column}{"upper_bound_col"}) 6 11 17
250 6 my $max_val = $self->get_column($def->{$column}{upper_bound_col});
251 6 ▶ $new_val = $max_val if $new_val > $max_val;
Branch: 1 branch
BranchTrueFalseTotal
if $new_val > $max_val 3 3 6
252 }
253 17 ▶ if (defined $def->{$column}{lower_bound_col}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $def->{$column}{"lower_bound_col"}) 6 11 17
254 6 my $min_val = $self->get_column($def->{$column}{lower_bound_col});
255 6 ▶ $new_val = $min_val if $new_val < $min_val;
Branch: 1 branch
BranchTrueFalseTotal
if $new_val < $min_val 3 3 6
256 }
257 }
258
259 17 return $new_val;
260 }
261  
262  
263 3 1;
↑ Summary