Class: Gemvault::VaultDestination

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

Overview

Where a new vault is about to be written: the .gemv path itself, and the directories that have to exist before anything can be written to it.

A vault is one file, so gemvault new vendor/vendored.gemv in a tree that has no vendor yet reads as an ordinary request rather than a mistake, and the missing directories are created. What cannot be papered over -- something already occupying the path, a parent that is a file, a parent that cannot be created -- is raised as Error, so the CLI reports one line instead of a Ruby backtrace.

Defined Under Namespace

Classes: Error

Constant Summary collapse

SUFFIX =
".gemv".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ VaultDestination

Returns a new instance of VaultDestination.



24
25
26
27
# File 'lib/gemvault/vault_destination.rb', line 24

def initialize(name)
  locator = suffixed(name.to_s)
  @path = Pathname(locator)
end

Instance Attribute Details

#pathObject (readonly)

The vault's path, with SUFFIX appended if the name lacked it.



22
23
24
# File 'lib/gemvault/vault_destination.rb', line 22

def path
  @path
end

Instance Method Details

#create_parentsObject

Creates the vault's missing parent directories.

Raises:



45
46
47
48
49
50
51
52
# File 'lib/gemvault/vault_destination.rb', line 45

def create_parents
  parent = @path.dirname
  return if parent.directory?

  raise Error, "#{parent} is not a directory" if parent.exist?

  make(parent)
end

#missing_directoryObject

:call-seq:

missing_directory -> Pathname or nil

The shallowest directory create_parents would have to make -- creating it brings every deeper one with it -- or nil when the parent already exists.



40
41
42
# File 'lib/gemvault/vault_destination.rb', line 40

def missing_directory
  @path.dirname.ascend.reject(&:exist?).last
end

#refuse_existingObject

Raises Error when something already occupies the vault's path.

Raises:



30
31
32
# File 'lib/gemvault/vault_destination.rb', line 30

def refuse_existing
  raise Error, "#{@path} already exists" if @path.exist?
end