Top Level Namespace

Defined Under Namespace

Modules: MonsterEncyclopedia Classes: Monster

Instance Method Summary collapse

Instance Method Details

#add_monster(monsters_array) ⇒ Object



11
12
13
14
15
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
# File 'lib/add_monster.rb', line 11

def add_monster(monsters_array)
  info = true
  
  while info == true
    system 'clear'
    puts '-'.colorize(:light_red) * 45
    puts "What is the name of the monster?"
    print '> '
    monster_name = gets.chomp 
    puts '-'.colorize(:light_red) * 45
    puts "Where was the monster sighted?"
    print '> '
    monster_location = gets.chomp
    puts '-'.colorize(:light_red) * 45
    puts "What does the monster look like?"
    print '> '
    monster_description = gets.chomp
    puts '-'.colorize(:light_red) * 45
    puts "Is your entry correct?"
    puts "1. Yes => Add entry to encyclopedia"
    puts "2. No  => Cancel entry return to menu"
    while info == true
      case gets.chomp.to_i
      when 1
        new_monster = Monster.new(monster_name, monster_location, monster_description)
        monsters_array << new_monster
        CSV.open("data.csv", "ab") do |csv|
          csv << [monster_name, monster_location, monster_description]
          system 'clear'
          info = false
        end
      when 2
        system 'clear'
        info = false
      else
        puts "Please pick 1 or 2"
      end
    end
  end
end

#exit_programObject



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/invalid_and_exit.rb', line 11

def exit_program()
    system 'clear'
    puts '-'.colorize(:light_red) * 45
    puts "Thankyou for using the Monster Encyclopedia!"
    puts '-'.colorize(:light_red) * 45
    text = Artii::Base.new :font => 'epic'
    puts text.asciify('--M:E--').colorize(:color => :black, :background => :red)
    puts '-'.colorize(:light_red) * 45
    sleep 3.0
    system "clear"
    exit
end

#invalid_inputObject



2
3
4
5
6
7
8
9
# File 'lib/invalid_and_exit.rb', line 2

def invalid_input
    system 'clear'
    puts '-'.colorize(:light_red) * 45
    puts "Please pick a number from the menu."
    puts '-'.colorize(:light_red) * 45
    sleep 3.0
    system 'clear'
end


22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/monster_encyclopedia.rb', line 22

def menu()
  system 'clear'
  puts "-".colorize(:red) * 45
  puts "Welcome to the Monster Encyclopedia"
  puts "-".colorize(:red) * 45
  input = $prompt.select("What would you like to do?") do |menu|
    menu.choice name: "View all entries", value: 1
    menu.choice name: "Add a new monster to the encyclopedia", value: 2
    menu.choice name: "Remove a monster from the encyclopedia", value: 3
    menu.choice name: "Close encyclopedia", value: 4
  end
  return input
end

#remove_monster(monsters_array) ⇒ Object



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
# File 'lib/remove_monster.rb', line 1

def remove_monster(monsters_array)
  monster = []
  monster_names = []
  csv_text = File.read('data.csv')
  csv = CSV.parse(csv_text, :headers => true)
  csv.each do |row|
    monster << row.to_hash
  end

  system 'clear'

  monster.sort_by! { |mon| mon["name"]}
  monster.each do |thing|
    puts '-'.colorize(:red) * 45
    puts "Name: #{thing["name"]}"
    puts
    monster_names << thing["name"]
  end

  puts "Which monster would you like to remove from the encyclopedia?"
  input = gets.chomp

  if monster_names.include?(input)
    table = CSV.table("data.csv")
    table.delete_if.with_index  do |row, index|
      next if index.zero?
      row[0] == input
    end

    File.open("data.csv", 'w') do |data|
      data.write(table.to_csv)
    end
    system 'clear'
    puts '-'.colorize(:red) * 45
    puts "#{input} was deleted!"
    puts '-'.colorize(:red) * 45
    sleep 2
  else
    system 'clear'
    puts '-'.colorize(:red) * 45
    puts "No monster by that name..."
    puts '-'.colorize(:red) * 45
    sleep 2
  end
end

#view_monstersObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/view_monsters.rb', line 2

def view_monsters()
  monsters_array = []
  csv_text = File.read('data.csv')
  csv = CSV.parse(csv_text, :headers => true)
  csv.each do |row|
    monster = row.to_hash
    monsters_array << monster
    system 'clear'
  end
    monsters_array.sort_by! { |mon| mon["name"]}
    monsters_array.each do |monster|
    puts '-'.colorize(:red) * 45
    puts "Name:" + " #{monster["name"]}".colorize(:light_red)
    puts "Found: #{monster["location"]}"
    puts
    puts "Description: #{monster["description"]}"  
    puts
  end 
  gets
end