Class: Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/wonko_the_sane/registry.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instanceObject



50
51
52
53
# File 'lib/wonko_the_sane/registry.rb', line 50

def self.instance
  Dir.mkdir 'files' unless Dir.exist? 'files'
  @@instance ||= Registry.new
end

Instance Method Details

#indexObject



12
13
14
15
# File 'lib/wonko_the_sane/registry.rb', line 12

def index
  return { index: [] } unless File.exists? 'files/index.json'
  $rw.read_index JSON.parse File.read 'files/index.json'
end

#retrieve(id, version) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/wonko_the_sane/registry.rb', line 42

def retrieve(id, version)
  if File.exist? WonkoVersion.local_filename(id, version)
    $rw.read_version JSON.parse File.read(WonkoVersion.local_filename id, version)
  else
    nil
  end
end

#store(version) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/wonko_the_sane/registry.rb', line 17

def store(version)
  if version.is_a? Array
    version.each do |f| store(f) end
  else
    BaseSanitizer.sanitize(version, DownloadsFixer).each do |version|
      Dir.mkdir 'files/' + version.uid unless Dir.exist? 'files/' + version.uid
      File.write version.local_filename, JSON.pretty_generate($rw.write_version version)
      WonkoTheSane.wonkoweb_uploader.version_changed version.uid, version.version

      vindex = version_index version.uid
      vindex.add_version version
      store_version_index vindex

      ind = index
      next if ind[:index].find { |i| version.uid == i[:uid] } # early exit if the uid already exists in the index
      ind[:formatVersion] = 0
      ind[:index] << { uid: version.uid }
      File.write 'files/index.json', JSON.pretty_generate($rw.write_index ind)
    end
  end
rescue Exception => e
  Logging.logger[version.uid].error 'Unable to store: ' + version.version
  raise e
end

#store_version_index(index) ⇒ Object



7
8
9
10
# File 'lib/wonko_the_sane/registry.rb', line 7

def store_version_index(index)
  File.write index.local_filename, JSON.pretty_generate($rw.write_version_index index)
  WonkoTheSane.wonkoweb_uploader.file_changed index.uid
end

#version_index(uid) ⇒ Object



2
3
4
5
# File 'lib/wonko_the_sane/registry.rb', line 2

def version_index(uid)
  return VersionIndex.new uid unless File.exists? VersionIndex.local_filename uid
  $rw.read_version_index JSON.parse File.read(VersionIndex.local_filename uid)
end