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
|
||||
|
||||
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
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user