Sha256: b035061a308f31f8556e173713ce6bf8e1610bf5b054ea9ef1b68495ad6e0c31

Contents?: true

Size: 924 Bytes

Versions: 8

Compression:

Stored size: 924 Bytes

Contents

require "cat_engine/bank"
require "cat_engine/engine"

module StudyEngine
  class Result < ActiveRecord::Base
    belongs_to :assessment, touch: true

    has_many :answers

    delegate :current_question, to: :bank

    def bank
      CatEngine::Bank.new(bank_id, history, assessment.full_history)
    end

    def name
      bank_id.sub(".flat","")
    end

    def history
      answers.pluck(:response_id).join(" ")
    end

    def save_answer answer
      wrapper do # race condition could bypass validity check
        if bank.valid_answer?(answer.response_id)
          answers << answer
          update_attributes bank.to_h
          save!
        end
      end
    end

    private

    def wrapper
      # UGH HACK HACK HACK why can't FlatEngine handle being wrapped in a lock?
      if bank.send(:engine).is_a? CatEngine::Engine
        with_lock { yield }
      else
        yield
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
study_engine-0.3.1 app/models/study_engine/result.rb
study_engine-0.3.0 app/models/study_engine/result.rb
study_engine-0.2.2 app/models/study_engine/result.rb
study_engine-0.2.1 app/models/study_engine/result.rb
study_engine-0.2.0 app/models/study_engine/result.rb
study_engine-0.1.1 app/models/study_engine/result.rb
study_engine-0.1.0 app/models/study_engine/result.rb
study_engine-0.0.2 app/models/study_engine/result.rb