Class: BggHotnessCLI::Page
- Inherits:
-
Object
- Object
- BggHotnessCLI::Page
- Defined in:
- lib/bgg-hotness-cli/page.rb
Overview
Each page is a list of 10 games. In total, the app has 5 pages (50 games).
Constant Summary collapse
- @@all =
[]
Instance Attribute Summary collapse
-
#end_rank ⇒ Object
Returns the value of attribute end_rank.
-
#games ⇒ Object
Returns the value of attribute games.
-
#page_number ⇒ Object
Returns the value of attribute page_number.
-
#start_rank ⇒ Object
Returns the value of attribute start_rank.
Class Method Summary collapse
- .all ⇒ Object
-
.make_pages ⇒ Object
Create 5 pages of 10 games.
Instance Method Summary collapse
-
#display_page ⇒ Object
Instance Methods ##.
-
#initialize(start_rank, end_rank) ⇒ Page
constructor
A new instance of Page.
Constructor Details
#initialize(start_rank, end_rank) ⇒ Page
Returns a new instance of Page.
12 13 14 15 16 17 18 |
# File 'lib/bgg-hotness-cli/page.rb', line 12 def initialize(start_rank, end_rank) @start_rank = start_rank @end_rank = end_rank @page_number = end_rank / 10 @games = [] self.class.all << self end |
Instance Attribute Details
#end_rank ⇒ Object
Returns the value of attribute end_rank.
4 5 6 |
# File 'lib/bgg-hotness-cli/page.rb', line 4 def end_rank @end_rank end |
#games ⇒ Object
Returns the value of attribute games.
4 5 6 |
# File 'lib/bgg-hotness-cli/page.rb', line 4 def games @games end |
#page_number ⇒ Object
Returns the value of attribute page_number.
4 5 6 |
# File 'lib/bgg-hotness-cli/page.rb', line 4 def page_number @page_number end |
#start_rank ⇒ Object
Returns the value of attribute start_rank.
4 5 6 |
# File 'lib/bgg-hotness-cli/page.rb', line 4 def start_rank @start_rank end |
Class Method Details
.all ⇒ Object
8 9 10 |
# File 'lib/bgg-hotness-cli/page.rb', line 8 def self.all @@all end |
.make_pages ⇒ Object
Create 5 pages of 10 games
95 96 97 98 99 100 101 102 103 |
# File 'lib/bgg-hotness-cli/page.rb', line 95 def self.make_pages _start_rank = 1 # temp local variable _end_rank = 10 # temp local variable 5.times do BggHotnessCLI::Page.new(_start_rank, _end_rank) _start_rank += 10 _end_rank += 10 end end |
Instance Method Details
#display_page ⇒ Object
Instance Methods ##
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/bgg-hotness-cli/page.rb', line 21 def display_page # Displays list of games between @start_rank and @end_rank # Print header BggHotnessCLI::CLI.header puts "The top #{@start_rank}–#{@end_rank} hot games on BGG." puts choices = [] # First option in array is to see next 10 games if @end_rank != 50 choices << {name: "See the next 10 games on the list", value: "next"} else choices << {name: "(end of list) Start over", value: "next"} end # Put each game into choices @games.each do |game| # If the rank is less than 10, add an extra space to make numbers line up choices << {name: " #{game.rank}. #{game.name} (#{game.year})", value: game} if game.rank.to_i < 10 choices << {name: "#{game.rank}. #{game.name} (#{game.year})", value: game} if game.rank.to_i >= 10 end # Last option is always to quit choices << {name: "Quit", value: "quit"} # Set up prompt prompt = TTY::Prompt.new(active_color: :blue) # Set up greeting greeting = "Select a game to view its details:" # Capture input (input takes :value from choice[x]) & display prompt @input = prompt.select(greeting, choices, per_page: 12, cycle: true) # Parse user input if @input == "next" # If user selects next part of the list... if @end_rank == 50 # ...if at the end of the list (50), display the first page BggHotnessCLI::Page.all[0].display_page else # ...otherwise, display the next page # this works because the first page is [0] index, # but it's page # is 1, meaning this will show # the second page (which is at all[1]). # TODO: make this less confusing? BggHotnessCLI::Page.all[@page_number].display_page end elsif @input == 'quit' # If they quit, run "goodbye" method BggHotnessCLI::CLI.goodbye else # Otherwise, @input is a game. Display its details. @input.display_details end end |