File Coverage

blib/lib/Convert/CookingTimes.pm
Criterion Covered Total %
statement 53 55 96.3
branch 5 8 62.5
condition 4 6 66.6
subroutine 8 8 100.0
pod 2 2 100.0
total 72 79 91.1


line stmt bran cond sub pod time code
1             package Convert::CookingTimes;
2              
3 1     1   63776 use 5.006;
  1         4  
4 1     1   4 use strict;
  1         3  
  1         22  
5 1     1   5 use warnings FATAL => 'all';
  1         6  
  1         47  
6              
7 1     1   355 use Lingua::Conjunction;
  1         946  
  1         56  
8 1     1   6 use List::Util;
  1         2  
  1         50  
9 1     1   274 use Math::Round;
  1         7165  
  1         613  
10              
11             our $VERSION = '0.03';
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 4     4 1 4926 my $class = shift;
88 4 50       24 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 4         12 List::Util::sum(map { $_->{temp} } @items) / scalar @items
  14         61  
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 4         83 my %items_by_time;
100             my @times;
101 4         13 for my $item (@items) {
102             my $adjusted_time = Math::Round::round(
103 14         155 ($item->{temp} * $item->{time}) / $desired_temp
104             );
105 14         92 push @{ $items_by_time{$adjusted_time} }, {
106             name => $item->{name},
107             adjusted_time => Math::Round::round(
108 14         155 ($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 4         40 my @output;
117 4         10 @times = sort { $b->{adjusted_time} <=> $a->{adjusted_time} } @times;
  0         0  
118              
119 4         18 for my $time (sort { $b <=> $a } keys %items_by_time) {
  10         28  
120 12         15 my @items = @{ $items_by_time{$time} };
  12         27  
121             my $condensed_item = {
122 14         38 name => conjunction(map { $_->{name} } @items),
123             adjusted_time => $items[0]->{adjusted_time},
124 12         19 };
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             my ($next_time) = grep {
129 36         102 $_ < $time
130 12         79 } sort { $b <=> $a} keys %items_by_time;
  30         49  
131 12 100       29 if ($next_time) {
132             $condensed_item->{time_until_next}
133             = $condensed_item->{adjusted_time}
134 8         17 - $items_by_time{$next_time}[0]{adjusted_time};
135             }
136 12         20 push @output, $condensed_item;
137             }
138            
139 4         29 return $desired_temp, \@output;
140             }
141              
142              
143             =item summarise_instructions
144              
145             Given the results of adjust_times, produce a list of instructions.
146              
147             For instance:
148              
149             - Warm oven up to 200 degrees
150             - Add Chicken Breasts, cook for 5 minutes
151             - Add Oven Chips, cook for 20 minutes
152              
153             Returns a list of instruction steps if called in list context, or that list
154             joined with newlines if called in scalar context.
155              
156             =cut
157              
158             sub summarise_instructions {
159 1     1 1 3 my $self = shift;
160 1         2 my $temp = shift;
161 1 50       4 my @items = ( ref $_[0] eq 'ARRAY' ? @{$_[0]} : @_ );
  1         3  
162            
163             my $total_mins = List::Util::sum(
164 1   66     3 map { $_->{time_until_next} // $_->{adjusted_time} } @items
  3         15  
165             );
166 1         2 my @instructions;
167 1         4 push @instructions, "Warm oven up to $temp degrees.";
168 1         6 push @instructions, "Cooking the whole meal will take $total_mins minutes.";
169              
170 1         3 for my $item (@items) {
171             push @instructions, sprintf "Add %s and cook for %d minutes",
172 3   66     16 $item->{name}, $item->{time_until_next} || $item->{adjusted_time};
173             }
174            
175 1 50       13 return wantarray ? @instructions : join "\n", @instructions;
176             }
177              
178              
179              
180             =head1 AUTHOR
181              
182             David Precious, C<< >>
183              
184             =head1 BUGS / CONTRIBUTING
185              
186             This module is developed on GitHub - bug reports, suggestions, and pull requests
187             welcomed:
188              
189             L
190              
191              
192              
193             =head1 SUPPORT
194              
195             You can find documentation for this module with the perldoc command.
196              
197             perldoc Convert::CookingTimes
198              
199              
200              
201             =head1 LICENSE AND COPYRIGHT
202              
203             Copyright 2015 David Precious.
204              
205             This program is free software; you can redistribute it and/or modify it
206             under the terms of the the Artistic License (2.0). You may obtain a
207             copy of the full license at:
208              
209             L
210              
211             Any use, modification, and distribution of the Standard or Modified
212             Versions is governed by this Artistic License. By using, modifying or
213             distributing the Package, you accept this license. Do not use, modify,
214             or distribute the Package, if you do not accept this license.
215              
216             If your Modified Version has been derived from a Modified Version made
217             by someone other than you, you are nevertheless required to ensure that
218             your Modified Version complies with the requirements of this license.
219              
220             This license does not grant you the right to use any trademark, service
221             mark, tradename, or logo of the Copyright Holder.
222              
223             This license includes the non-exclusive, worldwide, free-of-charge
224             patent license to make, have made, use, offer to sell, sell, import and
225             otherwise transfer the Package with respect to any patent claims
226             licensable by the Copyright Holder that are necessarily infringed by the
227             Package. If you institute patent litigation (including a cross-claim or
228             counterclaim) against any party alleging that the Package constitutes
229             direct or contributory patent infringement, then this Artistic License
230             to you shall terminate on the date that such litigation is filed.
231              
232             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
233             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
234             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
235             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
236             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
237             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
238             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
239             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
240              
241              
242             =cut
243              
244             1; # End of Convert::CookingTimes