File Coverage

blib/lib/Convert/CookingTimes.pm
Criterion Covered Total %
statement 51 53 96.2
branch 5 8 62.5
condition 4 6 66.6
subroutine 8 8 100.0
pod 2 2 100.0
total 70 77 90.9


line stmt bran cond sub pod time code
1             package Convert::CookingTimes;
2              
3 1     1   13755 use 5.006;
  1         2  
4 1     1   4 use strict;
  1         1  
  1         25  
5 1     1   3 use warnings FATAL => 'all';
  1         3  
  1         25  
6              
7 1     1   377 use Lingua::Conjunction;
  1         741  
  1         44  
8 1     1   5 use List::Util;
  1         1  
  1         55  
9 1     1   381 use Math::Round;
  1         6444  
  1         414  
10              
11             our $VERSION = '0.02';
12              
13             =head1 NAME
14              
15             Convert::CookingTimes - work out cooking times adjusted for temperature
16              
17              
18             =head1 SYNOPSIS
19              
20             Given a set of item names, temperatures and durations, works out the average
21             temperature and adjusts the times to suit that temperature, then returns a list
22             of suggested timings.
23              
24              
25             my ($temperature, @steps) = @steps = Convert::CookingTimes->adjust_times(
26             { name => 'Chicken breasts', temp => 200, time => 20 },
27             { name => 'Chips', temp => 220, time = 25 },
28             );
29             say "Warm oven up to $temperature degrees first.";
30             for my $step (@steps) {
31             say "Put $step->{name} in the oven, and wait for $step->{time_until_next}";
32             }
33              
34             # You can also feed the result of adjust_times to summarise_instructions to
35             # provide a simple set of instructions, e.g.:
36             say Convert::CookingTimes->summarise_instructions(
37             Convert::CookingTimes->adjust_times(\@items)
38             );
39              
40              
41             =head1 DESCRIPTION
42              
43             Often find yourself cooking a variety of things, the cooking instructions for
44             each requiring a different temperature and time?
45              
46             This module attempts to work out the appropriate oven temperature as an average
47             of all the items, and adjusts their cooking times based on that temperature -
48             so if they're going to be at a higher temperature the time is reduced and vice
49             versa.
50              
51             Results may vary - providing items with a wide variation of temperatures could
52             result in some foods being cooked at sub-optimal temperatures, and obviously you
53             need to sanity-check the results, and be particularly careful to check that meat
54             and poultry has reached a safe internal temperature etc. This is an algorhythm,
55             not a cook!
56              
57              
58             =head1 SUBROUTINES/METHODS
59              
60             =head2 adjust_times
61              
62             Takes a list or arrayref of hashrefs, each of which contains details of an
63             item being cooked, with the keys:
64              
65             =over
66              
67             =item name
68              
69             The name of the item
70              
71             =item temp
72              
73             The temperature the item's cooking instructions call for, in degrees Celcius
74              
75             =item time
76              
77             The cooking time the item's cooking instructions call for, in minutes
78              
79             =back
80              
81             Returns a suggested oven temperature, and an arrayref of cooking times adjusted
82             to suit that temperature.
83              
84             =cut
85              
86             sub adjust_times {
87 3     3 1 1479 my $class = shift;
88 3 50       10 my @items = ref $_[0] eq 'ARRAY' ? @{$_[0]} : @_;
  0         0  
89              
90             # First off: pick our desired temperature, as an average of all the
91             # temperatures, rounded to the nearest 10
92             my $desired_temp = Math::Round::nearest_ceil(10,
93 3         5 List::Util::sum(map { $_->{temp} } @items) / scalar @items
  11         28  
94             );
95              
96             # Now, for each item, work out its adjusted time; group items by
97             # adjusted_time, so if we have multiple items with the same time
98             # requirement, they are merged (as they'll go in together)
99 3         29 my %items_by_time;
100             my @times;
101 3         4 for my $item (@items) {
102             my $adjusted_time = Math::Round::round(
103 11         67 ($item->{temp} * $item->{time}) / $desired_temp
104             );
105 11         45 push @{ $items_by_time{$adjusted_time} }, {
106             name => $item->{name},
107             adjusted_time => Math::Round::round(
108 11         53 ($item->{temp} * $item->{time}) / $desired_temp
109             ),
110             };
111             }
112              
113              
114             # Finally, return the items, sorted by longest cooking duration first,
115             # with the time until the next item should be started included
116 3         16 my @output;
117 3         5 @times = sort { $b->{adjusted_time} <=> $a->{adjusted_time} } @times;
  0         0  
118              
119 3         10 for my $time (reverse sort keys %items_by_time) {
120 9         5 my @items = @{ $items_by_time{$time} };
  9         11  
121             my $condensed_item = {
122 11         22 name => conjunction(map { $_->{name} } @items),
123             adjusted_time => $items[0]->{adjusted_time},
124 9         7 };
125            
126             # Add time_until_next, if there are other items to come - find the next
127             # item(s) by looking for the first time that's shorter than this one:
128 9         44 my ($next_time) = grep { $_ < $time } reverse sort keys %items_by_time;
  27         47  
129 9 100       17 if ($next_time) {
130             $condensed_item->{time_until_next}
131             = $condensed_item->{adjusted_time}
132 6         10 - $items_by_time{$next_time}[0]{adjusted_time};
133             }
134 9         14 push @output, $condensed_item;
135             }
136            
137 3         15 return $desired_temp, \@output;
138             }
139              
140              
141             =item summarise_instructions
142              
143             Given the results of adjust_times, produce a list of instructions.
144              
145             For instance:
146              
147             - Warm oven up to 200 degrees
148             - Add Chicken Breasts, cook for 5 minutes
149             - Add Oven Chips, cook for 20 minutes
150              
151             Returns a list of instruction steps if called in list context, or that list
152             joined with newlines if called in scalar context.
153              
154             =cut
155              
156             sub summarise_instructions {
157 1     1 1 1 my $self = shift;
158 1         2 my $temp = shift;
159 1 50       3 my @items = ( ref $_[0] eq 'ARRAY' ? @{$_[0]} : @_ );
  1         2  
160            
161             my $total_mins = List::Util::sum(
162 1   66     2 map { $_->{time_until_next} // $_->{adjusted_time} } @items
  3         9  
163             );
164 1         1 my @instructions;
165 1         4 push @instructions, "Warm oven up to $temp degrees.";
166 1         4 push @instructions, "Cooking the whole meal will take $total_mins minutes.";
167              
168 1         2 for my $item (@items) {
169             push @instructions, sprintf "Add %s and cook for %d minutes",
170 3   66     12 $item->{name}, $item->{time_until_next} || $item->{adjusted_time};
171             }
172            
173 1 50       13 return wantarray ? @instructions : join "\n", @instructions;
174             }
175              
176              
177              
178             =head1 AUTHOR
179              
180             David Precious, C<< >>
181              
182             =head1 BUGS / CONTRIBUTING
183              
184             This module is developed on GitHub - bug reports, suggestions, and pull requests
185             welcomed:
186              
187             L
188              
189              
190              
191             =head1 SUPPORT
192              
193             You can find documentation for this module with the perldoc command.
194              
195             perldoc Convert::CookingTimes
196              
197              
198              
199             =head1 LICENSE AND COPYRIGHT
200              
201             Copyright 2015 David Precious.
202              
203             This program is free software; you can redistribute it and/or modify it
204             under the terms of the the Artistic License (2.0). You may obtain a
205             copy of the full license at:
206              
207             L
208              
209             Any use, modification, and distribution of the Standard or Modified
210             Versions is governed by this Artistic License. By using, modifying or
211             distributing the Package, you accept this license. Do not use, modify,
212             or distribute the Package, if you do not accept this license.
213              
214             If your Modified Version has been derived from a Modified Version made
215             by someone other than you, you are nevertheless required to ensure that
216             your Modified Version complies with the requirements of this license.
217              
218             This license does not grant you the right to use any trademark, service
219             mark, tradename, or logo of the Copyright Holder.
220              
221             This license includes the non-exclusive, worldwide, free-of-charge
222             patent license to make, have made, use, offer to sell, sell, import and
223             otherwise transfer the Package with respect to any patent claims
224             licensable by the Copyright Holder that are necessarily infringed by the
225             Package. If you institute patent litigation (including a cross-claim or
226             counterclaim) against any party alleging that the Package constitutes
227             direct or contributory patent infringement, then this Artistic License
228             to you shall terminate on the date that such litigation is filed.
229              
230             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
231             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
232             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
233             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
234             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
235             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
236             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
237             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
238              
239              
240             =cut
241              
242             1; # End of Convert::CookingTimes