Fixes for formula computation
Added default quantity
This commit is contained in:
parent
8749710330
commit
9c3d31c131
@ -30,7 +30,7 @@ class Formula < ActiveRecord::Base
|
|||||||
args = []
|
args = []
|
||||||
@parts.each do |p|
|
@parts.each do |p|
|
||||||
code = p[:type] == :indexed ?
|
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)
|
args << get_binding(quantities, args, length).eval(code)
|
||||||
end
|
end
|
||||||
args.last.map { |v| [v, self.unit] }
|
args.last.map { |v| [v, self.unit] }
|
||||||
@ -47,8 +47,8 @@ class Formula < ActiveRecord::Base
|
|||||||
identifiers, parts = parser.parse
|
identifiers, parts = parser.parse
|
||||||
errors = parser.errors
|
errors = parser.errors
|
||||||
|
|
||||||
quantities = Quantity.where(project: self.quantity.project, name: identifiers)
|
quantities = Quantity.where(project: self.quantity.project, name: identifiers.to_a)
|
||||||
(identifiers - quantities.map(&:name) - quantity_m.keys).each do |q|
|
(identifiers - quantities.map(&:name) - q_methods.keys).each do |q|
|
||||||
errors << [:unknown_quantity, {quantity: q}]
|
errors << [:unknown_quantity, {quantity: q}]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -15,6 +15,8 @@ class LoadDefaults < ActiveRecord::Migration
|
|||||||
name: "Energy", description: "Total energy"
|
name: "Energy", description: "Total energy"
|
||||||
e_aa = Quantity.create project: nil, domain: :diet, parent: e_a,
|
e_aa = Quantity.create project: nil, domain: :diet, parent: e_a,
|
||||||
name: "Calc. energy", description: "Total energy calculated from macronutrients"
|
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,
|
p_a = Quantity.create project: nil, domain: :diet, parent: nil,
|
||||||
name: "Proteins", description: "Total amount of proteins"
|
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
|
# Formulas go at the and to make sure dependencies exist
|
||||||
e_aa.create_formula code: "4*Proteins + 9*Fats + 4*Carbs", zero_nil: true,
|
e_aa.create_formula code: "4*Proteins + 9*Fats + 4*Carbs", zero_nil: true,
|
||||||
unit: u_b
|
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
|
b_aaa.create_formula code: "'% fat' * 'Weight'", zero_nil: true, unit: u_ab
|
||||||
|
|
||||||
s_a = Source.create project: nil, name: "nutrition label",
|
s_a = Source.create project: nil, name: "nutrition label",
|
||||||
|
@ -99,7 +99,7 @@ module BodyTracking
|
|||||||
begin
|
begin
|
||||||
calculated = q.formula.calculate(inputs.to_h)
|
calculated = q.formula.calculate(inputs.to_h)
|
||||||
rescue Exception => e
|
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(
|
q.formula.errors.add(
|
||||||
:code, :computation_failed,
|
:code, :computation_failed,
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user