Class: Gemvault::Tarvault

Inherits:
Object
  • Object
show all
Extended by:
VaultSession
Includes:
GemExtraction
Defined in:
lib/gemvault/tarvault.rb

Overview

A Tarvault: a tarball whose first entry is the manifest and whose remaining entries are .gem files. Portable, dependency-free storage.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from VaultSession

open

Methods included from GemExtraction

#spec_from_blob, #specs, #with_gem_file

Constructor Details

#initialize(path, create: false) ⇒ Tarvault

Returns a new instance of Tarvault.



22
23
24
25
26
27
# File 'lib/gemvault/tarvault.rb', line 22

def initialize(path, create: false)
  @path = Pathname(path).expand_path
  @archive = Tarball.new(@path)
  @closed = false
  create ? create_vault! : load_manifest!
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



20
21
22
# File 'lib/gemvault/tarvault.rb', line 20

def path
  @path
end

Instance Method Details

#add(gem_path, created_at: nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/gemvault/tarvault.rb', line 29

def add(gem_path, created_at: nil)
  gem_path = Pathname(gem_path).expand_path
  raise Vault::NotFoundError, "Gem file not found: #{gem_path}" unless gem_path.file?

  spec = spec_from_gem_file(gem_path)
  entry = GemEntry.from_spec(spec, created_at: Timestamp.canonical(created_at || Timestamp.now))
  raise_if_unwritable(entry, gem_path:)
  raise_if_duplicate(entry)
  store(entry:, bytes: gem_path.binread)
end

#closeObject



72
73
74
# File 'lib/gemvault/tarvault.rb', line 72

def close
  @closed = true
end

#closed?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/gemvault/tarvault.rb', line 76

def closed?
  @closed
end

#format_versionObject



64
65
66
# File 'lib/gemvault/tarvault.rb', line 64

def format_version
  @manifest.format_version
end

#gem_data(entry) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/gemvault/tarvault.rb', line 50

def gem_data(entry)
  record = @manifest.find(entry)
  raise Vault::NotFoundError, "Gem not found: #{entry}" unless record

  bytes = @archive.read(record.filename)
  raise Vault::Error, "Integrity check failed for #{record.filename}" unless record.matches?(bytes)

  bytes
end

#gem_entriesObject



60
61
62
# File 'lib/gemvault/tarvault.rb', line 60

def gem_entries
  @manifest.gem_entries
end

#remove(reference) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/gemvault/tarvault.rb', line 40

def remove(reference)
  dropped = @manifest.matching(reference)
  return 0 if dropped.empty?

  @manifest = @manifest.without(dropped)
  remaining = survivors_excluding(dropped)
  rewrite(remaining)
  dropped.size
end

#sizeObject



68
69
70
# File 'lib/gemvault/tarvault.rb', line 68

def size
  @manifest.size
end