Add type conversions to entity

This commit is contained in:
Anton Vakhrushev 2019-11-04 21:20:20 +03:00
parent 18b2bd5b01
commit 41176c7a1f
4 changed files with 40 additions and 51 deletions

View File

@ -61,28 +61,28 @@ module Dayoff::Test
prof.finish finish_time prof.finish finish_time
records = storage.get_work_records records = storage.get_work_records
records.size.should eq 2 records.size.should eq 2
records.last.finish_time.should eq finish_time records.last.finish.should eq finish_time
end end
it "can calc planned hours" do it "can calc planned hours" do
prof = create_profile prof = create_profile
s = prof.get_planned_hours t(3, 12) span = prof.get_planned_hours t(3, 12)
expected = 8 * 3 expected = 8 * 3
expected.should eq s.total_hours expected.should eq span.total_hours
end end
it "can calc work hours" do it "can calc work hours" do
prof = create_profile prof = create_profile
s = prof.get_work_hours t(3, 12) span = prof.get_work_hours t(3, 12)
expected = 10 * 2 expected = 10 * 2
expected.should eq s.total_hours expected.should eq span.total_hours
end end
it "can calc remaining time" do it "can calc remaining time" do
prof = create_profile prof = create_profile
s = prof.remaining_time t(3, 12) span = prof.remaining_time t(3, 12)
expected = 8 * 3 - 10 * 2 expected = 8 * 3 - 10 * 2
expected.should eq s.total_hours expected.should eq span.total_hours
end end
end end
end end

View File

@ -1,3 +1,5 @@
require "./dayoff/**"
module Dayoff module Dayoff
VERSION = "0.1.0" VERSION = "0.1.0"

View File

@ -2,21 +2,17 @@ require "json"
module Dayoff module Dayoff
class PlannedDate class PlannedDate
FORMAT = "%Y-%m-%d"
JSON.mapping( JSON.mapping(
date: String, date: {
type: Time,
converter: Time::Format.new(FORMAT, Time::Location.load("Europe/Moscow")),
},
hours: Int32, hours: Int32,
) )
FORMAT = "%Y-%m-%d" def initialize(@date : Time, @hours : Int32)
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)
end end
def time_span : Time::Span def time_span : Time::Span
@ -25,33 +21,24 @@ module Dayoff
end end
class WorkRecord class WorkRecord
JSON.mapping(
start: String,
finish: String | Nil,
)
FORMAT = "%Y-%m-%d %H:%M:%S" FORMAT = "%Y-%m-%d %H:%M:%S"
def initialize(start : Time, finish : Time | Nil = nil) JSON.mapping(
@start = start.to_s FORMAT start: {
@finish = nil type: Time,
if finish converter: Time::Format.new(FORMAT, Time::Location.load("Europe/Moscow")),
@finish = finish.to_s FORMAT },
end finish: {
type: Time | Nil,
converter: Time::Format.new(FORMAT, Time::Location.load("Europe/Moscow")),
}
)
def initialize(@start : Time, @finish : Time | Nil = nil)
end end
def start_time : Time def finish! : Time
location = Time::Location.load("Europe/Moscow") @finish.as(Time)
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
end end
def started? : Bool def started? : Bool
@ -63,21 +50,21 @@ module Dayoff
end end
def finished_to_time?(time : Time) : Bool def finished_to_time?(time : Time) : Bool
@finish && finish_time <= time @finish && @finish <= time
end end
def calc_span(time : Time) : Time::Span def calc_span(time : Time) : Time::Span
if @finish.nil? if @finish.nil?
if start_time <= time if @start <= time
time - start_time time - @start
else else
Time::Span.zero Time::Span.zero
end end
else else
if time > finish_time if time > finish!
finish_time - start_time finish! - @start
elsif time > start_time elsif time > @start
time - start_time time - @start
else else
Time::Span.zero Time::Span.zero
end end

View File

@ -22,7 +22,7 @@ module Dayoff
def get_planned_hours(on_time : Time) : Time::Span def get_planned_hours(on_time : Time) : Time::Span
check_date = on_time.at_beginning_of_day check_date = on_time.at_beginning_of_day
@pdates.reduce(Time::Span.zero) do |acc, wd| @pdates.reduce(Time::Span.zero) do |acc, wd|
if wd.date_time <= check_date if wd.date <= check_date
acc + wd.time_span acc + wd.time_span
else else
acc acc
@ -38,7 +38,7 @@ module Dayoff
def start(time : Time) : Nil def start(time : Time) : Nil
@wrecords.each do |wr| @wrecords.each do |wr|
if time <= wr.start_time || time <= wr.finish_time if time <= wr.start || time <= wr.finish!
raise CrossedTimeSpan.new raise CrossedTimeSpan.new
end end
end end
@ -52,7 +52,7 @@ module Dayoff
if started.nil? if started.nil?
raise StartedRecordNotFound.new raise StartedRecordNotFound.new
end end
started.finish_time = time started.finish = time
@storage.set_work_records @wrecords @storage.set_work_records @wrecords
end end