use Structs instead of long class definitions

This commit is contained in:
Christoffer Müller Madsen 2017-02-01 12:43:11 +01:00
parent 9d2ca3fec9
commit fa705fd813

View File

@ -21,35 +21,17 @@ OptionParser.new do |opts|
end end
end.parse! end.parse!
class Transaction Transaction = Struct.new(:time, :person, :product, :amount)
def initialize(time, person, product, amount: 1) Product = Struct.new(:id, :description, :price) do
@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
def to_s def to_s
"#{@id} => #{@description}, #{@price}" "#{id} => #{description}, #{price}"
end end
attr_accessor :id, :description, :price
end end
def read_file(file) def read_file(file)
rows = Array.new rows = Array.new
CSV.foreach(file, col_sep: ';', converters: :float) do |row| 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 end
rows rows
end end
@ -59,7 +41,7 @@ def read_db(db)
db.query("SELECT * FROM Transactions").each do |trans| db.query("SELECT * FROM Transactions").each do |trans|
if date_of_prev('monday').to_time < trans["time"] if date_of_prev('monday').to_time < trans["time"]
transactions << Transaction.new(trans["time"], trans["buyer"], transactions << Transaction.new(trans["time"], trans["buyer"],
trans["product"], amount: trans["amount"]) trans["product"], trans["amount"])
end end
end end
return transactions return transactions
@ -128,8 +110,6 @@ def generate_receipt(persons,draft)
end end
end end
# Utility
def date_of_next(day) def date_of_next(day)
date = Date.parse(day) date = Date.parse(day)
delta = date > Date.today ? 0 : 7 delta = date > Date.today ? 0 : 7