Class: Question

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CliTrivia::Findable

find_by_name, find_or_create_by_name

Methods included from CliTrivia::Filter

format_string

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

#categoryObject

Returns the value of attribute category.



4
5
6
# File 'lib/cli_trivia/question.rb', line 4

def category
  @category
end

#correct_answerObject

Returns the value of attribute correct_answer.



4
5
6
# File 'lib/cli_trivia/question.rb', line 4

def correct_answer
  @correct_answer
end

#difficultyObject

Returns the value of attribute difficulty.



4
5
6
# File 'lib/cli_trivia/question.rb', line 4

def difficulty
  @difficulty
end

#incorrect_answersObject

Returns the value of attribute incorrect_answers.



4
5
6
# File 'lib/cli_trivia/question.rb', line 4

def incorrect_answers
  @incorrect_answers
end

#questionObject

Returns the value of attribute question.



4
5
6
# File 'lib/cli_trivia/question.rb', line 4

def question
  @question
end

#typeObject

Returns the value of attribute type.



4
5
6
# File 'lib/cli_trivia/question.rb', line 4

def type
  @type
end

Class Method Details

.allObject



28
29
30
# File 'lib/cli_trivia/question.rb', line 28

def self.all
  @@all
end

.clear_allObject



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