Class: MealSelector::Frontend

Inherits:
Object
  • Object
show all
Defined in:
lib/meal_selector/frontend.rb

Overview

User interface for MealSelector actions

Author:

  • Anthony Tilelli

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFrontend

Returns a new instance of Frontend



9
10
11
# File 'lib/meal_selector/frontend.rb', line 9

def initialize
  @last_meal = nil
end

Instance Attribute Details

#last_mealObject

Returns the value of attribute last_meal



7
8
9
# File 'lib/meal_selector/frontend.rb', line 7

def last_meal
  @last_meal
end

Class Method Details

.clearvoid

This method returns an undefined value.

Marks old console output and clears screen



15
16
17
18
# File 'lib/meal_selector/frontend.rb', line 15

def self.clear
  puts '==== old console Output above ===='
  puts `clear`
end

.user_input(list_size, *chars) ⇒ String.downcase

prompts user input and check if allowed input

Parameters:

  • list_size (Integer)

    must be positive number or Zero, set to `0` to not accept numbers

  • chars (Array<String>)

    list of allowed characters, enter `''` to allow just enter without char or number

Returns:

  • (String.downcase)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/meal_selector/frontend.rb', line 26

def self.user_input(list_size, *chars)
  # Number input starts with 1.
  raise 'list_size must be an integer' unless list_size.is_a?(Integer)
  raise 'list_size cannot be negative' if list_size.negative?

  input = nil
  while input.nil?
    print '$: '
    input = gets&.chomp&.downcase
    # Allowed char
    return input if chars.include?(input)
    # Num in range
    return input if input.to_i.between?(1, list_size)

    # incorrect input
    input = nil
    puts 'Invalid Input, please try again'
  end
end

Instance Method Details

#meals_by_categories(backend) ⇒ void

This method returns an undefined value.

List categories user can choose from.

Parameters:



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/meal_selector/frontend.rb', line 138

def meals_by_categories(backend)
  repeat = true
  while repeat
    Frontend.clear
    puts 'Select a meal category:'
    backend.categories.each_index do |index|
      puts "=> `#{index + 1}` #{backend.categories[index]}"
    end
    puts 'Press `m` Return to menu'
    input = Frontend.user_input(backend.categories.size, 'm')
    return if input == 'm'

    category = backend.categories[input.to_i - 1]
    repeat = show_meal_list(backend.find_meals_by_categories(category), false, backend)
  end
end

#meals_by_main_ingrediant(backend) ⇒ void

This method returns an undefined value.

Search for meal by main ingrediant.

Parameters:



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/meal_selector/frontend.rb', line 158

def meals_by_main_ingrediant(backend)
  repeat = true
  while repeat
    Frontend.clear
    puts 'Enter an ingrediant to search by:'
    puts 'Enter `m` to return to menu'
    print '$: '
    input = gets.chomp.downcase
    next if input == ''
    return if input == 'm'

    results = backend.find_meal_by_ingredient(input)
    if results == {}
      puts 'Cannot find any meals for that ingredient, try Again'
      sleep 0.75
    else
      repeat = show_meal_list(results, false, backend)
    end
  end
end

#search_meal_by_name(backend) ⇒ void

This method returns an undefined value.

Search for meal by name

Parameters:



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/meal_selector/frontend.rb', line 114

def search_meal_by_name(backend)
  repeat = true
  while repeat
    Frontend.clear
    puts 'Enter a meal name to search:'
    puts 'Press `m` to return to menu'
    print '$: '
    input = gets.chomp.downcase
    next if input == ''
    return if input == 'm'

    results = backend.find_meals_by_name(input)
    if results == {}
      puts 'Cannot find any meals by that name, try Again'
      sleep 0.75
    else
      repeat = show_meal_list(results, false, backend)
    end
  end
end

#show_meal(meal, menu_only, backend) ⇒ Boolean

Shows a whole meal

Parameters:

  • meal (Meal)

    one Meal

  • menu_only (Boolean)

    Determines if the back option should be allowed (`back` option allowed on false)

  • backend (Backend)

Returns:

  • (Boolean)

    previous section should repeat

Raises:

  • when meal is not a whole_meal



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/meal_selector/frontend.rb', line 53

def show_meal(meal, menu_only, backend)
  raise "meal is not a MealSelector::Meal, instead #{meal.class}" unless meal.is_a?(Meal)
  raise 'meal is not whole' unless meal.whole_meal?
  raise 'backend is not a Backend' unless backend.is_a?(Backend)

  Frontend.clear
  @last_meal = meal
  puts "Name: #{meal.name.capitalize}"
  puts "Category: #{meal.category.capitalize}"
  puts "Type: #{meal.type.capitalize}"
  puts 'Ingredient:'
  meal.ingredient.each do |item, amount|
    puts "=> #{item}: #{amount}"
  end
  puts 'Instructions:'
  puts meal.instructions
  puts ''
  show_meal_actions(backend, meal, menu_only)
end

#show_meal_list(meals, menu_only, backend) ⇒ Boolean

List meals provided

Parameters:

  • meals (Hash)

    One or more meals

  • menu_only (Boolean)

    Determines if the back option should be allowed (`back` option allowed on false)

  • backend (Backend)

Returns:

  • (Boolean)

    previous section should repeat



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/meal_selector/frontend.rb', line 79

def show_meal_list(meals, menu_only, backend)
  raise 'Meals is not a hash of meals' unless meals.is_a?(Hash)
  raise 'meals is empty' if meals.empty?
  raise 'backend is not a backend' unless backend.is_a?(Backend)

  repeating = true
  while repeating
    allowed_input = ['m']
    count = 0
    round = {} # record the keys per round {round => id }
    Frontend.clear
    puts 'Select a meal below:'
    # Output meal name
    meals.each do |key, meal|
      count += 1
      round[count.to_s] = key
      puts "`#{count}` #{meal.name}"
    end
    puts 'Press `m` to go to menu'
    unless menu_only
      puts 'Press `b` to go back'
      allowed_input << 'b'
    end
    input = Frontend.user_input(round.size, *allowed_input)
    return true if input == 'b'
    return false if input == 'm'

    meal = backend.resolve_meal(meals[round[input]])
    repeating = show_meal(meal, false, backend)
  end
end