Class: JSONFilePersistedHash
- Inherits:
-
Object
- Object
- JSONFilePersistedHash
- Defined in:
- lib/util/json_file_persisted_hash.rb
Overview
An hash that is automatically persisted to a JSON file. This class behaves similarly to a regular Hash but it persists every operation to a specified JSON file. Not all Hash operations are supported and we added some of our own.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Methods borrowed from Ruby’s Hash class.
- #[]=(key, val) ⇒ Object
-
#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.
- #delete(key) ⇒ Object
-
#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.
- #empty? ⇒ Boolean
- #has_key?(key) ⇒ Boolean
- #include?(key) ⇒ Boolean
-
#initialize(file) ⇒ JSONFilePersistedHash
constructor
A new instance of JSONFilePersistedHash.
- #keys ⇒ Object
- #length ⇒ Object
- #merge!(other_hash) ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(file) ⇒ JSONFilePersistedHash
Returns a new instance of JSONFilePersistedHash.
9 10 11 |
# File 'lib/util/json_file_persisted_hash.rb', line 9 def initialize( file ) @store = JSONStore.new(file, true) end |
Instance Method Details
#[](key) ⇒ Object
Methods borrowed from Ruby’s Hash class
16 17 18 |
# File 'lib/util/json_file_persisted_hash.rb', line 16 def []( key ) @store.transaction { @store[key] } end |
#[]=(key, val) ⇒ Object
20 21 22 |
# File 'lib/util/json_file_persisted_hash.rb', line 20 def []=( key, val ) @store.transaction { @store[key]=val } 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
67 68 69 70 71 72 73 |
# File 'lib/util/json_file_persisted_hash.rb', line 67 def add_key_value?(key, val) @store.transaction do return false if @store.to_h.key? key @store[key]=val end true end |
#delete(key) ⇒ Object
24 25 26 |
# File 'lib/util/json_file_persisted_hash.rb', line 24 def delete( key ) @store.transaction { @store.delete key } 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
79 80 81 82 |
# File 'lib/util/json_file_persisted_hash.rb', line 79 def delete_key_value?( key ) @store.transaction { return false if @store.delete(key).nil? } true end |
#empty? ⇒ Boolean
28 29 30 |
# File 'lib/util/json_file_persisted_hash.rb', line 28 def empty? @store.transaction { @store.to_h.empty? } end |
#has_key?(key) ⇒ Boolean
32 33 34 |
# File 'lib/util/json_file_persisted_hash.rb', line 32 def has_key?( key ) @store.transaction { @store.to_h.has_key? key } end |
#include?(key) ⇒ Boolean
36 37 38 |
# File 'lib/util/json_file_persisted_hash.rb', line 36 def include?( key ) has_key? key end |
#keys ⇒ Object
48 49 50 |
# File 'lib/util/json_file_persisted_hash.rb', line 48 def keys @store.transaction { @store.to_h.keys } end |
#length ⇒ Object
52 53 54 |
# File 'lib/util/json_file_persisted_hash.rb', line 52 def length @store.transaction { @store.to_h.length } end |
#merge!(other_hash) ⇒ Object
56 57 58 |
# File 'lib/util/json_file_persisted_hash.rb', line 56 def merge!( other_hash ) @store.transaction { other_hash.each { |k, v| @store[k] = v } } end |
#to_h ⇒ Object
44 45 46 |
# File 'lib/util/json_file_persisted_hash.rb', line 44 def to_h @store.transaction { @store.to_h } end |
#to_s ⇒ Object
40 41 42 |
# File 'lib/util/json_file_persisted_hash.rb', line 40 def to_s @store.transaction { @store.to_h.to_s } end |