NAME

Food::Ratio - calculate ingredient ratios

SYNOPSIS

use Food::Ratio;
my $fr = Food::Ratio->new;

# add some ingredients of various amounts
$fr->add( 500,  'flour' );
$fr->add( 360,  'water' );    # at 90F to 95F
$fr->add( 11.5, 'salt'  );
$fr->add( 2,    'yeast' );

# make flour the basis for the ratio
$fr->ratio( id => 'flour' );

# emit to a string form
print $fr->string;
# 500   100.00% flour
# 360   72.00%  water
# 11.5  2.30%   salt
# 2     0.40%   yeast
# --
# 873.5 174.7%  *total

# how much of the other are required given 9 grams of yeast?
$fr->weigh( 9, id => 'yeast' );

# emit to a data structure that could be converted to JSON
use Data::Dumper;
print Dumper $fr->details;

The observant may notice that the water and salt are a bit off from the normal bread recipe. This is easier to see in ratio form.

DESCRIPTION

This module calculates ratios of ingredients, with the ability to select what ingredient or group of ingredients the ratio is based on. With a ratio, the masses of the ingredients can then be adjusted with weigh, which can be of any particular ingredient or group of ingredients.

METHODS

add mass name [ groups .. ]

Adds the given amount of the given ingredient. The mass probably should be consistent across the ingredients; grams might be a good choice. The mass could instead be volumes, provided that the units are consistent.

The optional groups indicate what groups the ingredient belongs to, for example

$fr->add( 160, 'cornmeal', 'gflour', 'dry' );
$fr->add( 150, 'flour',    'gflour', 'dry' );
$fr->add( 3.5, 'salt',               'dry' );
...

when there are multiple types of flour, and one wants to base the ratio on the group gflour, not the ingredient flour. The group could be named "flour"; it is named "gflour" here for clarity of documentation.

details

Returns a hash reference of the internal details. Must be called after ratio, and ideally after a few add calls. Data::Dumper will show the form of the resulting structure.

new

Constructor.

ratio param

Calculates the ratios for the ingredients and any ingredient groups. Must be called after add has been used to add things to the object. There are three possible ways to select the key ingredient for the basis of the ratio:

$fr->ratio;                        # $total is used
$fr->ratio( id    => 'flour' );    # use the first 'flour' ingredient
$fr->ratio( group => 'gflour' );   # the group 'gflour' is used

The id argument takes priority over group which in turn takes priority over the ratio being based on the total mass of the ingredients involved. Ingredients may be duplicated in the list; in this case, the first matching ingredient is used, in the order the ingredients were added with the add method.

ratio must be called before using various output methods.

A new ratio can be calculated by calling ratio again; the ratios stick around in the object.

string

Returns a string form of the ingredients and groups. The form depends on whether ratio has been called.

ratio (and probably some add calls) must be called before calling this method.

weigh mass param

Updates the mass for all the ingredients and ingredient groups based on the new mass that optionally may be associated by id or group to a particular ingredient or ingredient group, as also supported by ratio.

A typical use for this would be to adjust a recipe based on the weight of the egg you have, given that some fraction of flour is easier to weigh instead of breaking out the fractional eggs.

$fr->weigh( 53, id => 'egg' );

This updates the mass of all the ingredients, etc, in the object.

ACCESSORS

The data returned by these probably should not be fiddled with. On the other hand, the internal details are unlikely to change. I assume you know your way around Data::Dumper. The details method is probably a better way to get at this data?

amount

After ratio has been called contains the amount of the total or key ingredient or ingredient group, depending on how ratio was called. Is 0 if ratio has not been called.

groups

Hash reference of any optional ingredient groups.

key

After ratio has been called, maybe contains a reference to the key ingredient or ingredient group.

things

Array reference of the ingredients, if any.

total

The total mass of every ingredient added.

BUGS

Bugs are commonly present in flour and other ingredients.

SEE ALSO

Forkish, Ken. Flour water salt yeast: The fundamentals of artisan bread and pizza. Random House Digital, Inc., 2012.

Ruhlman, Michael. Ratio: The simple codes behind the craft of everyday cooking. Simon and Schuster, 2009.

https://github.com/hendricius/the-sourdough-framework

COPYRIGHT AND LICENSE

Copyright 2022 Jeremy Mates

This program is distributed under the (Revised) BSD License: https://opensource.org/licenses/BSD-3-Clause