~rgrjr/meals

89c4456b60c0d86087b7086c3fcc965ea04a1515 — Bob Rogers 1 year, 3 months ago d5b1929 master
Add inclusion context to unit conversion warnings

   Work done on 14-May-22.
* Food/HasIngredients.pm:
   + (add_ingredient):  Add a $warning param and pass when instantiating
     the ingredient.
* Food/Ingredient.pm:
   + (new):  If we have a conversion problem and the "warn" option is
     specified, call that to emit the warning message.
* Food/Item.pm:
   + (parse_recipes $parse_file):  Pass our $warning sub when making
     items and recipes and when calling add_ingredient so that
     conversion warnings come with inclusion context.
3 files changed, 22 insertions(+), 9 deletions(-)

M Food/HasIngredients.pm
M Food/Ingredient.pm
M Food/Item.pm
M Food/HasIngredients.pm => Food/HasIngredients.pm +3 -2
@@ 35,11 35,12 @@ sub n_ingredients {
}

sub add_ingredient {
    my ($self, $item, $amount, $units) = @_;
    my ($self, $item, $amount, $units, $warning) = @_;

    my $ingredient = Food::Ingredient->new(item => $item,
					   amount => $amount,
					   units => $units);
					   units => $units,
					   warn => $warning);
    push(@{$self->{_ingredients}}, $ingredient);
    return $ingredient;
}

M Food/Ingredient.pm => Food/Ingredient.pm +12 -3
@@ 17,6 17,8 @@ BEGIN {

sub new {
    my ($class, @args) = @_;
    my %args = @args;
    my $warn = $args{warn};

    my $self = $class->SUPER::new(@args);



@@ 47,9 49,16 @@ sub new {
	    $n_servings = $amount * $from_factor / $item->serving_size_g;
	}
	else {
	    warn("$0:  No conversion from $amount$units to ",
		 "item units for ", $item->name, ".\n")
		if $item->serving_size_g || $item->serving_size_ml;
	    if ($item->serving_size_g || $item->serving_size_ml) {
		my @message = ("No conversion from $amount$units to ",
			       "item units for ", $item->name, ".\n");
		if ($warn) {
		    $warn->(@message);
		}
		else {
		    warn("$0:  ", @message);
		}
	    }
	    # punt.
	    $n_servings = $amount;
	}

M Food/Item.pm => Food/Item.pm +7 -4
@@ 230,12 230,14 @@ sub parse_recipes {
		$current_item->finalize()
		    if $current_item && $current_item->can('finalize');
		if ($type eq 'item') {
		    $current_item = Food::Item->new(name => $name);
		    $current_item = Food::Item->new
			(name => $name, warn => $warning);
		    $store_item->($name, $current_item, $file_name,
				  $stream->input_line_number);
		}
		elsif ($type eq 'recipe') {
		    $current_item = Food::Recipe->new(name => $name);
		    $current_item = Food::Recipe->new
			(name => $name, warn => $warning);
		    $store_item->($name, $current_item, $file_name,
				  $stream->input_line_number);
		}


@@ 288,7 290,8 @@ sub parse_recipes {
		    }
		}
		elsif (my $item = $class->fetch_item($name)) {
		    $current_item->add_ingredient($item, $amount, $units);
		    $current_item->add_ingredient($item, $amount, $units,
						  $warning);
		}
		else {
		    $warning->("Don't know what to do with '$name'.\n");


@@ 298,7 301,7 @@ sub parse_recipes {
		# Ignore these for now.
	    }
	    elsif (my $item = $class->fetch_item($_)) {
		$current_item->add_ingredient($item, 1, 'serving');
		$current_item->add_ingredient($item, 1, 'serving', $warning);
	    }
	    else {
		$warning->("Don't know what to do with '$_'.\n");