Class: TriviaFactory::SportsQuestion

Inherits:
Question
  • Object
show all
Defined in:
lib/trivia_factory/sports_question.rb

Constant Summary collapse

QUESTION_SUB_TYPES =
[:champions, :mvps].freeze

Constants inherited from Question

Question::ANSWER_TYPES, Question::QUESTION_TYPES

Instance Attribute Summary

Attributes inherited from Question

#answer, #answer_type, #choices, #label, #question_type

Class Method Summary collapse

Methods inherited from Question

academy_awards, capital_cities, company, fetch_csv, #initialize, math, question_types, random, sports, #to_h, us_state_capitals, vocabulary

Constructor Details

This class inherits a constructor from TriviaFactory::Question

Class Method Details

.generateObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/trivia_factory/sports_question.rb', line 7

def generate
  # File: sports_champions.csv
  # 0: year
  # 1: championship ("wold series")
  # 2: counter/number ("IV" for super bowl)
  # 3: winner
  # 4: loser
  # Format: "Who defeated the [LOSER] to win (the) [YEAR] [CHAMPIONSHIP]?"
  question = TriviaFactory::Question.new
  data = fetch_csv('sports_champions')
  answer_row = data.sample
  if answer_row[2].nil?
    question.label = "In #{answer_row[0]} who defeated the #{answer_row[4]} to win the #{answer_row[1]}?"
  else
    question.label = "In #{answer_row[0]} who defeated the #{answer_row[4]} to win #{answer_row[1]} #{answer_row[2]}?"
  end
  question.choices = [answer_row[3]]
  question.question_type = :multiple_choice
  question.answer_type = :choice_index
  context_data = data.select {|row| row[1] == answer_row[1] }
  loop do
    row = context_data.sample
    question.choices << row[3] if question.choices.index(row[3]).nil?
    break if question.choices.size == 4
  end
  question.choices.shuffle!
  question.answer = question.choices.index(answer_row[3])
  question
end