Class: CitizenBudgetModel::Question

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/citizen_budget_model/question.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#labels_as_listObject

Returns the value of attribute labels_as_list.



37
38
39
# File 'app/models/citizen_budget_model/question.rb', line 37

def labels_as_list
  @labels_as_list
end

#maximumObject

Returns the value of attribute maximum.



37
38
39
# File 'app/models/citizen_budget_model/question.rb', line 37

def maximum
  @maximum
end

#minimumObject

Returns the value of attribute minimum.



37
38
39
# File 'app/models/citizen_budget_model/question.rb', line 37

def minimum
  @minimum
end

#options_as_listObject

Returns the value of attribute options_as_list.



37
38
39
# File 'app/models/citizen_budget_model/question.rb', line 37

def options_as_list
  @options_as_list
end

#stepObject

Returns the value of attribute step.



37
38
39
# File 'app/models/citizen_budget_model/question.rb', line 37

def step
  @step
end

Instance Method Details

#default_equationString

Returns a default equation, where the difference between the chosen value and the default value is multiplied by the unit value.

Returns:

  • (String)

    a default equation



57
58
59
60
61
# File 'app/models/citizen_budget_model/question.rb', line 57

def default_equation
  if machine_name.present? && default_value.present? && unit_value_down.present? && unit_value_up.present?
    "(#{machine_name} - #{default_value}) * (#{machine_name} < #{default_value} ? #{unit_value_down} : #{unit_value_up})"
  end
end

#display_nameString

Returns a question’s name, title or “Untitled”.

Returns:

  • (String)

    a question’s name, title or “Untitled”



42
43
44
# File 'app/models/citizen_budget_model/question.rb', line 42

def display_name
  name.present? && name || title.present? && title || _('Untitled')
end

#solve(value) ⇒ Float

Solves the equation with the variable’s given value.

Parameters:

  • value (Float, String)

    value

Returns:

  • (Float)

    a solution



67
68
69
70
71
# File 'app/models/citizen_budget_model/question.rb', line 67

def solve(value)
  if working_equation.present? && machine_name.present?
    eval(working_equation, Struct.new(machine_name.to_sym).new(Float(value)).instance_eval{binding})
  end
end

#working_equationString

Returns the equation or a default equation if not set.

Returns:

  • (String)

    an equation



49
50
51
# File 'app/models/citizen_budget_model/question.rb', line 49

def working_equation
  default_equation || ''
end