Class: Safeguard::Repository

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

Overview

Directory which holds file-related data.

Constant Summary collapse

DIRECTORY_NAME =

Name of the directory in which the Safeguard repository resides.

'.safeguard'.freeze
HASH_TABLE_FILE_NAME =

Name of the file to which the HashTable is saved.

'hash_table'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ Repository

Initializes a Safeguard repository in or from the given directory.

If the given directory does not contain a repository directory, whose name is defined by the DIRECTORY_NAME constant, it will be created.



21
22
23
24
# File 'lib/safeguard/repository.rb', line 21

def initialize(dir)
  self.directory = Repository.directory_in(dir)
  FileUtils.mkdir_p directory
end

Instance Attribute Details

#directoryObject Also known as: dir

Returns the value of attribute directory.



14
15
16
# File 'lib/safeguard/repository.rb', line 14

def directory
  @directory
end

Class Method Details

.directory_in(dir) ⇒ Object

Returns the path to the repository relative to the given dir.



63
64
65
# File 'lib/safeguard/repository.rb', line 63

def self.directory_in(dir)
  File.join File.expand_path(dir), DIRECTORY_NAME
end

.initialized?(dir) ⇒ Boolean

Checks whether or not a repository has been created in the given dir.

Returns:

  • (Boolean)


68
69
70
# File 'lib/safeguard/repository.rb', line 68

def self.initialized?(dir)
  File.directory? directory_in(dir)
end

Instance Method Details

#hash_tableObject

Loads this repository’s HashTable. Creates a new one if unable to do so.



27
28
29
# File 'lib/safeguard/repository.rb', line 27

def hash_table
  HashTable.load hash_table_file_name rescue HashTable.new
end

#hash_table_file_nameObject

Returns the name of the HashTable file relative to this repository.



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

def hash_table_file_name
  File.join directory, HASH_TABLE_FILE_NAME
end

#track(filename) ⇒ Object

Adds a file to this repository’s HashTable, and saves it.



37
38
39
40
41
42
43
44
# File 'lib/safeguard/repository.rb', line 37

def track(filename)
  table = hash_table_file_name
  file = File.expand_path filename
  hash_table.instance_eval do
    add file
    save table
  end
end

#verify(filename) ⇒ Object

Verifies whether or not the file still matches the original version.

An exception will be raised if the given file isn’t in the repository.



49
50
51
# File 'lib/safeguard/repository.rb', line 49

def verify(filename)
  hash_table.verify File.expand_path(filename)
end

#verify_all(&block) ⇒ Object

Verifies all files present in this repository, and returns a hash of results associating a filename with either true, when the file is the same as the original version, or false, when otherwise.

If a block is given, the filename and the result will be yielded instead.



58
59
60
# File 'lib/safeguard/repository.rb', line 58

def verify_all(&block)
  hash_table.verify_all &block
end