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