Class: Safeguard::Repository
- Inherits:
-
Object
- Object
- Safeguard::Repository
- 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
-
#directory ⇒ Object
(also: #dir)
Returns the value of attribute directory.
Class Method Summary collapse
-
.directory_in(dir) ⇒ Object
Returns the path to the repository relative to the given
dir
. -
.initialized?(dir) ⇒ Boolean
Checks whether or not a repository has been created in the given
dir
.
Instance Method Summary collapse
-
#hash_table ⇒ Object
Loads this repository’s HashTable.
-
#hash_table_file_name ⇒ Object
Returns the name of the HashTable file relative to this repository.
-
#initialize(dir) ⇒ Repository
constructor
Initializes a Safeguard repository in or from the given directory.
-
#track(filename) ⇒ Object
Adds a file to this repository’s HashTable, and saves it.
-
#verify(filename) ⇒ Object
Verifies whether or not the file still matches the original version.
-
#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, orfalse
, when otherwise.
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
#directory ⇒ Object 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.(dir), DIRECTORY_NAME end |
.initialized?(dir) ⇒ Boolean
Checks whether or not a repository has been created in the given dir
.
68 69 70 |
# File 'lib/safeguard/repository.rb', line 68 def self.initialized?(dir) File.directory? directory_in(dir) end |
Instance Method Details
#hash_table ⇒ Object
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_name ⇒ Object
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. 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.(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 |