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



60
61
62
63
64
# File 'lib/wonko_the_sane/registry.rb', line 60

def self.instance
  FileUtils.mkdir_p 'files' unless Dir.exist? 'files'
  FileUtils.mkdir_p out_dir unless out_dir.blank? || Dir.exists?(out_dir)
  @instance ||= Registry.new
end

.out_dirObject



66
67
68
# File 'lib/wonko_the_sane/registry.rb', line 66

def self.out_dir
  WonkoTheSane.configuration.out_dir
end

Instance Method Details

#indexObject



14
15
16
17
# File 'lib/wonko_the_sane/registry.rb', line 14

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



52
53
54
55
56
57
58
# File 'lib/wonko_the_sane/registry.rb', line 52

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

#store(version) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/wonko_the_sane/registry.rb', line 19

def store(version)
  if version.is_a? Array
    version.each do |f|
      store(f)
    end
  else
    BaseSanitizer.sanitize(version, DownloadsFixer).each do |ver|
      begin
        Dir.mkdir 'files/' + ver.uid unless Dir.exist? 'files/' + ver.uid
        File.write ver.local_filename + '.new', JSON.pretty_generate($rw.write_version ver)
        File.write ver.local_filename, JSON.pretty_generate($old_format.write_version ver)
        unless Registry.out_dir.blank?
          FileUtils.copy ver.local_filename + '.new', "#{Registry.out_dir}/#{ver.uid}-#{ver.version}.new.json"
          FileUtils.copy ver.local_filename, "#{Registry.out_dir}/#{ver.uid}-#{ver.version}.json"
        end

        vindex = version_index ver.uid
        vindex.add_version ver
        store_version_index vindex

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

#store_version_index(index) ⇒ Object



9
10
11
12
# File 'lib/wonko_the_sane/registry.rb', line 9

def store_version_index(index)
  File.write index.local_filename, JSON.pretty_generate($rw.write_version_index index)
  FileUtils.copy index.local_filename, "#{Registry.out_dir}/#{index.uid}.json" unless Registry.out_dir.blank?
end

#version_index(uid) ⇒ Object



4
5
6
7
# File 'lib/wonko_the_sane/registry.rb', line 4

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