Class: TriviaFactory::Question
- Inherits:
-
Object
- Object
- TriviaFactory::Question
- Defined in:
- lib/trivia_factory.rb
Direct Known Subclasses
AcademyAwardsQuestion, CapitalCitiesQuestion, CompanyQuestion, MathQuestion, SportsQuestion, UsStateCapitalsQuestion, VocabularyQuestion
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
-
#answer ⇒ Object
Returns the value of attribute answer.
-
#answer_type ⇒ Object
Returns the value of attribute answer_type.
-
#choices ⇒ Object
Returns the value of attribute choices.
-
#label ⇒ Object
Returns the value of attribute label.
-
#question_type ⇒ Object
Returns the value of attribute question_type.
Class Method Summary collapse
- .academy_awards ⇒ Object
- .capital_cities ⇒ Object
- .company ⇒ Object
- .fetch_csv(name) ⇒ Object
- .generate ⇒ Object
- .math ⇒ Object
- .question_types ⇒ Object
- .random ⇒ Object
- .sports ⇒ Object
- .us_state_capitals ⇒ Object
- .vocabulary ⇒ Object
Instance Method Summary collapse
-
#initialize ⇒ Question
constructor
A new instance of Question.
- #to_h ⇒ Object
Constructor Details
#initialize ⇒ Question
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
#answer ⇒ Object
Returns the value of attribute answer.
14 15 16 |
# File 'lib/trivia_factory.rb', line 14 def answer @answer end |
#answer_type ⇒ Object
Returns the value of attribute answer_type.
13 14 15 |
# File 'lib/trivia_factory.rb', line 13 def answer_type @answer_type end |
#choices ⇒ Object
Returns the value of attribute choices.
12 13 14 |
# File 'lib/trivia_factory.rb', line 12 def choices @choices end |
#label ⇒ Object
Returns the value of attribute label.
10 11 12 |
# File 'lib/trivia_factory.rb', line 10 def label @label end |
#question_type ⇒ Object
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_awards ⇒ Object
65 66 67 |
# File 'lib/trivia_factory.rb', line 65 def academy_awards TriviaFactory::AcademyAwardsQuestion.generate end |
.capital_cities ⇒ Object
57 58 59 |
# File 'lib/trivia_factory.rb', line 57 def capital_cities TriviaFactory::CapitalCitiesQuestion.generate end |
.company ⇒ Object
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 |
.generate ⇒ Object
45 46 47 |
# File 'lib/trivia_factory.rb', line 45 def generate TriviaFactory::Question.new end |
.math ⇒ Object
49 50 51 |
# File 'lib/trivia_factory.rb', line 49 def math TriviaFactory::MathQuestion.generate end |
.question_types ⇒ Object
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 |
.random ⇒ Object
40 41 42 43 |
# File 'lib/trivia_factory.rb', line 40 def random klass = question_types.sample TriviaFactory.const_get(klass).generate end |
.sports ⇒ Object
61 62 63 |
# File 'lib/trivia_factory.rb', line 61 def sports TriviaFactory::SportsQuestion.generate end |
.us_state_capitals ⇒ Object
53 54 55 |
# File 'lib/trivia_factory.rb', line 53 def us_state_capitals TriviaFactory::UsStateCapitalsQuestion.generate end |
.vocabulary ⇒ Object
69 70 71 |
# File 'lib/trivia_factory.rb', line 69 def vocabulary TriviaFactory::VocabularyQuestion.generate end |
Instance Method Details
#to_h ⇒ Object
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 |