Class: PlantToxicity::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/plant_toxicity/cli.rb

Instance Method Summary collapse

Instance Method Details

#callObject



3
4
5
6
7
8
9
10
11
# File 'lib/plant_toxicity/cli.rb', line 3

def call
  puts "Welcome to the Plant Toxicity to Pets tool!"
  puts "-------------------------------------------"
  puts "It can tell you whether various common plants are toxic to dogs, cats, and horses."
  list_plants_by_letter
  puts "-------------------------------------------"
  puts "Thank you for using the tool! Goodbye."
  exit
end

#display_list(list) ⇒ Object



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
# File 'lib/plant_toxicity/cli.rb', line 34

def display_list(list)
  if !list.plants.empty?
    puts "These are the plants that begin with the letter '#{list.letter}':"
    list.plants.each.with_index(1) do |plant, i|
      puts "#{i}. #{plant.name}"
    end
    puts "-------------------------------------------"
    puts "Would you like to see more details about a plant in this list? (Y/N)"
    input = nil
    until input == "n"
      input = gets.strip.downcase
      if input == "y"
        show_details(list)
        puts "Would you like more information about another plant in this list? (Y/N)"
      elsif input == "n"
        break
      else
        puts "Sorry, that is not a valid option. Please enter Y or N."
      end
    end
  else
    puts "-------------------------------------------"
    puts "There are no plants that begin with the letter '#{list.letter}'."
    puts "-------------------------------------------"
  end
end

#display_plant(plant) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/plant_toxicity/cli.rb', line 72

def display_plant(plant)
  header = "Details for #{plant.name}:"
  puts "-" * header.length
  puts header
  puts "-" * header.length
  plant.details.each do |detail|
    puts "Other Names:  #{plant.other_names}" if detail == :@other_names
    puts "Scientific Name:  #{plant.scientific_name}" if detail == :@scientific_name
    puts "Toxicity:  #{plant.toxicity}" if detail == :@toxicity
    puts "Non-Toxicity:  #{plant.non_toxicity}" if detail == :@non_toxicity
    puts "Clinical Signs:  #{plant.clinical_signs}" if detail == :@clinical_signs
    puts "" if detail != plant.details.last
  end
  puts "-" * header.length
end

#list_plants_by_letterObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/plant_toxicity/cli.rb', line 13

def list_plants_by_letter
  puts "The plants are listed alphabetically by letter, according to the first letter in each plant's name."
  puts "To see a list of plants, enter a letter from A to Z. Enter 'exit' to end the session."
  input = nil
  until input == "exit"
    input = gets.strip.downcase
    if input.match(/\b[a-z]\b/)
      list = PlantToxicity::List.all.detect {|list| list.letter == input.upcase}
      if !list
        list = PlantToxicity::List.new(input.upcase)
      end
      display_list(list)
      puts "Enter a letter from A to Z to see another list of plants, or enter 'exit' to end the session."
    elsif input == "exit"
      break
    else
      puts "Sorry, that is not a valid option. Enter a letter or 'exit'."
    end
  end
end

#show_details(list) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/plant_toxicity/cli.rb', line 61

def show_details(list)
  puts "Please enter the plant's number. (1-#{list.plants.length})"
  input = gets.strip.to_i
  until input >= 1 && input <= list.plants.length
    puts "Sorry, that is not a valid option. Please enter a number."
    input = gets.strip.to_i
  end
  plant = list.select_plant(input-1)
  display_plant(plant)
end