1
0

Quantity computation from formula

This commit is contained in:
cryptogopher 2019-11-06 23:11:58 +01:00
parent 304c8c788d
commit 88e5cb430a
2 changed files with 13 additions and 2 deletions

View File

@ -71,7 +71,9 @@ class Ingredient < ActiveRecord::Base
i,
input_q.map do |i_q|
nutrient_data = (completed_q[i_q.name] || nutrients[i_q.name])[i.id]
[i_q.name, (nutrient_data || [nil, nil])[0]]
# FIXME: result for computation with nil values (substituted with 0s)
# should be marked as not precise
[i_q.name, (nutrient_data || [0, nil])[0]]
end.to_h
]
end

View File

@ -70,6 +70,15 @@ class Quantity < ActiveRecord::Base
end
def calculate(inputs)
inputs.map { |i, values| [i, 1.0] }
paramed_formula = Ripper.lex(formula).map do |*, ttype, token|
QUANTITY_TTYPES.include?(ttype) ? "params['#{token}']" : token
end.join
inputs.map { |i, values| [i, get_binding(values).eval(paramed_formula)] }
end
private
def get_binding(params)
binding
end
end