Add file storage
This commit is contained in:
parent
41176c7a1f
commit
73ed1eca06
@ -3,16 +3,6 @@ require "./spec_helper"
|
||||
module Dayoff::Test
|
||||
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
|
||||
storage = MemoryStorage.new
|
||||
storage.set_planned_dates [
|
||||
|
@ -1,2 +1,25 @@
|
||||
require "spec"
|
||||
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
|
||||
abstract class Storage
|
||||
abstract def get_planned_dates : Array(PlannedDate)
|
||||
abstract def set_planned_dates(items : Array(PlannedDate))
|
||||
abstract def get_work_records : Array(WorkRecord)
|
||||
abstract def set_work_records(items : Array(WorkRecord))
|
||||
macro st_abstract_def(name, dtype)
|
||||
abstract def get_{{name}} : Array({{dtype}})
|
||||
abstract def set_{{name}}(items : Array({{dtype}}))
|
||||
end
|
||||
|
||||
st_abstract_def(planned_dates, PlannedDate)
|
||||
st_abstract_def(work_records, WorkRecord)
|
||||
end
|
||||
|
||||
class MemoryStorage < Storage
|
||||
@planned_dates = [] of PlannedDate
|
||||
@work_records = [] of WorkRecord
|
||||
macro st_memory_def(name, dtype)
|
||||
@{{name}} = [] of {{dtype}}
|
||||
|
||||
def get_planned_dates : Array(PlannedDate)
|
||||
@planned_dates
|
||||
def get_{{name}} : Array({{dtype}})
|
||||
@{{name}}
|
||||
end
|
||||
|
||||
def set_planned_dates(items : Array(PlannedDate))
|
||||
@planned_dates = items
|
||||
def set_{{name}}(items : Array({{dtype}}))
|
||||
@{{name}} = items
|
||||
end
|
||||
end
|
||||
|
||||
def get_work_records : Array(WorkRecord)
|
||||
@work_records
|
||||
st_memory_def(planned_dates, PlannedDate)
|
||||
st_memory_def(work_records, WorkRecord)
|
||||
end
|
||||
|
||||
def set_work_records(items : Array(WorkRecord))
|
||||
@work_records = items
|
||||
class FileStorage < Storage
|
||||
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
|
||||
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user