Class: Safeguard::Repository::HashTable

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

Overview

Holds filename => checksum pairs.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ribbon = nil, &block) ⇒ HashTable

Initializes this hash table with the contents of the given Ribbon.



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

def initialize(ribbon = nil, &block)
  ribbon = ribbon.ribbon if self.class === ribbon
  merge! ribbon, &block if ribbon
end

Class Method Details

.load(filename) ⇒ Object

Loads the HashTable from a YAML file.



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

def self.load(filename)
  new Ribbon::Wrapper.from_yaml File.read(filename)
end

Instance Method Details

#<<(filename) ⇒ Object

Same as #add, but returns self.



53
54
55
56
# File 'lib/safeguard/repository/hash_table.rb', line 53

def <<(filename)
  add filename
  self
end

#[](filename) ⇒ Object

Looks up the checksum data for the given filename.



41
42
43
# File 'lib/safeguard/repository/hash_table.rb', line 41

def [](filename)
  ribbon.ribbon[filename]
end

#add(*filenames) ⇒ Object

Adds a file to the hash table.

The file will not have any hash information associated with it.



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

def add(*filenames)
  filenames.each { |filename| self[filename] }
end

#fetch(*args, &block) ⇒ Object

Fetches a value. Same as Hash#fetch.



36
37
38
# File 'lib/safeguard/repository/hash_table.rb', line 36

def fetch(*args, &block)
  ribbon.fetch *args, &block
end

#filesObject

Returns a list of files present in this hash table.



75
76
77
# File 'lib/safeguard/repository/hash_table.rb', line 75

def files
  ribbon.keys
end

#has_file?(filename) ⇒ Boolean

Returns whether or not the given file is present in this hash table.

Returns:

  • (Boolean)


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

def has_file?(filename)
  ribbon.has_key? filename
end

#has_hash?(filename, function) ⇒ Boolean

Returns whether the given file has a hash associated with the function stored.

Returns:

  • (Boolean)


70
71
72
# File 'lib/safeguard/repository/hash_table.rb', line 70

def has_hash?(filename, function)
  has_file?(filename) and self[filename].fetch function, nil
end

#has_hashes?(filename, *functions) ⇒ Boolean

Returns whether the given file has any hash information associated.

Returns:

  • (Boolean)


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

def has_hashes?(filename, *functions)
  has_file?(filename) and not self[filename].empty?
end

#merge!(other, &block) ⇒ Object

Merges this hash table’s data with the other’s.



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

def merge!(other, &block)
  ribbon.deep_merge! other, &block
end

#ribbonObject

The underlying wrapped ribbon used to store filenames and their associated hashes.



81
82
83
84
85
86
87
88
# File 'lib/safeguard/repository/hash_table.rb', line 81

def ribbon
  (@ribbon ||= Ribbon::Wrapper.new).tap do |ribbon|
    # TODO: Possible bottleneck here. #wrap_all! is a recursive call.
    # Keep its usage localized instead of calling it on every access?
    # Once after every modification, perhaps?
    ribbon.wrap_all!
  end
end

#save(filename) ⇒ Object

Saves the HashTable to a YAML file.



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

def save(filename)
  File.open(filename, 'w') do |file|
    file.puts ribbon.to_yaml
  end
end