Class: Main_menu

Inherits:
Object
  • Object
show all
Defined in:
lib/classes/Main_menu.rb

Constant Summary collapse

@@pokemon_data =

Creates an array of hashes from the CSV file with keys taken from CSV headers.

SmarterCSV.process(File.expand_path("../pokemon.csv", __FILE__))

Class Method Summary collapse

Class Method Details

.delete_pokemonObject



81
82
83
# File 'lib/classes/Main_menu.rb', line 81

def self.delete_pokemon
  @@pokemon_data = Delete.delete(@@pokemon_data)
end

.exit_menuObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/classes/Main_menu.rb', line 85

def self.exit_menu
  exit_menu_prompt = TTY::Prompt.new(active_color: :red)
  save_changes = exit_menu_prompt.ask('Are you sure you want to save your changes?')
  if save_changes == true
    CSV.open(File.expand_path("../pokemon.csv", __FILE__), 'wb') do |csv|
      keys = @@pokemon_data.first.keys
      csv << keys
      @@pokemon_data.each do |hash|
        csv << hash.values_at(*keys)
      end
    end
    exit
  else
    exit
  end
end

.list_pokemonObject



63
64
65
# File 'lib/classes/Main_menu.rb', line 63

def self.list_pokemon
  List.list_menu(@@pokemon_data)
end

.new_pokemonObject



75
76
77
78
79
# File 'lib/classes/Main_menu.rb', line 75

def self.new_pokemon
  new_hash = New.add_pokemon
  @@pokemon_data << new_hash
  Print.print_pokemon_expanded(new_hash)
end

.return?Boolean

Returns:

  • (Boolean)


57
58
59
60
61
# File 'lib/classes/Main_menu.rb', line 57

def self.return?
  puts 'Press enter to return to the Main Menu.'
  user_input = gets.chomp
  run
end

.runObject



16
17
18
19
20
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
# File 'lib/classes/Main_menu.rb', line 16

def self.run
  loop do
    system 'clear'
    sleep 0.1
    fork { exec "artii 'Pokedex' --font larry3d | lolcat" }
    sleep(0.3)
    main_menu_prompt = TTY::Prompt.new(active_color: :red)
    puts '-' * 40
    puts '        Welcome to your Pokedex!        '.blue.on_red.blink
    puts '-' * 40
    user_input = main_menu_prompt.select('Please select from the following:') do |menu|
      menu.choice 'List Pokemon', 1
      menu.choice 'Search for a Pokemon', 2
      menu.choice 'Update an existing Pokemon', 3
      menu.choice 'Add a new Pokemon', 4
      menu.choice 'Delete a Pokemon', 5
      menu.choice 'Exit and save changes', 6
      # menu.choice 'Print hashes', 7
    end
    case user_input
    when 1
      list_pokemon
    when 2
      search_pokemon
    when 3
      update_pokemon
    when 4
      new_pokemon
    when 5
      delete_pokemon
    when 6
      exit_menu
    # uncomment below 2 lines to allow developer feature to check hashes
    # when 7
    # pp @@pokemon_data
    else
      'Invalid selection, please select from the following options.'
    end
  end
end

.search_pokemonObject



67
68
69
# File 'lib/classes/Main_menu.rb', line 67

def self.search_pokemon
  Print.print_pokemon_expanded(Search.return_hash(@@pokemon_data, Search.by_name(@@pokemon_data)))
end

.update_pokemonObject



71
72
73
# File 'lib/classes/Main_menu.rb', line 71

def self.update_pokemon
  Update.update_pokemon_menu(@@pokemon_data)
end