Class: TriviaFactory::Question

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

Constant Summary collapse

QUESTION_TYPES =
[:true_false, :multiple_choice, :fill_in_the_blank].freeze
ANSWER_TYPES =
[:choice_index, :boolean, :string, :integer].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQuestion

Returns a new instance of Question.



16
17
18
19
20
21
22
# File 'lib/trivia_factory.rb', line 16

def initialize
  @label = "Who won the MLB World Series in 2016?"
  @question_type = :multiple_choice
  @choices = ["San Francisco Giants", "Chicago Cubs", "Cleveland Indians", "Golden State Warriors"]
  @answer_type = :choice_index
  @answer = 1
end

Instance Attribute Details

#answerObject

Returns the value of attribute answer.



14
15
16
# File 'lib/trivia_factory.rb', line 14

def answer
  @answer
end

#answer_typeObject

Returns the value of attribute answer_type.



13
14
15
# File 'lib/trivia_factory.rb', line 13

def answer_type
  @answer_type
end

#choicesObject

Returns the value of attribute choices.



12
13
14
# File 'lib/trivia_factory.rb', line 12

def choices
  @choices
end

#labelObject

Returns the value of attribute label.



10
11
12
# File 'lib/trivia_factory.rb', line 10

def label
  @label
end

#question_typeObject

Returns the value of attribute question_type.



11
12
13
# File 'lib/trivia_factory.rb', line 11

def question_type
  @question_type
end

Class Method Details

.academy_awardsObject



65
66
67
# File 'lib/trivia_factory.rb', line 65

def academy_awards
  TriviaFactory::AcademyAwardsQuestion.generate
end

.capital_citiesObject



57
58
59
# File 'lib/trivia_factory.rb', line 57

def capital_cities
  TriviaFactory::CapitalCitiesQuestion.generate
end

.companyObject



73
74
75
# File 'lib/trivia_factory.rb', line 73

def company
  TriviaFactory::CompanyQuestion.generate
end

.fetch_csv(name) ⇒ Object



77
78
79
80
# File 'lib/trivia_factory.rb', line 77

def fetch_csv(name)
  file = File.join(File.dirname(__FILE__), "data", "#{name}.csv")
  CSV.read(file)
end

.generateObject



45
46
47
# File 'lib/trivia_factory.rb', line 45

def generate
  TriviaFactory::Question.new
end

.mathObject



49
50
51
# File 'lib/trivia_factory.rb', line 49

def math
  TriviaFactory::MathQuestion.generate
end

.question_typesObject



36
37
38
# File 'lib/trivia_factory.rb', line 36

def question_types
  TriviaFactory.constants.select { |k| TriviaFactory.const_get(k).instance_of?(Class) && k != :Question }
end

.randomObject



40
41
42
43
# File 'lib/trivia_factory.rb', line 40

def random
  klass = question_types.sample
  TriviaFactory.const_get(klass).generate
end

.sportsObject



61
62
63
# File 'lib/trivia_factory.rb', line 61

def sports
  TriviaFactory::SportsQuestion.generate
end

.us_state_capitalsObject



53
54
55
# File 'lib/trivia_factory.rb', line 53

def us_state_capitals
  TriviaFactory::UsStateCapitalsQuestion.generate
end

.vocabularyObject



69
70
71
# File 'lib/trivia_factory.rb', line 69

def vocabulary
  TriviaFactory::VocabularyQuestion.generate
end

Instance Method Details

#to_hObject



24
25
26
27
28
29
30
31
32
# File 'lib/trivia_factory.rb', line 24

def to_h
  {
    label: @label,
    question_type: @question_type,
    choices: @choices,
    answer: @answer,
    answer_type: @answer_type
  }
end