Class: Rstreet::ManifestBuilder

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

Overview

TODO: name something more general to manifests (does more than builds)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(manifest_file) ⇒ ManifestBuilder

Returns a new instance of ManifestBuilder.



8
9
10
11
# File 'lib/manifest_builder.rb', line 8

def initialize(manifest_file)
  @manifest = {}
  @manifest_file = manifest_file
end

Instance Attribute Details

#manifestObject (readonly)

Returns the value of attribute manifest.



6
7
8
# File 'lib/manifest_builder.rb', line 6

def manifest
  @manifest
end

Class Method Details

.empty_manifestObject



13
14
15
# File 'lib/manifest_builder.rb', line 13

def self.empty_manifest
  {}
end

Instance Method Details

#add(uploadable) ⇒ Object



32
33
34
# File 'lib/manifest_builder.rb', line 32

def add(uploadable)
  @manifest[uploadable.name] = uploadable.hash
end

#diff(other_manifest) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/manifest_builder.rb', line 36

def diff(other_manifest)
  # TODO: determine if this is the diff algorithm we want
  # TODO: in the future, account for deletions and delete objects from bucket
  # 8 [ruby-2.1.5]::rstreet]>a.to_set
  # => #<Set: {[:a, 1], [:b, 2], [:c, 3]}>
  #     9 [ruby-2.1.5]::rstreet]>a.to_set.difference b.to_set
  # => #<Set: {[:b, 2], [:c, 3]}>
  #     10 [ruby-2.1.5]::rstreet]>b.to_set.difference a.to_set
  # => #<Set: {[:b, 4]}>
  @manifest.to_set.difference(other_manifest.to_set).to_h.keys
end

#readObject



17
18
19
20
21
22
# File 'lib/manifest_builder.rb', line 17

def read
  gz = Zlib::GzipReader.new(StringIO.new(@manifest_file))
  @manifest = JSON.parse(gz.read).to_h
  gz.close
  @manifest
end

#writeObject



24
25
26
27
28
29
30
# File 'lib/manifest_builder.rb', line 24

def write()
  File.open(@manifest_file, "w") do |f|
    gz = Zlib::GzipWriter.new(f)
    gz.write @manifest.to_json.to_s
    gz.close
  end
end