Ratio.pm

filter: Statementstmt 100% 136 / 136 Branchbran 100% 48 / 48 Conditioncond 100% 32 / 32 MC/DCmcdc 100% 19 / 19 Subroutinesub 100% 18 / 18 Podpod 100% 9 / 9 totaltotal 100% 262 / 262 CC 46 SCAR 22.9
CC
46
Cov
100%
weigh
16.0
add
13.0
ratio
12.0
CRAP 9.9
« Ratio/ConfigData.pm ↑ Summary
1 # -*- Perl -*-
2 #
3 # Ultima Ratio Coquorum
4  
5 package Food::Ratio 0.04;
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Object::Pad 0.802;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 class Food::Ratio :strict(params);
11 2 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use List::UtilsBy 'nsort_by';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Scalar::Util 'looks_like_number';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 MASS => 0, # array index for $things, $groups, $total
17 NAME => 1,
18 GROUPS => 2,
19 ORDER => 3,
20 RATIO => 4,
21 2 };
22 1 ▶field $things :reader; # individual ingredients (aref of aref)
Subroutine things: called CC=1 SCAR=0.0
Pod documented
23 1 ▶field $groups :reader; # groups of ingredients (href of aref)
Subroutine groups: called CC=1 SCAR=0.0
Pod documented
24 1 ▶field $total :reader; # (aref)
Subroutine total: called CC=1 SCAR=0.0
Pod documented
25  
26 1 ▶field $key :reader; # ratio key ingredient, group, or total
Subroutine key: called CC=1 SCAR=0.0
Pod documented
27  
28 field $index_group = 0; # to keep the output in input addition order
29  
30 2 ADJUST {
31 $groups = {};
32 $things = [];
33 $total = [];
34 }
35  
36 21 ▶method add ( $mass, $name, @rest ) {
Subroutine add: called CC=13 SCAR=25.6
Pod documented
37 21 ▶ croak "mass must be positive"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mass and &looks_like_number($mass) and $mass > 0 3 18 21
Condition100%6/6
defined $mass and &looks_like_number($mass) and $mass > 0
!ll&&!rl&&r
2118
defined $mass and &looks_like_number($mass)
!ll&&!rl&&r
1119
MC/DC100%3/3
defined $mass and &looks_like_number($mass) and $mass > 0
defined $mass &looks_like_number($mass) $mass > 0
Truth table100%4/4
A and B and C
A= defined $massB= &looks_like_number($mass)C= $mass > 0
ABCresult
0XX0
10X0
1100
1111
38 unless defined $mass
39 and looks_like_number($mass)
40 and $mass > 0;
41 18 ▶ croak "things must be something"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 2 16 18
Condition100%3/3
defined $name and length $name
!ll&&!rl&&r
1116
MC/DC100%2/2
defined $name and length $name
defined $name length $name
Truth table100%3/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
42 unless defined $name and length $name;
43 16 for my $grname (@rest) {
44 16 ▶ croak "groups must be something"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $grname and length $grname 2 14 16
Condition100%3/3
defined $grname and length $grname
!ll&&!rl&&r
1114
MC/DC100%2/2
defined $grname and length $grname
defined $grname length $grname
Truth table100%3/3
A and B
A= defined $grnameB= length $grname
ABresult
0X0
100
111
45 unless defined $grname and length $grname;
46 }
47 # hopefully after here nothing blows up that might leave the object
48 # in an inconsistent state
49 14 my @meta;
50 14 ▶ @meta[ MASS, NAME, GROUPS, RATIO ] =
Branch: 1 branch
BranchTrueFalseTotal
@rest ? : 10 4 14
51 ( $mass, $name, @rest ? \@rest : [], 0 );
52 14 push @$things, \@meta;
53 14 for my $grname (@rest) {
54 14 ▶ my $gmeta = $groups->{$grname} //= [];
Condition100%2/2
$groups->{$grname} //= []
l!l
86
MC/DC100%1/1
$groups->{$grname} //= []
$groups->{$grname}
Truth table100%2/2
A
A= $groups->{$grname}
Aresult
11
00
55 14 $gmeta->@[ NAME, RATIO ] = ( $grname, 0 );
56 14 $gmeta->[MASS] += $mass;
57 14 ▶ $gmeta->[ORDER] = $index_group++ unless defined $gmeta->[ORDER];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $gmeta->[3] 6 8 14
58 }
59 14 $total->[MASS] += $mass;
60 14 return $self;
61 2 }
62  
63 2 ▶method details() {
Subroutine details: called CC=4 SCAR=13.9
Pod documented
64 2 ▶ croak "ratio has not been called" unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 1 1 2
65 1 my %details;
66 1 for my $ref (@$things) {
67 push $details{ingredients}->@*,
68 2 { groups => [ $ref->[GROUPS]->@* ],
69 mass => $ref->[MASS],
70 name => $ref->[NAME],
71 ratio => $ref->[RATIO],
72 };
73 }
74 1 ▶ for my $ref ( nsort_by { $_->[ORDER] } values %$groups ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
75 push $details{groups}->@*,
76 2 { mass => $ref->[MASS],
77 name => $ref->[NAME],
78 order => $ref->[ORDER],
79 ratio => $ref->[RATIO],
80 };
81 }
82 $details{total} = {
83 1 mass => $total->[MASS],
84 ratio => $total->[RATIO],
85 };
86 1 return \%details;
87 2 }
88  
89 # the ratio could be based on the total amount, or for cooking there is
90 # more likely some key ingredient--flour--or a group of ingredients,
91 # such as a variety of flours that together form the total for the ratio
92 10 ▶method ratio (%param) {
Subroutine ratio: called CC=12 SCAR=24.8
Pod documented
93 10 my $amount;
94 10 ▶ if ( exists $param{id} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $param{'id'}) { } 4 6 10
elsif (exists $param{'group'}) { } 4 2 6
95 croak "id must be something"
96 4 ▶ unless defined $param{id} and length $param{id};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param{"id"} and length $param{"id"} 2 2 4
Condition100%3/3
defined $param{"id"} and length $param{"id"}
!ll&&!rl&&r
112
MC/DC100%2/2
defined $param{"id"} and length $param{"id"}
defined $param{"id"} length $param{"id"}
Truth table100%3/3
A and B
A= defined $param{"id"}B= length $param{"id"}
ABresult
0X0
100
111
97 # NOTE only the first match is used if there are duplicates in
98 # the ingredients list
99 2 my $okay = 0;
100 2 for my $ref (@$things) {
101 9 ▶ if ( $ref->[NAME] eq $param{id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->[1] eq $param{"id"}) 1 8 9
102 1 ( $key, $amount ) = ( $ref, $ref->[MASS] );
103 1 $okay = 1;
104 1 last;
105 }
106 }
107 2 ▶ croak "no such id '$param{id}'" unless $okay;
Branch: 1 branch
BranchTrueFalseTotal
unless $okay 1 1 2
108 } elsif ( exists $param{group} ) {
109 croak "group must be something"
110 4 ▶ unless defined $param{group} and length $param{group};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param{"group"} and length $param{"group"} 2 2 4
Condition100%3/3
defined $param{"group"} and length $param{"group"}
!ll&&!rl&&r
112
MC/DC100%2/2
defined $param{"group"} and length $param{"group"}
defined $param{"group"} length $param{"group"}
Truth table100%3/3
A and B
A= defined $param{"group"}B= length $param{"group"}
ABresult
0X0
100
111
111 croak "no such group '$param{group}'"
112 2 ▶ unless exists $groups->{ $param{group} };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$groups{$param{"group"}} 1 1 2
113 1 $key = $groups->{ $param{group} };
114 1 $amount = $key->[MASS];
115 } else {
116 2 $key = $total;
117 2 $amount = $total->[MASS];
118 }
119 4 for my $ref ( @$things, values %$groups, $total ) {
120 28 $ref->[RATIO] = $ref->[MASS] * 100 / $amount;
121 }
122 4 return $self;
123 2 }
124  
125 5 ▶method string() {
Subroutine string: called CC=5 SCAR=16.1
Pod documented
126 5 ▶ croak "ratio has not been called" unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 1 4 5
127 4 my $s = '';
128 4 for my $ref (@$things) {
129 20 $s .= join( "\t",
130 sprintf( "%.4g\t%.4g%%", $ref->@[ MASS, RATIO ] ),
131 $ref->[NAME], $ref->[GROUPS]->@* )
132 . "\n";
133 }
134 4 ▶ if ( keys %$groups ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$groups) 1 3 4
135 1 $s .= "--\n";
136 1 ▶ for my $ref ( nsort_by { $_->[ORDER] } values %$groups ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
137 4 $s .= join( "\t",
138 sprintf( "%.4g\t%.4g%%", $ref->@[ MASS, RATIO ] ),
139 $ref->[NAME] )
140 . "\n";
141 }
142 }
143 4 $s .= "--\n";
144 4 $s .= join "\t", $total->[MASS], sprintf( '%.4g%%', $total->[RATIO] ),
145 "*total\n";
146 4 return $s;
147 2 }
148  
149 13 ▶method weigh ( $mass, %param ) {
Subroutine weigh: called CC=16 SCAR=27.7
Pod documented
150 13 ▶ croak "ratio has not been called" unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 1 12 13
151 12 ▶ croak "mass must be positive"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mass and &looks_like_number($mass) and $mass > 0 3 9 12
Condition100%6/6
defined $mass and &looks_like_number($mass) and $mass > 0
!ll&&!rl&&r
219
defined $mass and &looks_like_number($mass)
!ll&&!rl&&r
1110
MC/DC100%3/3
defined $mass and &looks_like_number($mass) and $mass > 0
defined $mass &looks_like_number($mass) $mass > 0
Truth table100%4/4
A and B and C
A= defined $massB= &looks_like_number($mass)C= $mass > 0
ABCresult
0XX0
10X0
1100
1111
152 unless defined $mass
153 and looks_like_number($mass)
154 and $mass > 0;
155 9 my $ratio;
156 9 ▶ if ( exists $param{id} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $param{'id'}) { } 4 5 9
elsif (exists $param{'group'}) { } 4 1 5
157 croak "id must be something"
158 4 ▶ unless defined $param{id} and length $param{id};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param{"id"} and length $param{"id"} 2 2 4
Condition100%3/3
defined $param{"id"} and length $param{"id"}
!ll&&!rl&&r
112
MC/DC100%2/2
defined $param{"id"} and length $param{"id"}
defined $param{"id"} length $param{"id"}
Truth table100%3/3
A and B
A= defined $param{"id"}B= length $param{"id"}
ABresult
0X0
100
111
159 # NOTE only the first match is used if there are duplicates in
160 # the ingredients list
161 2 my $okay = 0;
162 2 for my $ref (@$things) {
163 8 ▶ if ( $ref->[NAME] eq $param{id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->[1] eq $param{"id"}) 1 7 8
164 1 $ratio = $mass / $ref->[MASS];
165 1 $okay = 1;
166 1 last;
167 }
168 }
169 2 ▶ croak "no such id '$param{id}'" unless $okay;
Branch: 1 branch
BranchTrueFalseTotal
unless $okay 1 1 2
170 } elsif ( exists $param{group} ) {
171 croak "group must be something"
172 4 ▶ unless defined $param{group} and length $param{group};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param{"group"} and length $param{"group"} 2 2 4
Condition100%3/3
defined $param{"group"} and length $param{"group"}
!ll&&!rl&&r
112
MC/DC100%2/2
defined $param{"group"} and length $param{"group"}
defined $param{"group"} length $param{"group"}
Truth table100%3/3
A and B
A= defined $param{"group"}B= length $param{"group"}
ABresult
0X0
100
111
173 croak "no such group '$param{group}'"
174 2 ▶ unless exists $groups->{ $param{group} };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$groups{$param{"group"}} 1 1 2
175 1 $ratio = $mass / $groups->{ $param{group} }->[MASS];
176 } else {
177 1 $ratio = $mass / $total->[MASS];
178 }
179 3 for my $ref ( @$things, values %$groups, $total ) {
180 15 $ref->[MASS] *= $ratio;
181 }
182 3 return $self;
183 2 }
184  
185 2 1;
186 __END__
« Ratio/ConfigData.pm ↑ Summary