From 9a79e8fa557e1728e8ed41fb5fa37990feec5946 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Fri, 20 Mar 2020 00:57:20 +0100 Subject: [PATCH] Making Formula#calculate public --- app/models/formula.rb | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/app/models/formula.rb b/app/models/formula.rb index 8871e15..ee18b5a 100644 --- a/app/models/formula.rb +++ b/app/models/formula.rb @@ -16,23 +16,6 @@ class Formula < ActiveRecord::Base end end - private - - def parse - parser = FormulaBuilder.new(self.code) - identifiers, parts = parser.parse - errors = parser.errors - - quantities = Quantity.where(project: self.quantity.project, name: identifiers) - quantities_names = quantities.pluck(:name) - (identifiers - quantities_names).each do |q| - errors << [:unknown_quantity, {quantity: q}] - end - - @parts, @quantities = parts, quantities.to_a if errors.empty? - errors - end - def calculate(inputs) quantities = inputs.map { |q, v| [q.name, v.transpose[0]] }.to_h length = quantities.values.first.length @@ -52,6 +35,23 @@ class Formula < ActiveRecord::Base [[nil, nil]] * length end + private + + def parse + parser = FormulaBuilder.new(self.code) + identifiers, parts = parser.parse + errors = parser.errors + + quantities = Quantity.where(project: self.quantity.project, name: identifiers) + quantities_names = quantities.pluck(:name) + (identifiers - quantities_names).each do |q| + errors << [:unknown_quantity, {quantity: q}] + end + + @parts, @quantities = parts, quantities.to_a if errors.empty? + errors + end + def get_binding(quantities, args, length) binding end