Skip to content

Commit

Permalink
Issue #112 - restructured add/sub functions in grocery list, moving t…
Browse files Browse the repository at this point in the history
…o ingredient model, avoids checking compatibility for non-united ingredients.
  • Loading branch information
johnhutch committed Jun 14, 2017
1 parent 6aadbb0 commit 9cc3d59
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 22 deletions.
27 changes: 6 additions & 21 deletions app/models/grocery_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,8 @@ def add_ingredient(add_i)

self.ingredients.each do |list_i|
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)
return list_i.save
end
else
if list_i.unitized_amount.compatible?(add_i.unitized_amount)
list_i.amount = Unit.new( list_i.unitized_amount + add_i.unitized_amount ).scalar
return list_i.save
end
if list_i.sum(add_i)
return true
end
end
end
Expand All @@ -29,23 +21,16 @@ def add_ingredient(add_i)

def subtract_ingredient(sub_i)
# search for the ingredient by name, then subtract the recipe's amount from the grocery list
# if amount = 0, remove entirely
# if resultant amount would be <= 0, remove entirely

self.ingredients.each do |list_i|
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
else
return self.ingredients.delete(list_i)
if list_i.sub(sub_i)
return true
end
end
return self.ingredients.delete(list_i)
end
end

private

def sum_i(list_i, add_i)
Unit.new( list_i.unitized_amount + add_i.unitized_amount ).scalar
end
end
43 changes: 42 additions & 1 deletion app/models/ingredient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,51 @@ def matched?(ing)
return self.name.singularize == ing.name.singularize
end

def sum(ing)
# verify that ingredients are compatibly unit'd (or not unit'd)
# could probably be combined with sub(ing) with the math operator sent as a
# param but I don't know how to do that.

if self.unitless? || ing.unitless?
if self.unitless? && ing.unitless?
self.amount = Unit.new( self.unitized_amount + ing.unitized_amount ).scalar
return self.save
end
else
if self.unitized_amount.compatible?(ing.unitized_amount)
self.amount = Unit.new( self.unitized_amount + ing.unitized_amount ).scalar
return self.save
end
end
return false
end

def sub(ing)
# verify that ingredients are compatibly unit'd (or not unit'd) and then subtract
# could probably be combined with sum(ing) with the math operator sent as a param
# but I don't know how to do that.

if self.unitless? || ing.unitless?
if self.unitless? && ing.unitless?
if (self.amount > ing.amount)
self.amount = Unit.new( self.unitized_amount - ing.unitized_amount ).scalar
return self.save
end
end
else
if self.unitized_amount.compatible?(ing.unitized_amount)
if (self.unitized_amount > ing.unitized_amount)
self.amount = Unit.new( self.unitized_amount - ing.unitized_amount ).scalar
return self.save
end
end
end
end

def ingreedy_parse(ing_string)
begin
parsed_ing = Ingreedy.parse(ing_string)
rescue
rescue
# ingreedy chokes if you give it an ingredient string that doesn't contain a
# quantity. so let's handle that so our app doesn't crash
parsed_ing = false
Expand Down

0 comments on commit 9cc3d59

Please sign in to comment.