use Structs instead of long class definitions
This commit is contained in:
parent
9d2ca3fec9
commit
fa705fd813
30
invoice.rb
30
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
|
||||
|
|
Loading…
Reference in New Issue
Block a user