From e9fa65a8f79308136489435eb3ae956069178703 Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Sun, 3 Nov 2019 19:45:48 +0300 Subject: [PATCH] Add profile class --- shard.yml | 5 +++++ spec/dayoff_spec.cr | 16 ++++++---------- src/dayoff.cr | 19 ++++++++++++++----- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/shard.yml b/shard.yml index f38e6a6..a5c876e 100644 --- a/shard.yml +++ b/shard.yml @@ -11,3 +11,8 @@ targets: crystal: 0.31.1 license: MIT + +development_dependencies: + ameba: + github: crystal-ameba/ameba + version: ~> 0.10.1 diff --git a/spec/dayoff_spec.cr b/spec/dayoff_spec.cr index b389254..4e61eaf 100644 --- a/spec/dayoff_spec.cr +++ b/spec/dayoff_spec.cr @@ -2,18 +2,14 @@ require "./spec_helper" describe Dayoff do it "can calc work hours" do - app = Dayoff::App.new( - "./spec/data/planned-dates.json", - "./spec/data/work-records.json", - ) - app.get_planned_hours.should eq 20 + app = Dayoff::App.new + prof = app.profile("./spec/data") + prof.get_planned_hours.should eq 20 end it "can calc work hours" do - app = Dayoff::App.new( - "./spec/data/planned-dates.json", - "./spec/data/work-records.json", - ) - app.get_work_hours.should eq 10 + app = Dayoff::App.new + prof = app.profile("./spec/data") + prof.get_work_hours.should eq 10 end end diff --git a/src/dayoff.cr b/src/dayoff.cr index 5505bbe..d82dccc 100644 --- a/src/dayoff.cr +++ b/src/dayoff.cr @@ -17,14 +17,17 @@ module Dayoff ) end - class App - def initialize(pdates_path, wdates_path) - content = File.open(pdates_path) do |file| + 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) - content = File.open(wdates_path) do |file| + content = File.open(File.join(@path, WORK_RECORDS)) do |file| file.gets_to_end end @wrecords = Array(WorkRecord).from_json(content) @@ -47,7 +50,13 @@ module Dayoff diff = f - s sum += diff.total_hours.to_i32 end - return sum + sum + end + end + + class App + def profile(path) + Profile.new(path) end end end