Class: Ditz::FileStorage

Inherits:
Object show all
Defined in:
lib/ditz/file-storage.rb

Overview

stores ditz database on disk

Constant Summary collapse

PROJECT_FN =
"project.yaml"
ISSUE_FN_GLOB =
"issue-*.yaml"

Instance Method Summary collapse

Constructor Details

#initialize(base_dir) ⇒ FileStorage

Returns a new instance of FileStorage.



10
11
12
13
# File 'lib/ditz/file-storage.rb', line 10

def initialize base_dir
  @base_dir = base_dir
  @project_fn = File.join @base_dir, PROJECT_FN
end

Instance Method Details

#filename_for_issue(i) ⇒ Object



49
# File 'lib/ditz/file-storage.rb', line 49

def filename_for_issue i; File.join @base_dir, ISSUE_TO_FN(i) end

#filename_for_projectObject



50
# File 'lib/ditz/file-storage.rb', line 50

def filename_for_project; @project_fn end

#ISSUE_TO_FN(i) ⇒ Object



8
# File 'lib/ditz/file-storage.rb', line 8

def ISSUE_TO_FN i; "issue-#{i.id}.yaml" end

#loadObject



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ditz/file-storage.rb', line 15

def load
  Ditz::debug "loading project from #{@project_fn}"
  project = Project.from @project_fn

  fn = File.join @base_dir, ISSUE_FN_GLOB
  Ditz::debug "loading issues from #{fn}"
  project.issues = Dir[fn].map { |fn| Issue.from fn }
  Ditz::debug "found #{project.issues.size} issues"

  project.issues.each { |i| i.project = project }
  project
end

#save(project) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ditz/file-storage.rb', line 28

def save project
  dirty = project.each_modelobject { |o| break true if o.changed? }
  if dirty
    Ditz::debug "project is dirty, saving #{@project_fn}"
    project.save! @project_fn
  end

  changed_issues = project.issues.select { |i| i.changed? }
  changed_issues.each do |i|
    fn = filename_for_issue i
    Ditz::debug "issue #{i.name} is dirty, saving #{fn}"
    i.save! fn
  end

  project.deleted_issues.each do |i|
    fn = filename_for_issue i
    Ditz::debug "issue #{i.name} has been deleted, deleting #{fn}"
    FileUtils.rm fn
  end
end