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 manifest.json 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.



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

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.



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

def path
  @path
end

Instance Method Details

#add(gem_path, created_at: nil) ⇒ Object



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

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: created_at || timestamp)
  raise_if_duplicate(entry)
  store(entry:, bytes: gem_path.binread)
end

#closeObject



70
71
72
# File 'lib/gemvault/tarvault.rb', line 70

def close
  @closed = true
end

#closed?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/gemvault/tarvault.rb', line 74

def closed?
  @closed
end

#format_versionObject



62
63
64
# File 'lib/gemvault/tarvault.rb', line 62

def format_version
  @manifest.format_version
end

#gem_data(entry) ⇒ Object



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

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



58
59
60
# File 'lib/gemvault/tarvault.rb', line 58

def gem_entries
  @manifest.gem_entries
end

#remove(reference) ⇒ Object



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

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



66
67
68
# File 'lib/gemvault/tarvault.rb', line 66

def size
  @manifest.size
end