Chef/Container.pm

filter: Statementstmt 66.9% 77 / 115 Branchbran 40.0% 8 / 20 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 71.4% 15 / 21 Podpod 100% 15 / 15 totaltotal 66.4% 117 / 176 CC 16 SCAR 12.5
CC
16
Cov
63%
stir_time
12.0
divide
6.0
put_sum
6.0
CRAP 3.5
« Chef/Ingredient.pm ↑ Summary
1  
2 package Acme::Chef::Container;
3  
4 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 10 ▶use Acme::Chef::Ingredient;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 10 ▶use vars qw/$VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 $VERSION = '1.00';
13  
14 =head1 NAME
15  
16 Acme::Chef::Container - Internal module used by Acme::Chef
17  
18 =head1 SYNOPSIS
19  
20 use Acme::Chef;
21  
22 =head1 DESCRIPTION
23  
24 Please see L<Acme::Chef>;
25  
26 =head2 METHODS
27  
28 This is a list of methods in this package.
29  
30 =over 2
31  
32 =cut
33  
34 =item new
35  
36 This is the Acme::Chef::Container constructor. Creates a new
37 Acme::Chef::Container object. All arguments are treated as key/value pairs for
38 object attributes.
39  
40 =cut
41  
42 279 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
43 279 my $proto = shift;
44 279 ▶ my $class = ref $proto || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
244350
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
45  
46 279 my $self = {};
47  
48 279 ▶ if (ref $proto) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proto) 244 35 279
49 244 %$self = %$proto;
50 244 $self->{contents} = [ map { $_->new() } @{$self -> {contents}} ];
51 }
52  
53 279 %$self = (
54 contents => [],
55 %$self,
56 @_,
57 );
58  
59 279 return bless $self => $class;
60 }
61  
62  
63 =item put
64  
65 This method implements the 'put' command. Please refer to L<Acme::Chef> for
66 details.
67  
68 =cut
69  
70 943 ▶sub put {
Subroutine put: called CC=2 SCAR=6.9
Pod documented
71 943 my $self = shift;
72  
73 943 my @ingredients = @_;
74  
75 943 push @{$self->{contents}}, $_->new() for @ingredients;
76  
77 943 return $self;
78 }
79  
80 =item fold
81  
82 This method implements the 'fold' command. Please refer to L<Acme::Chef> for
83 details.
84  
85 =cut
86  
87 111 ▶sub fold {
Subroutine fold: called CC=2 SCAR=7.0
Pod documented
88 111 my $self = shift;
89  
90 111 my $ingredient = shift;
91  
92 croak "Invalid operation on empty container: fold."
93 111 ▶ unless @{$self->{contents}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"contents"};} 0 111 111
94  
95 111 my $new_val = pop @{ $self->{contents} };
96  
97 111 $ingredient->value( $new_val->value() );
98  
99 111 return $ingredient;
100 }
101  
102 =item add
103  
104 This method implements the 'add' command. Please refer to L<Acme::Chef> for
105 details.
106  
107 =cut
108  
109 3 ▶sub add {
Subroutine add: called CC=2 SCAR=7.0
Pod documented
110 3 my $self = shift;
111  
112 3 my $ingredient = shift;
113  
114 croak "Invalid operation on empty container: add."
115 3 ▶ unless @{$self->{contents}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"contents"};} 0 3 3
116  
117 $self->{contents}->[-1]->value(
118 3 $self->{contents}->[-1]->value() +
119 $ingredient->value()
120 );
121  
122 3 return $ingredient;
123 }
124  
125 =item remove
126  
127 This method implements the 'remove' command. Please refer to L<Acme::Chef> for
128 details.
129  
130 =cut
131  
132  
133 168 ▶sub remove {
Subroutine remove: called CC=2 SCAR=7.0
Pod documented
134 168 my $self = shift;
135  
136 168 my $ingredient = shift;
137  
138 croak "Invalid operation on empty container: remove."
139 168 ▶ unless @{$self->{contents}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"contents"};} 0 168 168
140  
141 $self->{contents}->[-1]->value(
142 168 $self->{contents}->[-1]->value() -
143 $ingredient->value()
144 );
145  
146 168 return $ingredient;
147 }
148  
149  
150 =item combine
151  
152 This method implements the 'combine' command. Please refer to L<Acme::Chef> for
153 details.
154  
155 =cut
156  
157 240 ▶sub combine {
Subroutine combine: called CC=2 SCAR=7.0
Pod documented
158 240 my $self = shift;
159  
160 240 my $ingredient = shift;
161  
162 croak "Invalid operation on empty container: combine."
163 240 ▶ unless @{$self->{contents}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"contents"};} 0 240 240
164  
165 $self->{contents}->[-1]->value(
166 240 $self->{contents}->[-1]->value() *
167 $ingredient->value()
168 );
169  
170 240 return $ingredient;
171 }
172  
173  
174 =item divide
175  
176 This method implements the 'divide' command. Please refer to L<Acme::Chef> for
177 details.
178  
179 =cut
180  
181 0 ▶sub divide {
Subroutine divide: not called CC=2 SCAR=17.9
Pod documented
182 0 my $self = shift;
183  
184 0 my $ingredient = shift;
185  
186 croak "Invalid operation on empty container: divide."
187 0 ▶ unless @{$self->{contents}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"contents"};} 0 0 0
188  
189 $self->{contents}->[-1]->value(
190 0 $self->{contents}->[-1]->value() /
191 $ingredient->value()
192 );
193  
194 0 return $ingredient;
195 }
196  
197 =item put_sum
198  
199 This method takes a number of Acme::Chef::Ingredient objects as arguments and
200 creates and 'puts' the sum of the ingredients.
201  
202 Please refer to L<Acme::Chef> for details.
203  
204 =cut
205  
206 0 ▶sub put_sum {
Subroutine put_sum: not called CC=2 SCAR=17.9
Pod documented
207 0 my $self = shift;
208  
209 0 my @ingredients = @_;
210  
211 0 my $sum = 0;
212 0 $sum += $_->value() for @ingredients;
213  
214 0 my $ingredient = Acme::Chef::Ingredient->new(
215 name => '',
216 value => $sum,
217 measure => '',
218 type => 'dry',
219 );
220  
221 0 $self->put($ingredient);
222  
223 0 return $ingredient;
224 }
225  
226 =item liquify_contents
227  
228 This method implements the 'liquify' command for all ingredients.
229 Please refer to L<Acme::Chef> for details.
230  
231 =cut
232  
233 21 ▶sub liquify_contents {
Subroutine liquify_contents: called CC=2 SCAR=6.9
Pod documented
234 21 my $self = shift;
235  
236 21 foreach my $ingredient (@{$self->{contents}}) {
237 174 $ingredient->liquify();
238 }
239  
240 21 return $self;
241 }
242  
243 =item stir_time
244  
245 This method implements the 'stir' command.
246 First argument should be the depth ("time") to stir.
247 Please refer to L<Acme::Chef> for details.
248  
249 =cut
250  
251 0 ▶sub stir_time {
Subroutine stir_time: not called CC=3 SCAR=24.8
Pod documented
252 0 my $self = shift;
253  
254 0 my $depth = shift;
255  
256 0 ▶ return $self unless scalar @{$self->{contents}};
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$self->{"contents"};} 0 0 0
257  
258 0 ▶ $depth = $#{$self->{contents}} if $depth > $#{$self->{contents}};
Branch: 1 branch
BranchTrueFalseTotal
if $depth > $#{$$self{"contents"};} 0 0 0
259  
260 0 my $top = pop @{ $self->{contents} };
261 0 splice @{$self->{contents}}, (@{$self->{contents}}-$depth), 0, $top;
262  
263 0 return $self;
264 }
265  
266  
267 =item stir_ingredient
268  
269 This method implements the 'stir_ingredient' command. Please refer to
270 L<Acme::Chef> for details.
271  
272 =cut
273  
274  
275 0 ▶sub stir_ingredient {
Subroutine stir_ingredient: not called CC=1 SCAR=6.9
Pod documented
276 0 my $self = shift;
277  
278 0 my $ingredient = shift;
279  
280 0 $self->stir_time($ingredient->value());
281  
282 0 return $self;
283 }
284  
285 =item mix
286  
287 This method implements the 'mix' command. Please refer to L<Acme::Chef> for
288 details.
289  
290 Shuffles the container's contents.
291  
292 =cut
293  
294 0 ▶sub mix {
Subroutine mix: not called CC=1 SCAR=6.9
Pod documented
295 0 my $self = shift;
296  
297 0 _fisher_yates_shuffle( $self->{contents} );
298  
299 0 return $self;
300 }
301  
302 =item clean
303  
304 This method implements the 'clean' command. Please refer to L<Acme::Chef> for
305 details.
306  
307 Empties the container.
308  
309 =cut
310  
311 72 ▶sub clean {
Subroutine clean: called CC=1 SCAR=0.0
Pod documented
312 72 my $self = shift;
313  
314 72 @{$self->{contents}} = ();
315  
316 72 return $self;
317 }
318  
319  
320 =item pour
321  
322 This method implements the 'pour' command. Please refer to L<Acme::Chef> for
323 details.
324  
325 Returns the contained ingredients.
326  
327 =cut
328  
329 131 ▶sub pour {
Subroutine pour: called CC=1 SCAR=0.0
Pod documented
330 131 my $self = shift;
331  
332 131 return @{ $self->{contents} };
333 }
334  
335  
336 =item print
337  
338 Returns stringification of the object.
339  
340 =cut
341  
342 35 ▶sub print {
Subroutine print: called CC=3 SCAR=11.0
Pod documented
343 35 my $self = shift;
344  
345 35 my $string = '';
346  
347 35 foreach my $ingr ( reverse @{$self->{contents}} ) {
348 248 ▶ if ($ingr->type() eq 'liquid') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ingr->type eq 'liquid') { } 174 74 248
349 174 $string .= chr( $ingr->value() );
350 } else {
351 74 $string .= ' '.$ingr->value();
352 }
353 }
354  
355 35 return $string;
356 }
357  
358  
359 # From the Perl FAQ: (NOT a method)
360 # fisher_yates_shuffle( \@array ) :
361 # generate a random permutation of @array in place
362 0 ▶sub _fisher_yates_shuffle {
Subroutine _fisher_yates_shuffle: not called CC=2 SCAR=17.9
363 0 my $array = shift;
364 0 my $i;
365 0 ▶ for ($i = @$array; --$i; ) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$i) 0 0 0
366 0 my $j = int rand ($i+1);
367 0 @$array[$i,$j] = @$array[$j,$i];
368 }
369 }
370  
371 __END__
« Chef/Ingredient.pm ↑ Summary