From fa705fd813ad66c2f949d0bd8085d18ab880d120 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoffer=20M=C3=BCller=20Madsen?= Date: Wed, 1 Feb 2017 12:43:11 +0100 Subject: [PATCH] use Structs instead of long class definitions --- invoice.rb | 30 +++++------------------------- 1 file changed, 5 insertions(+), 25 deletions(-) diff --git a/invoice.rb b/invoice.rb index 5fd4947..345d5ab 100644 --- a/invoice.rb +++ b/invoice.rb @@ -21,35 +21,17 @@ OptionParser.new do |opts| end end.parse! -class Transaction - def initialize(time, person, product, amount: 1) - @time = time - @person = person - @product = product - @amount = amount - end - - attr_accessor :time, :person, :product, :amount -end - -class Product - def initialize(id, description, price) - @id = id - @description = description - @price = price - end - +Transaction = Struct.new(:time, :person, :product, :amount) +Product = Struct.new(:id, :description, :price) do def to_s - "#{@id} => #{@description}, #{@price}" + "#{id} => #{description}, #{price}" end - - attr_accessor :id, :description, :price end def read_file(file) rows = Array.new CSV.foreach(file, col_sep: ';', converters: :float) do |row| - rows << Transaction.new(row[0],row[1],row[2],amount: row[3]) + rows << Transaction.new(row[0],row[1],row[2],row[3]) end rows end @@ -59,7 +41,7 @@ def read_db(db) db.query("SELECT * FROM Transactions").each do |trans| if date_of_prev('monday').to_time < trans["time"] transactions << Transaction.new(trans["time"], trans["buyer"], - trans["product"], amount: trans["amount"]) + trans["product"], trans["amount"]) end end return transactions @@ -128,8 +110,6 @@ def generate_receipt(persons,draft) end end -# Utility - def date_of_next(day) date = Date.parse(day) delta = date > Date.today ? 0 : 7