class Nutella::PersistedHash

This class behaves similarly to a regular Hash but it persists every operation to the file passed in the constructor. Not all Hash operations are supported and we added some of our own.

Public Class Methods

new(file) click to toggle source
# File lib/config/persisted_hash.rb, line 11
def initialize(file)
  @file=file
end

Public Instance Methods

[]( key ) click to toggle source
# File lib/config/persisted_hash.rb, line 15
def []( key )
  hash = load_hash
  hash[key]
end
[]=( key, val ) click to toggle source
# File lib/config/persisted_hash.rb, line 20
def []=( key, val )
  hash = load_hash
  hash[key]=val
  store_hash hash
end
add_key_value?(key, val) click to toggle source

Adds a <key, value> pair to the PersistedHash _only if_ there is currently no value associated with the specified key. @return [Boolean] false if the key already exists, true if the <key, value> pair was added successfully

# File lib/config/persisted_hash.rb, line 72
def add_key_value?(key, val)
  hash = load_hash
  return false if hash.key? key
  hash[key] = val
  store_hash hash
  true
end
delete( key ) click to toggle source
# File lib/config/persisted_hash.rb, line 26
def delete( key )
  hash = load_hash
  return_value = hash.delete key
  store_hash hash
  return_value
end
delete_key_value?( key ) click to toggle source

Removes a <key, value> pair from the PersistedHash _only if_ there is currently a value associated with the specified key. @return [Boolean] false if there is no value associated with the specified key, true otherwise

# File lib/config/persisted_hash.rb, line 84
def delete_key_value?( key )
  hash = load_hash
  return false if hash.delete(key).nil?
  store_hash hash
  true
end
empty?() click to toggle source
# File lib/config/persisted_hash.rb, line 33
def empty?
  hash = load_hash
  hash.empty?
end
has_key?( key ) click to toggle source
# File lib/config/persisted_hash.rb, line 38
def has_key?( key )
  hash = load_hash
  hash.has_key? key
end
include?( key ) click to toggle source
# File lib/config/persisted_hash.rb, line 43
def include?( key )
  has_key? key
end
keys() click to toggle source
# File lib/config/persisted_hash.rb, line 56
def keys
  hash = load_hash
  hash.keys
end
length() click to toggle source
# File lib/config/persisted_hash.rb, line 61
def length
  hash = load_hash
  hash.length
end
remove_file() click to toggle source

Removes the file the hash is persisted to

# File lib/config/persisted_hash.rb, line 93
def remove_file
  File.delete(@file) if File.exist?(@file)
end
to_h() click to toggle source
# File lib/config/persisted_hash.rb, line 52
def to_h
  load_hash
end
to_s() click to toggle source
# File lib/config/persisted_hash.rb, line 47
def to_s
  hash = load_hash
  hash.to_s
end

Private Instance Methods

load_hash() click to toggle source
# File lib/config/persisted_hash.rb, line 108
def load_hash
  begin
    return JSON.parse IO.read @file
  rescue
    # File doesn't exist, return new empty Hash
    Hash.new
  end
end
store_hash(hash) click to toggle source
# File lib/config/persisted_hash.rb, line 99
def store_hash(hash)
  dirname = File.dirname(@file)
  FileUtils.mkdir_p(dirname) unless File.directory?(dirname)
  File.open(@file, 'w+') do |f|
    f.write(JSON.pretty_generate(hash))
  end
  File.chmod(0777, @file)
end