1
0

Fixes for formula computation

Added default quantity
This commit is contained in:
cryptogopher 2020-05-05 22:02:48 +02:00
parent 8749710330
commit 9c3d31c131
3 changed files with 8 additions and 4 deletions

View File

@ -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

View File

@ -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",

View File

@ -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,
{