Class: Quby::Questionnaires::Repos::DiskRepo

Inherits:
Base
  • Object
show all
Defined in:
lib/quby/questionnaires/repos/disk_repo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#all, #create!, #update!

Constructor Details

#initialize(path) ⇒ DiskRepo

Returns a new instance of DiskRepo.



12
13
14
15
# File 'lib/quby/questionnaires/repos/disk_repo.rb', line 12

def initialize(path)
  @path = path
  @questionnaire_cache = {}
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/quby/questionnaires/repos/disk_repo.rb', line 10

def path
  @path
end

Instance Method Details

#exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
# File 'lib/quby/questionnaires/repos/disk_repo.rb', line 31

def exists?(key)
  questionnaire_path = questionnaire_path(key)
  File.exist?(questionnaire_path)
end

#find(key) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/quby/questionnaires/repos/disk_repo.rb', line 23

def find(key)
  fail(QuestionnaireNotFound, key) unless exists?(key)
  path = questionnaire_path(key)
  definition = File.read(path)
  timestamp  = timestamp(key)
  entity(key, definition, timestamp, path)
end

#keysObject



17
18
19
20
21
# File 'lib/quby/questionnaires/repos/disk_repo.rb', line 17

def keys
  Dir[File.join(path, "*.rb")].map do |filename|
    File.basename(filename, '.rb')
  end
end

#timestamp(key) ⇒ Object



36
37
38
# File 'lib/quby/questionnaires/repos/disk_repo.rb', line 36

def timestamp(key)
  Time.at(File.mtime(questionnaire_path(key)).to_i)
end