Class: Category

Inherits:
Object
  • Object
show all
Extended by:
CliTrivia::Findable
Defined in:
lib/cli_trivia/category.rb

Overview

Category Objects are created upon initializing the app using bin/cli_trivia. This allows the user to choose between random and category. Each Category has MANY questions, but there is only one of every Category.

Constant Summary collapse

@@all =

Categories are stored in @@all and are accessed to print the list of user selectable categories.

[]

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

Constructor Details

#initialize(name, id) ⇒ Category

Returns a new instance of Category.



10
11
12
13
14
15
# File 'lib/cli_trivia/category.rb', line 10

def initialize(name, id)
  @id = id
  @name = name
  @questions = []
  @@all << self
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#questionsObject

Returns the value of attribute questions.



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

def questions
  @questions
end

Class Method Details

.allObject



17
18
19
# File 'lib/cli_trivia/category.rb', line 17

def self.all
  @@all
end

.all_by_nameObject

A modification of the self.all method to return all Categories alphabetically. Used when categories are printed for user choice.



23
24
25
26
27
28
29
30
# File 'lib/cli_trivia/category.rb', line 23

def self.all_by_name
  sorted = @@all.sort! {|a, b|  a.name <=> b.name}
  sorted_names = []
  sorted.each do |category|
    sorted_names << category.name
  end
  sorted_names
end

.clear_allObject



38
39
40
# File 'lib/cli_trivia/category.rb', line 38

def self.clear_all
  @@all.clear
end

Instance Method Details

#add_question(question) ⇒ Object

This stores questions that belong to this Category. @questions is an instance variable available to this Category Object only



34
35
36
# File 'lib/cli_trivia/category.rb', line 34

def add_question(question)
  @questions << question unless question.nil? || @questions.include?(question)
end