Class: Safeguard::Hasher
- Includes:
- Enumerable
- Defined in:
- lib/safeguard/hasher.rb
Overview
Hashes a set of files.
Instance Attribute Summary collapse
-
#files ⇒ Object
The files which will be hashed.
-
#functions ⇒ Object
The hash functions to use.
Instance Method Summary collapse
-
#each(&block) ⇒ Object
Yields file => hash data pairs or returns an enumerator.
-
#hash_files! ⇒ Object
Calculates the hash of each file.
-
#initialize(*args) ⇒ Hasher
constructor
Initializes a new hasher with the given files.
-
#results ⇒ Object
Returns the cached results containing the hashes calculated for each file.
Methods inherited from Worker
Constructor Details
#initialize(*args) ⇒ Hasher
26 27 28 29 30 31 32 33 34 |
# File 'lib/safeguard/hasher.rb', line 26 def initialize(*args) = args.extract_ribbon! funcs = .functions? do raise ArgumentError, 'No hash functions specified' end self.functions = [*funcs] self.files = args initialize_callbacks_from end |
Instance Attribute Details
#files ⇒ Object
The files which will be hashed.
12 13 14 |
# File 'lib/safeguard/hasher.rb', line 12 def files @files end |
#functions ⇒ Object
The hash functions to use.
15 16 17 |
# File 'lib/safeguard/hasher.rb', line 15 def functions @functions end |
Instance Method Details
#each(&block) ⇒ Object
Yields file => hash data pairs or returns an enumerator.
59 60 61 |
# File 'lib/safeguard/hasher.rb', line 59 def each(&block) Ribbon.wrap(results).each &block end |
#hash_files! ⇒ Object
Calculates the hash of each file. Updates the cached hash results.
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/safeguard/hasher.rb', line 37 def hash_files! results = Ribbon.new files.each do |file| functions.each do |function| call_callback before_hashing, file, function results[file][function] = result = Safeguard::Digest.file file, function call_callback after_hashing, file, function, result end end results = Ribbon.wrap results results.wrap_all! @results = results end |
#results ⇒ Object
Returns the cached results containing the hashes calculated for each file.
In order to force hash recalculation, call #hash_files!.
54 55 56 |
# File 'lib/safeguard/hasher.rb', line 54 def results @results ||= hash_files! end |