Class: Safeguard::Repository

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

Overview

Directory which holds file-related data.

Defined Under Namespace

Classes: HashTable

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.



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

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

Instance Attribute Details

#directoryObject Also known as: dir

Returns the value of attribute directory.



19
20
21
# File 'lib/safeguard/repository.rb', line 19

def directory
  @directory
end

Class Method Details

.directory_in(dir) ⇒ Object

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



94
95
96
# File 'lib/safeguard/repository.rb', line 94

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)


105
106
107
# File 'lib/safeguard/repository.rb', line 105

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

Instance Method Details

#add!(*files) ⇒ Object

Adds the given files to the repository.



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

def add!(*files)
  hash_table.add *files
end

#before_save(&block) ⇒ Object

Calls the block and ensures that all data is persisted afterwards.



50
51
52
53
54
# File 'lib/safeguard/repository.rb', line 50

def before_save(&block)
  if block.arity == 1 then block.call self else instance_eval &block end
ensure
  save_hash_table
end

#create_directory!Object

Creates a directory for this repository, if one doesn’t exist. The directory will have the name defined in the DIRECTORY_NAME constant.



29
30
31
# File 'lib/safeguard/repository.rb', line 29

def create_directory!
  FileUtils.mkdir_p directory
end

#create_hasher_with(functions) ⇒ Object

Creates a Hasher for the files present in this repository.



77
78
79
# File 'lib/safeguard/repository.rb', line 77

def create_hasher_with(functions)
  Hasher.new *hash_table.files, :functions => functions
end

#create_verifier_for(*args) ⇒ Object

Creates a verifier using this repository’s hash table.



82
83
84
85
# File 'lib/safeguard/repository.rb', line 82

def create_verifier_for(*args)
  ribbon = args.extract_wrapped_ribbon!
  Verifier.new *args, ribbon.merge!(hash_table: hash_table)
end

#hash_and_add!(*args) ⇒ Object

Calculates the checksum of the given files and stores the results. args will be used to instantiate a new Hasher.

By default, the hashes of files already in the repository will not be recalculated. To force that, call with :force => true.



66
67
68
69
70
71
72
73
74
# File 'lib/safeguard/repository.rb', line 66

def hash_and_add!(*args)
  options = args.extract_ribbon!
  hasher = Hasher.new *args, options
  hasher.files.delete_if do |file|
    hasher.functions.any? { |function| hash_table.has_hash? file, function }
  end unless options.force?
  results = hasher.results
  hash_table.merge! results
end

#hash_tableObject

This repository’s HashTable. Lazily loaded from disk.



34
35
36
# File 'lib/safeguard/repository.rb', line 34

def hash_table
  @table ||= load_hash_table!
end

#hash_table_file_nameObject

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



39
40
41
# File 'lib/safeguard/repository.rb', line 39

def hash_table_file_name
  File.join directory, HASH_TABLE_FILE_NAME
end

#initialized?Boolean

Checks whether or not this repository’s directory has been created and initialized.

Returns:

  • (Boolean)


100
101
102
# File 'lib/safeguard/repository.rb', line 100

def initialized?
  File.directory? dir
end

#save_hash_tableObject

Saves this Repository’s HashTable with the filename returned by #hash_table_file_name.



45
46
47
# File 'lib/safeguard/repository.rb', line 45

def save_hash_table
  hash_table.save hash_table_file_name
end

#verify_files(*args) ⇒ Object

Recalculates the checksum of the given files and compares them to the stored values. args will be used to instantiate a new Verifier.



89
90
91
# File 'lib/safeguard/repository.rb', line 89

def verify_files(*args)
  create_verifier_for(*args).results
end