Created
July 27, 2021 21:47
-
-
Save arthur-littm/e0cb5ce7c67b84a730b7686e1ceb3147 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_relative "app/repositories/meal_repository" | |
require_relative "app/controllers/meals_controller" | |
require_relative "app/repositories/customer_repository" | |
require_relative "app/controllers/customers_controller" | |
require_relative "router" | |
MEALS_CSV_FILE = File.join(__dir__, "data/meals.csv") | |
CUSTOMERS_CSV_FILE = File.join(__dir__, "data/customers.csv") | |
meal_repository = MealRepository.new(MEALS_CSV_FILE) | |
meals_controller = MealsController.new(meal_repository) | |
customer_repository = CustomerRepository.new(CUSTOMERS_CSV_FILE) | |
customers_controller = CustomersController.new(customer_repository) | |
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_accessor :id | |
attr_reader :name, :address | |
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
require "csv" | |
require_relative "../models/customer" | |
class CustomerRepository | |
def initialize(csv_file) | |
@csv_file = csv_file | |
@customers = [] | |
@next_id = 1 | |
load_csv if File.exist?(@csv_file) | |
end | |
def create(customer) | |
customer.id = @next_id | |
@customers << customer | |
@next_id += 1 | |
save_csv | |
end | |
def all | |
@customers | |
end | |
def find(id) | |
@customers.find { |customer| customer.id == id } | |
end | |
private | |
def save_csv | |
CSV.open(@csv_file, "wb") do |csv| | |
csv << %w[id name address] | |
@customers.each do |customer| | |
csv << [customer.id, customer.name, customer.address] | |
end | |
end | |
end | |
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 | |
@customers << Customer.new(row) | |
end | |
@next_id = @customers.last.id + 1 unless @customers.empty? | |
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
require_relative "../views/customers_view" | |
require_relative "../models/customer" | |
class CustomersController | |
def initialize(customer_repository) | |
@customer_repository = customer_repository | |
@customers_view = CustomersView.new | |
end | |
def add | |
name = @customers_view.ask_user_for(:name) | |
address = @customers_view.ask_user_for(:address) | |
customer = Customer.new(name: name, address: address) | |
@customer_repository.create(customer) | |
display_customers | |
end | |
def list | |
display_customers | |
end | |
private | |
def display_customers | |
customers = @customer_repository.all | |
@customers_view.display(customers) | |
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 CustomersView | |
def display(customers) | |
customers.each_with_index do |customer, index| | |
puts "#{index + 1}. #{customer.name} : #{customer.address}" | |
end | |
end | |
def ask_user_for(stuff) | |
puts "#{stuff.capitalize}?" | |
print "> " | |
return gets.chomp | |
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_accessor :id | |
attr_reader :name, :price | |
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
require "csv" | |
require_relative "../models/meal" | |
class MealRepository | |
def initialize(csv_file) | |
@csv_file = csv_file | |
@meals = [] | |
@next_id = 1 | |
load_csv if File.exist?(@csv_file) | |
end | |
def create(meal) | |
meal.id = @next_id | |
@meals << meal | |
@next_id += 1 | |
save_csv | |
end | |
def all | |
@meals | |
end | |
def find(id) | |
@meals.find { |meal| meal.id == id } | |
end | |
private | |
def save_csv | |
CSV.open(@csv_file, "wb") do |csv| | |
csv << %w[id name price] | |
@meals.each do |meal| | |
csv << [meal.id, meal.name, meal.price] | |
end | |
end | |
end | |
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 | |
@next_id = @meals.last.id + 1 unless @meals.empty? | |
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
require_relative "../views/meals_view" | |
require_relative "../models/meal" | |
class MealsController | |
def initialize(meal_repository) | |
@meal_repository = meal_repository | |
@meals_view = MealsView.new | |
end | |
def add | |
name = @meals_view.ask_user_for(:name) | |
price = @meals_view.ask_user_for(:price).to_i | |
meal = Meal.new(name: name, price: price) | |
@meal_repository.create(meal) | |
display_meals | |
end | |
def list | |
display_meals | |
end | |
private | |
def display_meals | |
meals = @meal_repository.all | |
@meals_view.display(meals) | |
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 MealsView | |
def display(meals) | |
meals.each_with_index do |meal, index| | |
puts "#{index + 1}. #{meal.name} : #{meal.price}€" | |
end | |
end | |
def ask_user_for(stuff) | |
puts "#{stuff.capitalize}?" | |
print "> " | |
return gets.chomp | |
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 | |
@running = true | |
end | |
def run | |
while @running | |
print_menu | |
choice = gets.chomp.to_i | |
print `clear` | |
route_action(choice) | |
end | |
end | |
private | |
def print_menu | |
puts "--------------------" | |
puts "------- MENU -------" | |
puts "--------------------" | |
puts "1. Add new meal" | |
puts "2. List all meals" | |
puts "3. Add new customer" | |
puts "4. List all customers" | |
puts "8. Exit" | |
print "> " | |
end | |
def route_action(choice) | |
case choice | |
when 1 then @meals_controller.add | |
when 2 then @meals_controller.list | |
when 3 then @customers_controller.add | |
when 4 then @customers_controller.list | |
when 8 then stop! | |
else puts "Try again..." | |
end | |
end | |
def stop! | |
@running = false | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment