Created
April 23, 2019 22:16
-
-
Save Martin-Alexander/5e0294744a8f5694eb3d8aef81ac0b68 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "csv" | |
require_relative "router" | |
require_relative "app/models/meal" | |
require_relative "app/controllers/meals_controller" | |
require_relative "app/repositories/meal_repository" | |
require_relative "app/views/meal_view" | |
require_relative "app/models/customer" | |
require_relative "app/controllers/customers_controller" | |
require_relative "app/repositories/customer_repository" | |
require_relative "app/views/customer_view" | |
meals_csv_path = "data/meals.csv" | |
meals_repo = MealRepository.new(meals_csv_path) | |
meals_controller = MealsController.new(meals_repo) | |
customers_csv_path = "data/customers.csv" | |
customers_repo = CustomerRepository.new(customers_csv_path) | |
customers_controller = CustomersController.new(customers_repo) | |
router = Router.new(meals_controller, customers_controller) | |
router.run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Customer | |
attr_reader :name, :address | |
attr_accessor :id | |
def initialize(attributes = {}) | |
@id = attributes[:id] | |
@name = attributes[:name] | |
@address = attributes[:address] | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CustomerRepository | |
def initialize(csv_file_path) | |
@csv_file_path = csv_file_path | |
@customers = [] # An array of customer objects | |
csv_options = { headers: true, header_converters: :symbol } | |
if File.exist?(@csv_file_path) | |
CSV.foreach(@csv_file_path, csv_options) do |row| | |
# row = { id: "1", name: "Paul McCartney", address: "Liverpool" } | |
row[:id] = row[:id].to_i | |
@customers << Customer.new(row) | |
end | |
end | |
end | |
def all | |
@customers | |
end | |
def add(new_customer) | |
# set the id of the new customer | |
if @customers.length == 0 | |
new_id = 1 | |
else | |
new_id = @customers.last.id + 1 | |
end | |
new_customer.id = new_id | |
# push it into the array `@customers` | |
@customers << new_customer | |
# write to the csv | |
CSV.open(@csv_file_path, "w") do |csv_file| | |
csv_file << ["id", "name", "address"] | |
@customers.each do |customer| | |
csv_file << [customer.id, customer.name, customer.address] | |
end | |
end | |
end | |
def find(customer_id) | |
@customers.find do |customer| | |
customer.id == customer_id | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CustomerView | |
def display_customers(customers) | |
customers.each do |customer| | |
puts "#{customer.id} - #{customer.name} #{customer.address}" | |
end | |
end | |
def ask_for(message) | |
puts message | |
print "> " | |
user_input = gets.chomp | |
user_input | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
id | name | address | |
---|---|---|---|
1 | Paul McCartney | Liverpool | |
2 | John Bonham | Redditch | |
3 | John Entwistle | Chiswick | |
4 | Martin | 123 Fake St. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative "../views/customer_view" | |
class CustomersController | |
def initialize(repository) | |
@repository = repository | |
@view = CustomerView.new | |
end | |
def list | |
customers = @repository.all | |
@view.display_customers(customers) | |
end | |
def add | |
customer_name = @view.ask_for("Name:") | |
customer_address = @view.ask_for("Address:") | |
customer_hash = { | |
name: customer_name, | |
address: customer_address | |
} | |
new_customer = Customer.new(customer_hash) | |
@repository.add(new_customer) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Meal | |
attr_reader :name, :price | |
attr_accessor :id | |
def initialize(attributes = {}) | |
@id = attributes[:id] | |
@name = attributes[:name] | |
@price = attributes[:price] | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MealRepository | |
def initialize(csv_file_path) | |
@csv_file_path = csv_file_path | |
@meals = [] # An array of Meal objects | |
csv_options = { headers: true, header_converters: :symbol } | |
if File.exist?(@csv_file_path) | |
CSV.foreach(@csv_file_path, csv_options) do |row| | |
# row = { id: "1", name: "Margherita", price: "8" } | |
row[:id] = row[:id].to_i | |
row[:price] = row[:price].to_i | |
@meals << Meal.new(row) | |
end | |
end | |
end | |
def all | |
@meals | |
end | |
def add(new_meal) | |
# set the id of the new meal | |
if @meals.length == 0 | |
new_id = 1 | |
else | |
new_id = @meals.last.id + 1 | |
end | |
new_meal.id = new_id | |
# push it into the array `@meals` | |
@meals << new_meal | |
# write to the csv | |
CSV.open(@csv_file_path, "w") do |csv_file| | |
csv_file << ["id", "name", "price"] | |
@meals.each do |meal| | |
csv_file << [meal.id, meal.name, meal.price] | |
end | |
end | |
end | |
def find(meal_id) | |
@meals.find do |meal| | |
meal.id == meal_id | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MealView | |
def display_meals(meals) | |
meals.each do |meal| | |
puts "#{meal.id} - $#{meal.price} #{meal.name}" | |
end | |
end | |
def ask_for(message) | |
puts message | |
print "> " | |
user_input = gets.chomp | |
user_input | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
id | name | price | |
---|---|---|---|
1 | Margherita | 8 | |
2 | Capricciosa | 11 | |
3 | Napolitana | 9 | |
4 | Funghi | 12 | |
5 | Calzone | 10 | |
6 | Pizza | 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative "../views/meal_view" | |
class MealsController | |
def initialize(repository) | |
@repository = repository | |
@view = MealView.new | |
end | |
def list | |
meals = @repository.all | |
@view.display_meals(meals) | |
end | |
def add | |
meal_name = @view.ask_for("Name:") | |
meal_price = @view.ask_for("Price:").to_i | |
meal_hash = { | |
name: meal_name, | |
price: meal_price | |
} | |
new_meal = Meal.new(meal_hash) | |
@repository.add(new_meal) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Router | |
def initialize(meals_controller, customers_controller) | |
@meals_controller = meals_controller | |
@customers_controller = customers_controller | |
end | |
def run | |
# start a loop | |
loop do | |
# display a list of actions | |
puts "=== MAIN MENU ===" | |
puts "1 - All Meals" | |
puts "2 - New Meal" | |
puts "3 - All Customers" | |
puts "4 - New Customer" | |
puts "5 - Quit" | |
# ask the user which actions to do | |
puts "Input" | |
print "> " | |
user_choice = gets.chomp.to_i | |
# based on the user's input, call the correct controller action | |
case user_choice | |
when 1 then @meals_controller.list | |
when 2 then @meals_controller.add | |
when 3 then @customers_controller.list | |
when 4 then @customers_controller.add | |
when 5 then break | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment