Sha256: d01882a5c3370a7ca2d7f76f84fcf002a6d14f00656d75e739b5f283a0b4c55c

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

module CitizenBudgetModel
  class Simulator < ActiveRecord::Base
    acts_as_paranoid
    translates :name, fallbacks_for_empty_translations: true
    globalize_accessors

    belongs_to :organization
    has_many :sections, -> { order(:position) }, dependent: :destroy

    validates_presence_of :organization_id
    validates :name, 'citizen_budget_model/locale' => true
    validates :equation, 'citizen_budget_model/equation' => true, allow_blank: true

    # Sets the simulator as active and all others as inactive.
    def activate!
      self.class.where(active: true).each do |simulator|
        simulator.update!(active: false)
      end
      update!(active: true)
    end

    # Returns the equation or a default equation if not set.
    #
    # @return [String] an equation
    def working_equation
      if equation?
        equation
      else
        default_equation
      end
    end

    # Returns a default equation, where the difference between the chosen value
    # and the default value is multiplied by the unit value for each variable.
    #
    # @return [String] a default equation
    def default_equation
      equation = []
      sections.each do |section|
        section.questions.each do |question|
          equation << question.working_equation
        end
      end
      equation.reject(&:empty?).join(' + ')
    end

    # Solves the equation with the given variables' values.
    #
    # @param [Hash{Symbol,String=>Float,String}] variables variables
    # @return [Float] a solution
    def solve(variables)
      keys = []
      values = []

      variables.each do |key,value|
        keys << key.to_sym
        values << Float(value)
      end

      # Struct will raise a NameError, whereas OpenStruct will not.
      eval(working_equation, Struct.new(*keys).new(*values).instance_eval{binding})
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
citizen_budget_model-0.0.1 app/models/citizen_budget_model/simulator.rb