CalculatedValue.pm

filter: Statementstmt 100% 141 / 141 Branchbran 97.6% 41 / 42 Conditioncond 95.8% 23 / 24 MC/DCmcdc 91.6% 11 / 12 Subroutinesub 100% 31 / 31 Podpod 100% 15 / 15 totaltotal 98.8% 262 / 265 CC 39 SCAR 13.9
CC
39
Cov
99%
replace_adjustment
8.0
new
7.0
include_adjustment
5.0
CRAP 4.0
↑ Summary CalculatedValue/Validatable.pm »
1 package Math::Util::CalculatedValue;
2  
3 5 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use List::Util qw(min max);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Math::Util::CalculatedValue - math adjustment, which can containe another adjustments
13  
14 =head1 DESCRIPTION
15  
16 Represents an adjustment to a value (which can contain additional adjustments).
17  
18 =cut
19  
20 5 our $VERSION = '0.07';
21  
22 =head1 SYNOPSIS
23  
24 my $tid = Math::Util::CalculatedValue->new({
25 name => 'time_in_days',
26 description => 'Duration in days',
27 set_by => 'Contract',
28 base_amount => 0,
29 });
30  
31 my $tiy = Math::Util::CalculatedValue->new({
32 name => 'time_in_years',
33 description => 'Duration in years',
34 set_by => 'Contract',
35 base_amount => 1,
36 });
37  
38 my $dpy = Math::Util::CalculatedValue->new({
39 name => 'days_per_year',
40 description => 'days in a year',
41 set_by => 'Contract',
42 base_amount => 365,
43 });
44  
45 $tid->include_adjustment('reset', $tiy);
46 $tid->include_adjustment('multiply', $dpy);
47  
48 print $tid->amount;
49  
50 =head1 ATTRIBUTES
51  
52 =head2 name
53  
54 This is the name of the operation which called this module
55  
56 =cut
57  
58 173 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod documented
59 173 my ($self) = @_;
60 173 return $self->{'name'};
61 }
62  
63 =head2 description
64  
65 This is the description of the operation which called this module
66  
67 =cut
68  
69 1 ▶sub description {
Subroutine description: called CC=1 SCAR=0.0
Pod documented
70 1 my ($self) = @_;
71 1 return $self->{'description'};
72 }
73  
74 =head2 set_by
75  
76 This is the name of the module which called this module
77  
78 =cut
79  
80 1 ▶sub set_by {
Subroutine set_by: called CC=1 SCAR=0.0
Pod documented
81 1 my ($self) = @_;
82 1 return $self->{'set_by'};
83 }
84  
85 =head2 base_amount
86  
87 This is the base amount on which the adjustments are to be made
88  
89 =cut
90  
91 4 ▶sub base_amount {
Subroutine base_amount: called CC=2 SCAR=6.9
Pod documented
92 4 my ($self) = @_;
93 4 ▶ return $self->{'base_amount'} || 0;
Condition100%2/2
$self->{'base_amount'} || 0
l!l
31
MC/DC100%1/1
$self->{'base_amount'} || 0
$self->{'base_amount'}
Truth table100%2/2
A
A= $self->{'base_amount'}
Aresult
11
00
94 }
95  
96 =head2 metadata
97  
98 Additional information that you wish to include.
99  
100 =cut
101  
102 1 ▶sub metadata {
Subroutine metadata: called CC=1 SCAR=0.0
Pod documented
103 1 my ($self) = @_;
104 1 return $self->{'metadata'};
105 }
106  
107 =head2 minimum
108  
109 The minimum value for amount
110  
111 =cut
112  
113 1 ▶sub minimum {
Subroutine minimum: called CC=1 SCAR=0.0
Pod documented
114 1 my ($self) = @_;
115 1 return $self->{'minimum'};
116 }
117  
118 =head2 maximum
119  
120 The maximum value for amount
121  
122 =cut
123  
124 1 ▶sub maximum {
Subroutine maximum: called CC=1 SCAR=0.0
Pod documented
125 1 my ($self) = @_;
126 1 return $self->{'maximum'};
127 }
128  
129 my %available_adjustments = (
130 20 ▶ 'add' => sub { my ($this, $prev) = @_; return $prev + $this->amount; },
Subroutine __ANON__: called CC=1 SCAR=0.0
131 21 ▶ 'multiply' => sub { my ($this, $prev) = @_; return $prev * $this->amount; },
Subroutine __ANON__: called CC=1 SCAR=0.0
132 17 ▶ 'subtract' => sub { my ($this, $prev) = @_; return $prev - $this->amount; },
Subroutine __ANON__: called CC=1 SCAR=0.0
133 11 ▶ 'divide' => sub { my ($this, $prev) = @_; return $prev / $this->amount; },
Subroutine __ANON__: called CC=1 SCAR=0.0
134 19 ▶ 'reset' => sub { my ($this, $prev) = @_; return $this->amount; },
Subroutine __ANON__: called CC=1 SCAR=0.0
135 8 ▶ 'exp' => sub { my ($this, $prev) = @_; return exp($this->amount); },
Subroutine __ANON__: called CC=1 SCAR=0.0
136 7 ▶ 'log' => sub { my ($this, $prev) = @_; return log($this->amount); },
Subroutine __ANON__: called CC=1 SCAR=0.0
137 38 ▶ 'info' => sub { my ($this, $prev) = @_; return $prev; },
Subroutine __ANON__: called CC=1 SCAR=0.0
138 1 ▶ 'absolute' => sub { my ($this, $prev) = @_; return abs($this->amount); },
Subroutine __ANON__: called CC=1 SCAR=0.0
139 5 );
140  
141 =head1 Methods
142  
143 =head2 new
144  
145 New instance method
146  
147 =cut
148  
149 37 ▶sub new { ## no critic (ArgUnpacking)
Subroutine new: called CC=7 SCAR=19.5
Pod documented
150 37 my $class = shift;
151 37 ▶ my %params_ref = ref($_[0]) ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 37 0 37
152  
153 37 foreach my $required ('name', 'description', 'set_by') {
154 confess "Attribute $required is required"
155 90 ▶ unless $params_ref{$required};
Branch: 1 branch
BranchTrueFalseTotal
unless $params_ref{$required} 14 76 90
156 }
157  
158 23 my $self = \%params_ref;
159 23 my $minimum = $self->{'minimum'};
160 23 my $maximum = $self->{'maximum'};
161  
162 23 ▶ confess "Provided maximum [$maximum] is less than the provided minimum [$minimum]"
Branch: 1 branch
BranchTrueFalseTotal
if defined $minimum and defined $maximum and $maximum < $minimum 2 21 23
Condition100%6/6
defined $minimum and defined $maximum and $maximum < $minimum
!ll&&!rl&&r
1832
defined $minimum and defined $maximum
!ll&&!rl&&r
1715
MC/DC100%3/3
defined $minimum and defined $maximum and $maximum < $minimum
defined $minimum defined $maximum $maximum < $minimum
Truth table100%4/4
A and B and C
A= defined $minimumB= defined $maximumC= $maximum < $minimum
ABCresult
0XX0
10X0
1100
1111
163 if (defined $minimum
164 and defined $maximum
165 and $maximum < $minimum);
166  
167 21 $self->{'calculatedValue'} = 1;
168  
169 21 my $obj = bless $self, $class;
170 21 return $obj;
171 }
172  
173 =head2 amount
174  
175 This is the final amount from this object, after applying all adjustments.
176  
177 =cut
178  
179 134 ▶sub amount {
Subroutine amount: called CC=4 SCAR=13.9
Pod documented
180 134 my $self = shift;
181  
182 134 my $value = $self->_verified_cached_value;
183 134 ▶ if (not defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 45 89 134
184 45 $value = $self->_apply_all_adjustments;
185 45 my $min = $self->{'minimum'};
186 45 ▶ $value = max($min, $value) if (defined $min);
Branch: 1 branch
BranchTrueFalseTotal
if defined $min 6 39 45
187 45 my $max = $self->{'maximum'};
188 45 ▶ $value = min($max, $value) if (defined $max);
Branch: 1 branch
BranchTrueFalseTotal
if defined $max 6 39 45
189  
190 45 $self->{_cached_amount} = $value;
191 }
192  
193 134 return $value;
194 }
195  
196 =head2 adjustments
197  
198 The ordered adjustments (if any) applied to arrive at the final value.
199  
200 =cut
201  
202 3 ▶sub adjustments {
Subroutine adjustments: called CC=2 SCAR=6.9
Pod documented
203 3 my ($self) = @_;
204 3 ▶ return $self->{'_adjustments'} || [];
Condition100%2/2
$self->{'_adjustments'} || []
l!l
12
MC/DC100%1/1
$self->{'_adjustments'} || []
$self->{'_adjustments'}
Truth table100%2/2
A
A= $self->{'_adjustments'}
Aresult
11
00
205 }
206  
207 =head2 include_adjustment
208  
209 Creates the ordered adjustments as per the operation.
210  
211 =cut
212  
213 23 ▶sub include_adjustment {
Subroutine include_adjustment: called CC=5 SCAR=16.1
Pod documented
214 23 my ($self, $operation, $adjustment) = @_;
215  
216 confess 'Operation [' . $operation . '] is not supported by ' . __PACKAGE__
217 23 ▶ unless ($available_adjustments{$operation});
Branch: 1 branch
BranchTrueFalseTotal
unless $available_adjustments{$operation} 1 22 23
218 22 ▶ confess 'Supplied adjustment must be type of ' . __PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
unless ref $adjustment 1 21 22
219 if !ref($adjustment);
220 confess 'Supplied adjustment must be type of' . __PACKAGE__
221 21 ▶ if !$adjustment->{calculatedValue};
Branch: 1 branch
BranchTrueFalseTotal
unless $adjustment->{"calculatedValue"} 1 20 21
222  
223 20 delete $self->{_cached_amount};
224 20 ▶ my $adjustments = $self->{'_adjustments'} || [];
Condition100%2/2
$self->{'_adjustments'} || []
l!l
137
MC/DC100%1/1
$self->{'_adjustments'} || []
$self->{'_adjustments'}
Truth table100%2/2
A
A= $self->{'_adjustments'}
Aresult
11
00
225 20 push @{$adjustments}, [$operation, $adjustment];
226 20 return $self->{'_adjustments'} = $adjustments;
227 }
228  
229 =head2 exclude_adjustment
230  
231 Remove an adjustment by name. Returns the number of instances found and excluded.
232  
233 Excluded items are changed into 'info' so that that still show up but are do not alter the parent value
234  
235 THis can be extremely dangerous, so make sure you know where and why you are doing it.
236  
237 =cut
238  
239 63 ▶sub exclude_adjustment {
Subroutine exclude_adjustment: called CC=5 SCAR=16.1
Pod documented
240 63 my ($self, $adj_name) = @_;
241  
242 63 my $excluded = 0;
243 63 ▶ my $adjustments = $self->{'_adjustments'} || [];
Condition100%2/2
$self->{'_adjustments'} || []
l!l
3330
MC/DC100%1/1
$self->{'_adjustments'} || []
$self->{'_adjustments'}
Truth table100%2/2
A
A= $self->{'_adjustments'}
Aresult
11
00
244 63 foreach my $sub_adj (@{$adjustments}) {
245 60 my $obj = $sub_adj->[1];
246 60 $excluded += $obj->exclude_adjustment($adj_name);
247 60 ▶ if ($obj->name eq $adj_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->name eq $adj_name) 30 30 60
248 30 $sub_adj->[0] = 'info';
249 30 $excluded++;
250 }
251 }
252  
253 63 ▶ delete $self->{_cached_amount} if ($excluded);
Branch: 1 branch
BranchTrueFalseTotal
if $excluded 23 40 63
254  
255 63 return $excluded;
256 }
257  
258 =head2 replace_adjustment
259  
260 Replace all instances of the same named adjustment with the provided adjustment
261  
262 Returns the number of instances replaced.
263  
264 =cut
265  
266 30 ▶sub replace_adjustment {
Subroutine replace_adjustment: called CC=8 SCAR=20.8
Pod documented
267 30 my ($self, $replacement) = @_;
268  
269 30 ▶ confess 'Supplied replacement must be type of ' . __PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
unless ref $replacement 1 29 30
270 if !ref($replacement);
271  
272 confess 'Supplied replacement must be type of' . __PACKAGE__
273 29 ▶ if !$replacement->{calculatedValue};
Branch: 1 branch
BranchTrueFalseTotal
unless $replacement->{"calculatedValue"} 1 28 29
274  
275 28 my $replaced = 0;
276 28 ▶ my $adjustments = $self->{'_adjustments'} || [];
Condition100%2/2
$self->{'_adjustments'} || []
l!l
253
MC/DC100%1/1
$self->{'_adjustments'} || []
$self->{'_adjustments'}
Truth table100%2/2
A
A= $self->{'_adjustments'}
Aresult
11
00
277 28 foreach my $sub_adj (@{$adjustments}) {
278 43 my $obj = $sub_adj->[1];
279 43 ▶ $replaced += $obj->replace_adjustment($replacement)
Branch: 1 branch
BranchTrueFalseTotal
if $obj != $replacement 25 18 43
280 if ($obj != $replacement);
281 43 ▶ if ($obj->name eq $replacement->name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->name eq $replacement->name) 21 22 43
282 21 $sub_adj->[1] = $replacement;
283 21 $replaced++;
284 }
285 }
286  
287 28 ▶ delete $self->{_cached_amount} if ($replaced);
Branch: 1 branch
BranchTrueFalseTotal
if $replaced 25 3 28
288  
289 28 return $replaced;
290 }
291  
292 # Loops through the ordered adjustments and performs the operation/adjustment
293 45 ▶sub _apply_all_adjustments {
Subroutine _apply_all_adjustments: called CC=4 SCAR=13.9
294 45 my ($self) = @_;
295 45 ▶ my $value = $self->{'base_amount'} || 0;
Condition50%1/2
$self->{'base_amount'} || 0
l!l
450
MC/DC0%0/1
$self->{'base_amount'} || 0
$self->{'base_amount'}
Truth table50%1/2
A
A= $self->{'base_amount'}
Aresult
11
00
296 45 ▶ my $adjustments = $self->{'_adjustments'} || [];
Condition100%2/2
$self->{'_adjustments'} || []
l!l
369
MC/DC100%1/1
$self->{'_adjustments'} || []
$self->{'_adjustments'}
Truth table100%2/2
A
A= $self->{'_adjustments'}
Aresult
11
00
297 45 foreach my $adjustment (@{$adjustments}) {
298 142 $value = $available_adjustments{$adjustment->[0]}->($adjustment->[1], $value);
299 }
300 45 return $value;
301 }
302  
303 204 ▶sub _verified_cached_value {
Subroutine _verified_cached_value: called CC=5 SCAR=16.1
304 204 my ($self) = @_;
305 204 my $can;
306 204 ▶ if (exists $self->{_cached_amount}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_cached_amount"}) 154 50 204
307 154 $can = $self->{_cached_amount};
308 154 ▶ my $adjustments = $self->{'_adjustments'} || [];
Condition100%2/2
$self->{'_adjustments'} || []
l!l
44110
MC/DC100%1/1
$self->{'_adjustments'} || []
$self->{'_adjustments'}
Truth table100%2/2
A
A= $self->{'_adjustments'}
Aresult
11
00
309 154 foreach my $adjustment (@{$adjustments}) {
310 62 ▶ if (not defined $adjustment->[-1]->_verified_cached_value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $adjustment->[-1]->_verified_cached_value) 4 58 62
311 4 delete $self->{_cached_amount};
312 4 $can = undef;
313 4 last;
314 }
315 }
316 }
317 204 return $can;
318 }
319  
320 =head2 peek
321  
322 Peek at an included adjustment by name.
323  
324 =cut
325  
326 20 ▶sub peek {
Subroutine peek: called CC=5 SCAR=16.1
Pod documented
327 20 my ($self, $adj_name) = @_;
328  
329 20 my $picked;
330  
331 20 ▶ if ($self->name eq $adj_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->name eq $adj_name) { } 3 17 20
332 3 $picked = $self;
333 } else {
334 # Depth first traversal. We assume that if there are two things named the same
335 # in any given CV that they are, in fact, the same value. So we can just return the first one we find.
336 17 ▶ my $adjustments = $self->{'_adjustments'} || [];
Condition100%2/2
$self->{'_adjustments'} || []
l!l
512
MC/DC100%1/1
$self->{'_adjustments'} || []
$self->{'_adjustments'}
Truth table100%2/2
A
A= $self->{'_adjustments'}
Aresult
11
00
337 17 foreach my $sub_adj (@{$adjustments}) {
338 14 my $obj = $sub_adj->[1];
339 14 $picked = $obj->peek($adj_name);
340 14 ▶ last if $picked;
Branch: 1 branch
BranchTrueFalseTotal
if $picked 2 12 14
341 }
342 }
343  
344 20 return $picked;
345 }
346  
347 =head2 peek_amount
348  
349 Peek at the value of an included adjustment by name.
350  
351 =cut
352  
353 3 ▶sub peek_amount {
Subroutine peek_amount: called CC=2 SCAR=6.9
Pod documented
354 3 my ($self, $adj_name) = @_;
355 3 my $adj = $self->peek($adj_name);
356 3 ▶ return ($adj) ? $adj->amount : undef;
Branch: 1 branch
BranchTrueFalseTotal
$adj ? : 1 2 3
357 }
358  
359 =head1 AUTHOR
360  
361 binary.com, C<< <rakesh at binary.com> >>
362  
363 =head1 BUGS
364  
365 Please report any bugs or feature requests to C<bug-math-util-calculatedvalue at rt.cpan.org>, or through
366 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Math-Util-CalculatedValue>. I will be notified, and then you'll
367 automatically be notified of progress on your bug as I make changes.
368  
369  
370 =head1 SUPPORT
371  
372 You can find documentation for this module with the perldoc command.
373  
374 perldoc Math::Util::CalculatedValue
375  
376  
377 You can also look for information at:
378  
379 =over 4
380  
381 =item * RT: CPAN's request tracker (report bugs here)
382  
383 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Math-Util-CalculatedValue>
384  
385 =item * AnnoCPAN: Annotated CPAN documentation
386  
387 L<http://annocpan.org/dist/Math-Util-CalculatedValue>
388  
389 =item * CPAN Ratings
390  
391 L<http://cpanratings.perl.org/d/Math-Util-CalculatedValue>
392  
393 =item * Search CPAN
394  
395 L<http://search.cpan.org/dist/Math-Util-CalculatedValue/>
396  
397 =back
398  
399  
400 =head1 ACKNOWLEDGEMENTS
401  
402  
403  
404 =cut
405  
406 5 1; # End of Math::Util::CalculatedValue
↑ Summary CalculatedValue/Validatable.pm »