From 991396f68c15702791d1d3b61b8b7f7d723c50a7 Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Sun, 3 Nov 2019 11:57:38 +0300 Subject: [PATCH] Calc work hours --- spec/data/work-dates.json | 14 ++++++++++++++ spec/dayoff_spec.cr | 7 +++---- src/dayoff.cr | 27 +++++++++++++++++++++++++-- 3 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 spec/data/work-dates.json diff --git a/spec/data/work-dates.json b/spec/data/work-dates.json new file mode 100644 index 0000000..0eecd84 --- /dev/null +++ b/spec/data/work-dates.json @@ -0,0 +1,14 @@ +[ + { + "date": "2019-01-01", + "hours": 10 + }, + { + "date": "2019-01-02", + "hours": 6 + }, + { + "date": "2019-01-03", + "hours": 4 + } +] \ No newline at end of file diff --git a/spec/dayoff_spec.cr b/spec/dayoff_spec.cr index 5cfe5ef..a35f282 100644 --- a/spec/dayoff_spec.cr +++ b/spec/dayoff_spec.cr @@ -1,9 +1,8 @@ require "./spec_helper" describe Dayoff do - # TODO: Write tests - - it "works" do - false.should eq(true) + it "can calc work hours" do + app = Dayoff::App.new("./spec/data/work-dates.json") + app.get_work_hours.should eq 20 end end diff --git a/src/dayoff.cr b/src/dayoff.cr index 589b05f..8295190 100644 --- a/src/dayoff.cr +++ b/src/dayoff.cr @@ -1,6 +1,29 @@ -# TODO: Write documentation for `Dayoff` +require "json" + module Dayoff VERSION = "0.1.0" - # TODO: Put your code here + class WorkDate + JSON.mapping( + date: String, + hours: Int32, + ) + end + + class App + def initialize(wh_path) + content = File.open(wh_path) do |file| + file.gets_to_end + end + @wdates = Array(WorkDate).from_json(content) + end + + def get_work_hours + sum = 0 + @wdates.each do |wd| + sum += wd.hours + end + sum + end + end end