Add file storage
This commit is contained in:
parent
41176c7a1f
commit
73ed1eca06
@ -3,16 +3,6 @@ require "./spec_helper"
|
|||||||
module Dayoff::Test
|
module Dayoff::Test
|
||||||
extend self
|
extend self
|
||||||
|
|
||||||
def d(day)
|
|
||||||
location = Time::Location.load("Europe/Moscow")
|
|
||||||
Time.local(2019, 1, day, location: location)
|
|
||||||
end
|
|
||||||
|
|
||||||
def t(day, hour, min = 0)
|
|
||||||
location = Time::Location.load("Europe/Moscow")
|
|
||||||
Time.local(2019, 1, day, hour, min, location: location)
|
|
||||||
end
|
|
||||||
|
|
||||||
def create_profile
|
def create_profile
|
||||||
storage = MemoryStorage.new
|
storage = MemoryStorage.new
|
||||||
storage.set_planned_dates [
|
storage.set_planned_dates [
|
||||||
|
@ -1,2 +1,25 @@
|
|||||||
require "spec"
|
require "spec"
|
||||||
require "../src/dayoff/**"
|
require "../src/dayoff/**"
|
||||||
|
|
||||||
|
def d(day)
|
||||||
|
location = Time::Location.load("Europe/Moscow")
|
||||||
|
Time.local(2019, 1, day, location: location)
|
||||||
|
end
|
||||||
|
|
||||||
|
def t(day, hour, min = 0)
|
||||||
|
location = Time::Location.load("Europe/Moscow")
|
||||||
|
Time.local(2019, 1, day, hour, min, location: location)
|
||||||
|
end
|
||||||
|
|
||||||
|
def with_temp_dir(&block)
|
||||||
|
tmpdir = File.tempname("dayoff_file_storage")
|
||||||
|
Dir.mkdir tmpdir
|
||||||
|
begin
|
||||||
|
yield tmpdir
|
||||||
|
ensure
|
||||||
|
Dir.glob(tmpdir + "/*") do |f|
|
||||||
|
File.delete f
|
||||||
|
end
|
||||||
|
Dir.rmdir tmpdir
|
||||||
|
end
|
||||||
|
end
|
||||||
|
17
spec/storage_spec.cr
Normal file
17
spec/storage_spec.cr
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
require "./spec_helper"
|
||||||
|
|
||||||
|
module Dayoff::Test
|
||||||
|
describe Storage do
|
||||||
|
it "can store data in file" do
|
||||||
|
with_temp_dir do |tmpdir|
|
||||||
|
storage = FileStorage.new tmpdir
|
||||||
|
storage.set_planned_dates [
|
||||||
|
PlannedDate.new(d(1), 8),
|
||||||
|
PlannedDate.new(d(1), 8),
|
||||||
|
]
|
||||||
|
entries = Dir.glob(tmpdir + "/*")
|
||||||
|
1.should eq entries.size
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -2,30 +2,57 @@ require "json"
|
|||||||
|
|
||||||
module Dayoff
|
module Dayoff
|
||||||
abstract class Storage
|
abstract class Storage
|
||||||
abstract def get_planned_dates : Array(PlannedDate)
|
macro st_abstract_def(name, dtype)
|
||||||
abstract def set_planned_dates(items : Array(PlannedDate))
|
abstract def get_{{name}} : Array({{dtype}})
|
||||||
abstract def get_work_records : Array(WorkRecord)
|
abstract def set_{{name}}(items : Array({{dtype}}))
|
||||||
abstract def set_work_records(items : Array(WorkRecord))
|
end
|
||||||
|
|
||||||
|
st_abstract_def(planned_dates, PlannedDate)
|
||||||
|
st_abstract_def(work_records, WorkRecord)
|
||||||
end
|
end
|
||||||
|
|
||||||
class MemoryStorage < Storage
|
class MemoryStorage < Storage
|
||||||
@planned_dates = [] of PlannedDate
|
macro st_memory_def(name, dtype)
|
||||||
@work_records = [] of WorkRecord
|
@{{name}} = [] of {{dtype}}
|
||||||
|
|
||||||
def get_planned_dates : Array(PlannedDate)
|
def get_{{name}} : Array({{dtype}})
|
||||||
@planned_dates
|
@{{name}}
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_planned_dates(items : Array(PlannedDate))
|
def set_{{name}}(items : Array({{dtype}}))
|
||||||
@planned_dates = items
|
@{{name}} = items
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_work_records : Array(WorkRecord)
|
st_memory_def(planned_dates, PlannedDate)
|
||||||
@work_records
|
st_memory_def(work_records, WorkRecord)
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_work_records(items : Array(WorkRecord))
|
class FileStorage < Storage
|
||||||
@work_records = items
|
def initialize(@path : String)
|
||||||
|
end
|
||||||
|
|
||||||
|
macro st_file_def(name, dtype, file)
|
||||||
|
@{{name}} = [] of {{dtype}}
|
||||||
|
|
||||||
|
def get_{{name}} : Array({{dtype}})
|
||||||
|
fname = File.join(@path, {{file}})
|
||||||
|
if File.exists? fname
|
||||||
|
content = File.read(fname)
|
||||||
|
Array({{dtype}}).from_json content
|
||||||
|
else
|
||||||
|
[] of {{dtype}}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def set_{{name}}(items : Array({{dtype}}))
|
||||||
|
fname = File.join(@path, {{file}})
|
||||||
|
content = items.to_pretty_json
|
||||||
|
File.write fname, content
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
st_file_def(planned_dates, PlannedDate, "planed-dates.json")
|
||||||
|
st_file_def(work_records, WorkRecord, "work_records.json")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user