From 41176c7a1fcc9f0bd182771c5f9be501dc574002 Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Mon, 4 Nov 2019 21:20:20 +0300 Subject: [PATCH] Add type conversions to entity --- spec/profile_spec.cr | 14 ++++----- src/dayoff.cr | 2 ++ src/dayoff/entities.cr | 69 +++++++++++++++++------------------------- src/dayoff/profile.cr | 6 ++-- 4 files changed, 40 insertions(+), 51 deletions(-) diff --git a/spec/profile_spec.cr b/spec/profile_spec.cr index 343fddf..4270545 100644 --- a/spec/profile_spec.cr +++ b/spec/profile_spec.cr @@ -61,28 +61,28 @@ module Dayoff::Test prof.finish finish_time records = storage.get_work_records records.size.should eq 2 - records.last.finish_time.should eq finish_time + records.last.finish.should eq finish_time end it "can calc planned hours" do prof = create_profile - s = prof.get_planned_hours t(3, 12) + span = prof.get_planned_hours t(3, 12) expected = 8 * 3 - expected.should eq s.total_hours + expected.should eq span.total_hours end it "can calc work hours" do prof = create_profile - s = prof.get_work_hours t(3, 12) + span = prof.get_work_hours t(3, 12) expected = 10 * 2 - expected.should eq s.total_hours + expected.should eq span.total_hours end it "can calc remaining time" do prof = create_profile - s = prof.remaining_time t(3, 12) + span = prof.remaining_time t(3, 12) expected = 8 * 3 - 10 * 2 - expected.should eq s.total_hours + expected.should eq span.total_hours end end end diff --git a/src/dayoff.cr b/src/dayoff.cr index 71c63e1..d617624 100644 --- a/src/dayoff.cr +++ b/src/dayoff.cr @@ -1,3 +1,5 @@ +require "./dayoff/**" + module Dayoff VERSION = "0.1.0" diff --git a/src/dayoff/entities.cr b/src/dayoff/entities.cr index 6cddd54..b017413 100644 --- a/src/dayoff/entities.cr +++ b/src/dayoff/entities.cr @@ -2,21 +2,17 @@ require "json" module Dayoff class PlannedDate + FORMAT = "%Y-%m-%d" + JSON.mapping( - date: String, + date: { + type: Time, + converter: Time::Format.new(FORMAT, Time::Location.load("Europe/Moscow")), + }, hours: Int32, ) - FORMAT = "%Y-%m-%d" - - def initialize(date : Time, hours : Int32) - @date = date.to_s FORMAT - @hours = hours - end - - def date_time : Time - location = Time::Location.load("Europe/Moscow") - Time.parse(@date, FORMAT, location) + def initialize(@date : Time, @hours : Int32) end def time_span : Time::Span @@ -25,33 +21,24 @@ module Dayoff end class WorkRecord - JSON.mapping( - start: String, - finish: String | Nil, - ) - FORMAT = "%Y-%m-%d %H:%M:%S" - def initialize(start : Time, finish : Time | Nil = nil) - @start = start.to_s FORMAT - @finish = nil - if finish - @finish = finish.to_s FORMAT - end + JSON.mapping( + start: { + type: Time, + converter: Time::Format.new(FORMAT, Time::Location.load("Europe/Moscow")), + }, + finish: { + type: Time | Nil, + converter: Time::Format.new(FORMAT, Time::Location.load("Europe/Moscow")), + } + ) + + def initialize(@start : Time, @finish : Time | Nil = nil) end - def start_time : Time - location = Time::Location.load("Europe/Moscow") - Time.parse(@start, FORMAT, location) - end - - def finish_time : Time - location = Time::Location.load("Europe/Moscow") - Time.parse(@finish.as(String), FORMAT, location) - end - - def finish_time=(v : Time) - @finish = v.to_s FORMAT + def finish! : Time + @finish.as(Time) end def started? : Bool @@ -63,21 +50,21 @@ module Dayoff end def finished_to_time?(time : Time) : Bool - @finish && finish_time <= time + @finish && @finish <= time end def calc_span(time : Time) : Time::Span if @finish.nil? - if start_time <= time - time - start_time + if @start <= time + time - @start else Time::Span.zero end else - if time > finish_time - finish_time - start_time - elsif time > start_time - time - start_time + if time > finish! + finish! - @start + elsif time > @start + time - @start else Time::Span.zero end diff --git a/src/dayoff/profile.cr b/src/dayoff/profile.cr index be8339c..b2d8ebe 100644 --- a/src/dayoff/profile.cr +++ b/src/dayoff/profile.cr @@ -22,7 +22,7 @@ module Dayoff def get_planned_hours(on_time : Time) : Time::Span check_date = on_time.at_beginning_of_day @pdates.reduce(Time::Span.zero) do |acc, wd| - if wd.date_time <= check_date + if wd.date <= check_date acc + wd.time_span else acc @@ -38,7 +38,7 @@ module Dayoff def start(time : Time) : Nil @wrecords.each do |wr| - if time <= wr.start_time || time <= wr.finish_time + if time <= wr.start || time <= wr.finish! raise CrossedTimeSpan.new end end @@ -52,7 +52,7 @@ module Dayoff if started.nil? raise StartedRecordNotFound.new end - started.finish_time = time + started.finish = time @storage.set_work_records @wrecords end