Skip to content

Instantly share code, notes, and snippets.

@jonny-gates
Created October 22, 2019 17:38
Show Gist options
  • Save jonny-gates/9478196c271a9055d154266eba4f1d4d to your computer and use it in GitHub Desktop.
Save jonny-gates/9478196c271a9055d154266eba4f1d4d to your computer and use it in GitHub Desktop.
require_relative 'app/repositories/meal_repository'
require_relative 'app/controllers/meals_controller'
require_relative 'router'
csv_file = 'data/meals.csv'
meal_repository = MealRepository.new(csv_file)
meals_controller = MealsController.new(meal_repository)
router = Router.new(meals_controller)
router.run
class Meal
attr_reader :name, :price
attr_accessor :id
def initialize(attributes = {})
@id = attributes[:id]
@name = attributes[:name]
@price = attributes[:price]
end
end
require 'csv'
require_relative '../models/meal'
class MealRepository
def initialize(csv_file)
@csv_file = csv_file
@meals = []
@next_id = 1
# only run load csv if the file exists
load_csv if File.exist?(@csv_file)
end
def all
return @meals
end
def add(meal)
# attr_accessor gives us meal.id & meal.id=
meal.id = @next_id
@next_id += 1
@meals << meal
save_csv
end
def find(id)
@meals.find { |meal| meal.id == id }
end
private
def load_csv
csv_options = { headers: :first_row, header_converters: :symbol }
CSV.foreach(@csv_file, csv_options) do |row|
row[:id] = row[:id].to_i
row[:price] = row[:price].to_i
@meals << Meal.new(row)
end
# If there are no meals - id is 1
@next_id = @meals.empty? ? 1 : @meals.last.id + 1
end
def save_csv
CSV.open(@csv_file, 'wb') do |csv|
csv << ['id', 'name', 'price']
@meals.each do |meal|
csv << [meal.id, meal.name, meal.price]
end
end
end
end
require_relative '../views/meals_view'
class MealsController
def initialize(meal_repository)
@meal_repository = meal_repository
@meals_view = MealsView.new
end
def list
# Gets the meals from the repo
meals = @meal_repository.all
# Ask the view to print the meals
@meals_view.display_meals(meals)
end
def add
# Ask the user for a name
name = @meals_view.ask_user_for('name')
# Ask the user for a price
price = @meals_view.ask_user_for('price').to_i
# Create a meal
meal = Meal.new(name: name, price: price)
# Add it to the repo
@meal_repository.add(meal)
end
end
class MealsView
def display_meals(meals)
meals.each_with_index do |meal, index|
puts "#{index + 1} #{meal.name}, $#{meal.price}"
end
end
def ask_user_for(attribute)
puts "Please enter your #{attribute}"
return gets.chomp
end
end
class Router
def initialize(meals_controller)
@meals_controller = meals_controller
@running = true
end
def run
while @running do
list_options
choice = gets.chomp.to_i
dispatch(choice)
end
end
private
def stop
@running = false
end
def list_options
puts "What would you like to do?"
puts "1. Keep going"
puts "2. List meals"
puts "3. Add meal"
puts "9. Stop"
end
def dispatch(choice)
case choice
when 1 then puts 'hi'
when 2 then @meals_controller.list
when 3 then @meals_controller.add
when 9 then stop
else puts 'Wrong choice'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment