From 9c3d31c1313812e60d01669470fd69748f49918f Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Tue, 5 May 2020 22:02:48 +0200 Subject: [PATCH] Fixes for formula computation Added default quantity --- app/models/formula.rb | 6 +++--- db/migrate/002_load_defaults.rb | 4 ++++ lib/body_tracking/items_with_quantities.rb | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/models/formula.rb b/app/models/formula.rb index 3f4c139..341a8be 100644 --- a/app/models/formula.rb +++ b/app/models/formula.rb @@ -30,7 +30,7 @@ class Formula < ActiveRecord::Base args = [] @parts.each do |p| code = p[:type] == :indexed ? - "length.times.map { |index| #{p[:content]} }" : p[:content] + "length.times.map { |_index| #{p[:content]} }" : p[:content] args << get_binding(quantities, args, length).eval(code) end args.last.map { |v| [v, self.unit] } @@ -47,8 +47,8 @@ class Formula < ActiveRecord::Base identifiers, parts = parser.parse errors = parser.errors - quantities = Quantity.where(project: self.quantity.project, name: identifiers) - (identifiers - quantities.map(&:name) - quantity_m.keys).each do |q| + quantities = Quantity.where(project: self.quantity.project, name: identifiers.to_a) + (identifiers - quantities.map(&:name) - q_methods.keys).each do |q| errors << [:unknown_quantity, {quantity: q}] end diff --git a/db/migrate/002_load_defaults.rb b/db/migrate/002_load_defaults.rb index 9c8cfdf..22100e2 100644 --- a/db/migrate/002_load_defaults.rb +++ b/db/migrate/002_load_defaults.rb @@ -15,6 +15,8 @@ class LoadDefaults < ActiveRecord::Migration name: "Energy", description: "Total energy" e_aa = Quantity.create project: nil, domain: :diet, parent: e_a, name: "Calc. energy", description: "Total energy calculated from macronutrients" + e_ab = Quantity.create domain: :diet, parent: e_a, name: "Energy as %RM", + description: "Energy percent value relative to current resting metabolism" p_a = Quantity.create project: nil, domain: :diet, parent: nil, name: "Proteins", description: "Total amount of proteins" @@ -147,6 +149,8 @@ class LoadDefaults < ActiveRecord::Migration # Formulas go at the and to make sure dependencies exist e_aa.create_formula code: "4*Proteins + 9*Fats + 4*Carbs", zero_nil: true, unit: u_b + e_ab.create_formula code: "100*Energy/RM.lastBefore(Meal.eaten_at||Meal.created_at)", + zero_nil: true, unit: u_c b_aaa.create_formula code: "'% fat' * 'Weight'", zero_nil: true, unit: u_ab s_a = Source.create project: nil, name: "nutrition label", diff --git a/lib/body_tracking/items_with_quantities.rb b/lib/body_tracking/items_with_quantities.rb index f5c16d8..4ca54bf 100644 --- a/lib/body_tracking/items_with_quantities.rb +++ b/lib/body_tracking/items_with_quantities.rb @@ -99,7 +99,7 @@ module BodyTracking begin calculated = q.formula.calculate(inputs.to_h) rescue Exception => e - output_items.each { |o_i| subitems[q][o_i] = [BigDecimal::NAN, nil] } + output_items.each { |o_i| subitems[q][o_i] = nil } q.formula.errors.add( :code, :computation_failed, {