Skip to content

Commit

Permalink
issue #112 - moved ingredient matching logic into ingredient model.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnhutch committed Jun 9, 2017
1 parent 9661287 commit a2994da
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/models/grocery_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def add_ingredient(add_i)
# if not found, add a new ingredient to the grocery list

self.ingredients.each do |list_i|
if list_i.name.singularize == add_i.name.singularize
if list_i.matched?(add_i)
if list_i.unitless? || add_i.unitless?
if list_i.unitless? && add_i.unitless?
list_i.amount = sum_i(list_i, add_i)
Expand All @@ -32,7 +32,7 @@ def subtract_ingredient(sub_i)
# if amount = 0, remove entirely

self.ingredients.each do |list_i|
if list_i.name.singularize == sub_i.name.singularize
if list_i.matched?(sub_i)
if (list_i.unitized_amount > sub_i.unitized_amount) && list_i.unitized_amount.compatible?(sub_i.unitized_amount)
list_i.amount = Unit.new( list_i.unitized_amount - sub_i.unitized_amount ).scalar
return list_i.save
Expand Down
4 changes: 4 additions & 0 deletions app/models/ingredient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def unitized_amount
end
end

def matched?(ing)
return self.name.singularize == ing.name.singularize
end

def ingreedy_parse(ing_string)
begin
parsed_ing = Ingreedy.parse(ing_string)
Expand Down

0 comments on commit a2994da

Please sign in to comment.