Class: Quby::Answers::Services::OutcomeCalculation

Inherits:
Object
  • Object
show all
Defined in:
lib/quby/answers/services/outcome_calculation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(answer) ⇒ OutcomeCalculation

Returns a new instance of OutcomeCalculation.



13
14
15
# File 'lib/quby/answers/services/outcome_calculation.rb', line 13

def initialize(answer)
  @answer = answer
end

Instance Attribute Details

#answerObject (readonly)

Returns the value of attribute answer.



11
12
13
# File 'lib/quby/answers/services/outcome_calculation.rb', line 11

def answer
  @answer
end

Instance Method Details

#calculateObject

rubocop:disable Metrics/MethodLength



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/quby/answers/services/outcome_calculation.rb', line 17

def calculate # rubocop:disable Metrics/MethodLength
  results = {}
  score_results = {}
  action_results = {}
  completion_result = {}

  questionnaire.score_calculations.each do |key, calculation|
    begin
      result = ScoreCalculator.calculate(questionnaire: answer.questionnaire,
                                         values: value_by_regular_values,
                                         observation_time: answer.observation_time,
                                         patient_attrs: patient&.slice("birthyear", "birthdate", "gender"),
                                         respondent_attrs: patient&.slice("respondent_type"),
                                         &calculation.calculation)

      if calculation.completion
        result = {"value" => result}
      elsif calculation.score
        result = (result || {}).reverse_merge(calculation.options)
      end

      results[key] = result
    rescue ScoreCalculator::MissingAnswerValues => exception
      results[key] = calculation.options.merge(missing_values: exception.missing)
    rescue StandardError => exception
      if defined? Roqua::Support::Errors
        Roqua::Support::Errors.report exception, root_path: Rails.root.to_s
      end
      results[key] = {exception: exception.message,
                      backtrace: exception.backtrace}.reverse_merge(calculation.options)
    end

    score_results[key] = results[key] if calculation.score
    action_results[key] = results[key] if calculation.action
    completion_result = results[key] if calculation.completion
  end

  Entities::Outcome.new(scores: score_results,
                        actions: action_results,
                        completion: completion_result,
                        generated_at: Time.now)
end

#update_scoresObject

Calculate scores and actions, write to the database but bypass any validations This function is called by parts of the system that only want to calculate stuff, and can’t help it if an answer is not completed.



63
64
65
66
67
68
# File 'lib/quby/answers/services/outcome_calculation.rb', line 63

def update_scores
  # Now we can fill it back up
  outcome = calculate
  answer.outcome = outcome
  Quby.answers.update!(answer)
end