Class: Question
- Inherits:
-
Object
- Object
- Question
- Extended by:
- CliTrivia::Filter, CliTrivia::Findable
- Defined in:
- lib/cli_trivia/question.rb
Overview
This is a question object. Question objects are created based on the user choice (either random or category). A JSON hash is returned from the openTDB GET request and stored in each question object.
Constant Summary collapse
- @@all =
[]
Instance Attribute Summary collapse
-
#category ⇒ Object
Returns the value of attribute category.
-
#correct_answer ⇒ Object
Returns the value of attribute correct_answer.
-
#difficulty ⇒ Object
Returns the value of attribute difficulty.
-
#incorrect_answers ⇒ Object
Returns the value of attribute incorrect_answers.
-
#question ⇒ Object
Returns the value of attribute question.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
-
#add_category(name) ⇒ Object
Allows for the adding of a Category Object to self.
-
#initialize(question_data) ⇒ Question
constructor
Because all metadata regarding a question is available on creation of each Question Object, it is populated upon initialization and not after.
Methods included from CliTrivia::Findable
find_by_name, find_or_create_by_name
Methods included from CliTrivia::Filter
Constructor Details
#initialize(question_data) ⇒ Question
Because all metadata regarding a question is available on creation of each Question Object, it is populated upon initialization and not after.
12 13 14 15 16 17 18 19 20 |
# File 'lib/cli_trivia/question.rb', line 12 def initialize(question_data) @category = add_category(question_data['category']) @type = question_data['type'] @difficulty = question_data['difficulty'] @question = Question.format_string(question_data['question']) @correct_answer = question_data['correct_answer'] @incorrect_answers = question_data['incorrect_answers'] @@all << self end |
Instance Attribute Details
#category ⇒ Object
Returns the value of attribute category.
4 5 6 |
# File 'lib/cli_trivia/question.rb', line 4 def category @category end |
#correct_answer ⇒ Object
Returns the value of attribute correct_answer.
4 5 6 |
# File 'lib/cli_trivia/question.rb', line 4 def correct_answer @correct_answer end |
#difficulty ⇒ Object
Returns the value of attribute difficulty.
4 5 6 |
# File 'lib/cli_trivia/question.rb', line 4 def difficulty @difficulty end |
#incorrect_answers ⇒ Object
Returns the value of attribute incorrect_answers.
4 5 6 |
# File 'lib/cli_trivia/question.rb', line 4 def incorrect_answers @incorrect_answers end |
#question ⇒ Object
Returns the value of attribute question.
4 5 6 |
# File 'lib/cli_trivia/question.rb', line 4 def question @question end |
#type ⇒ Object
Returns the value of attribute type.
4 5 6 |
# File 'lib/cli_trivia/question.rb', line 4 def type @type end |
Class Method Details
.all ⇒ Object
28 29 30 |
# File 'lib/cli_trivia/question.rb', line 28 def self.all @@all end |
.clear_all ⇒ Object
32 33 34 |
# File 'lib/cli_trivia/question.rb', line 32 def self.clear_all @@all.clear end |
Instance Method Details
#add_category(name) ⇒ Object
Allows for the adding of a Category Object to self. A question BELONGS to a category.
23 24 25 26 |
# File 'lib/cli_trivia/question.rb', line 23 def add_category(name) category = Category.find_or_create_by_name(name) category.add_question(self) end |