Sha256: 86f22128483ed94828d1b81b16c4f9af67e6ca4307b49b54340a132e22a0ecec

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

module Quizzes
  class GameFactory

    def initialize(params = {})
      @user = params[:user]
      @level = params[:level]
      @number_of_questions = params[:number_of_questions]
    end

    def generate
      game = Quizzes::Game.new
      game.user = @user
      game.level = @level

      questions = Quizzes::QuestionFactory.new({
        levels: group_of_levels,
        used_questions: used_questions,
        number_of_questions: verified_number_of_questions
      }).generate

      game.game_questions = Quizzes::GameQuestionFactory.new({ game: game, questions: questions }).generate
      game
    end

    private

    def group_of_levels
      @level.present? ? [@level] : Quizzes::Level.all_and_nil
    end

    def used_questions
      @user.present? ? @user.questions : []
    end

    def verified_number_of_questions
      @number_of_questions || (@level.present? ? @level.number_of_questions : total_questions_by_game)
    end

    def total_questions_by_game
      Rails.configuration.total_questions_by_game rescue 10
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
quizzes-0.1.0 app/models/quizzes/game_factory.rb