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
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

View File

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

View File

@ -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

View File

@ -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