From 54f79af8a8d3e4757bada5a21b39c3fd75b9a2e2 Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Mon, 4 Nov 2019 18:29:23 +0300 Subject: [PATCH] Add item collection --- src/dayoff.cr | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/dayoff.cr b/src/dayoff.cr index d82dccc..095886b 100644 --- a/src/dayoff.cr +++ b/src/dayoff.cr @@ -17,20 +17,35 @@ module Dayoff ) 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 PLANNED_DATES = "planned-dates.json" WORK_RECORDS = "work-records.json" - def initialize(@path : String) - content = File.open(File.join(@path, PLANNED_DATES)) do |file| - file.gets_to_end - end - @pdates = Array(PlannedDate).from_json(content) + @pdates = [] of PlannedDate + @wrecords = [] of WorkRecord - content = File.open(File.join(@path, WORK_RECORDS)) do |file| - file.gets_to_end - end - @wrecords = Array(WorkRecord).from_json(content) + def initialize(@path : String) + @pdates = Collection(PlannedDate).new(File.join(@path, PLANNED_DATES)).get_all + @wrecords = Collection(WorkRecord).new(File.join(@path, WORK_RECORDS)).get_all end def get_planned_hours