Class: Nutella::PersistedHash

Inherits:
Object
  • Object
show all
Defined in:
lib/config/persisted_hash.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ PersistedHash

Returns a new instance of PersistedHash.



11
12
13
# File 'lib/config/persisted_hash.rb', line 11

def initialize(file)
  @file=file
end

Instance Method Details

#[](key) ⇒ Object



15
16
17
18
# File 'lib/config/persisted_hash.rb', line 15

def []( key )
  hash = load_hash
  hash[key]
end

#[]=(key, val) ⇒ Object



20
21
22
23
24
# 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) ⇒ Boolean

Adds a <key, value> pair to the PersistedHash _only if_ there is currently no value associated with the specified key. <key, value> pair was added successfully

Returns:

  • (Boolean)

    false if the key already exists, true if the



72
73
74
75
76
77
78
# 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) ⇒ Object



26
27
28
29
30
31
# 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) ⇒ Boolean

Removes a <key, value> pair from the PersistedHash _only if_ there is currently a value associated with the specified key. the specified key, true otherwise

Returns:

  • (Boolean)

    false if there is no value associated with



84
85
86
87
88
89
# 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?Boolean

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/config/persisted_hash.rb', line 33

def empty?
  hash = load_hash
  hash.empty?
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
# File 'lib/config/persisted_hash.rb', line 38

def has_key?( key )
  hash = load_hash
  hash.has_key? key
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/config/persisted_hash.rb', line 43

def include?( key )
  has_key? key
end

#keysObject



56
57
58
59
# File 'lib/config/persisted_hash.rb', line 56

def keys
  hash = load_hash
  hash.keys
end

#lengthObject



61
62
63
64
# File 'lib/config/persisted_hash.rb', line 61

def length
  hash = load_hash
  hash.length
end

#remove_fileObject

Removes the file the hash is persisted to



93
94
95
# File 'lib/config/persisted_hash.rb', line 93

def remove_file
  File.delete(@file) if File.exist?(@file)
end

#to_hObject



52
53
54
# File 'lib/config/persisted_hash.rb', line 52

def to_h
  load_hash
end

#to_sObject



47
48
49
50
# File 'lib/config/persisted_hash.rb', line 47

def to_s
  hash = load_hash
  hash.to_s
end