Add item collection

This commit is contained in:
Anton Vakhrushev 2019-11-04 18:29:23 +03:00
parent e9fa65a8f7
commit 54f79af8a8

View File

@ -17,20 +17,35 @@ module Dayoff
) )
end end
class Collection(T)
def initialize(@path : String)
end
def get_all : Array(T)
if File.file? @path
content = File.read @path
Array(T).from_json(content)
else
[] of T
end
end
def write(items : Array(T))
content = items.to_json
File.write @path, content
end
end
class Profile class Profile
PLANNED_DATES = "planned-dates.json" PLANNED_DATES = "planned-dates.json"
WORK_RECORDS = "work-records.json" WORK_RECORDS = "work-records.json"
def initialize(@path : String) @pdates = [] of PlannedDate
content = File.open(File.join(@path, PLANNED_DATES)) do |file| @wrecords = [] of WorkRecord
file.gets_to_end
end
@pdates = Array(PlannedDate).from_json(content)
content = File.open(File.join(@path, WORK_RECORDS)) do |file| def initialize(@path : String)
file.gets_to_end @pdates = Collection(PlannedDate).new(File.join(@path, PLANNED_DATES)).get_all
end @wrecords = Collection(WorkRecord).new(File.join(@path, WORK_RECORDS)).get_all
@wrecords = Array(WorkRecord).from_json(content)
end end
def get_planned_hours def get_planned_hours