Moved remaining formula code to Formula module
This commit is contained in:
parent
19e2a45ba8
commit
2a3e7e8d00
@ -28,22 +28,10 @@ class Quantity < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def formula_quantities
|
def formula_quantities
|
||||||
q_names = Ripper.lex(formula).map do |*, ttype, token|
|
Formula.new(self.project, self.formula).get_quantities
|
||||||
token if BodyTracking::Formula::QUANTITY_TTYPES.include?(ttype)
|
|
||||||
end.compact
|
|
||||||
self.project.quantities.where(name: q_names).to_a
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def calculate(inputs)
|
def calculate(inputs)
|
||||||
paramed_formula = Ripper.lex(formula).map do |*, ttype, token|
|
Formula.new(self.project, self.formula).calculate(inputs)
|
||||||
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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -47,6 +47,26 @@ module BodyTracking
|
|||||||
|
|
||||||
errors
|
errors
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
class FormulaValidator < ActiveModel::EachValidator
|
class FormulaValidator < ActiveModel::EachValidator
|
||||||
|
Reference in New Issue
Block a user