1
0

Moved remaining formula code to Formula module

This commit is contained in:
cryptogopher 2019-11-07 19:30:02 +01:00
parent 19e2a45ba8
commit 2a3e7e8d00
2 changed files with 22 additions and 14 deletions

View File

@ -28,22 +28,10 @@ class Quantity < ActiveRecord::Base
end
def formula_quantities
q_names = Ripper.lex(formula).map do |*, ttype, token|
token if BodyTracking::Formula::QUANTITY_TTYPES.include?(ttype)
end.compact
self.project.quantities.where(name: q_names).to_a
Formula.new(self.project, self.formula).get_quantities
end
def calculate(inputs)
paramed_formula = Ripper.lex(formula).map do |*, ttype, token|
BodyTracking::Formula::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
Formula.new(self.project, self.formula).calculate(inputs)
end
end

View File

@ -47,6 +47,26 @@ module BodyTracking
errors
end
def get_quantities
q_names = Ripper.lex(@formula).map do |*, ttype, token|
token if QUANTITY_TTYPES.include?(ttype)
end.compact
@project.quantities.where(name: q_names).to_a
end
def calculate(inputs)
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
class FormulaValidator < ActiveModel::EachValidator