Class: UtilityPack::PersistentHash
- Inherits:
-
Object
- Object
- UtilityPack::PersistentHash
- Defined in:
- lib/databasedotcom_additions/persistent_hash.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#initialize(path) ⇒ PersistentHash
constructor
A new instance of PersistentHash.
- #json_parse_file(path) ⇒ Object
- #load ⇒ Object
- #save(h) ⇒ Object
Constructor Details
#initialize(path) ⇒ PersistentHash
Returns a new instance of PersistentHash.
7 8 9 |
# File 'lib/databasedotcom_additions/persistent_hash.rb', line 7 def initialize path @path = path end |
Instance Method Details
#[](key) ⇒ Object
23 24 25 |
# File 'lib/databasedotcom_additions/persistent_hash.rb', line 23 def [](key) load[key] end |
#[]=(key, value) ⇒ Object
27 28 29 30 31 |
# File 'lib/databasedotcom_additions/persistent_hash.rb', line 27 def []=(key, value) h = load h[key] = value save(h) end |
#json_parse_file(path) ⇒ Object
11 12 13 |
# File 'lib/databasedotcom_additions/persistent_hash.rb', line 11 def json_parse_file(path) JSON.parse(File.open(path).read) end |
#load ⇒ Object
15 16 17 |
# File 'lib/databasedotcom_additions/persistent_hash.rb', line 15 def load File.exists?(@path) ? json_parse_file(@path) : {} end |
#save(h) ⇒ Object
19 20 21 |
# File 'lib/databasedotcom_additions/persistent_hash.rb', line 19 def save h File.open(@path, 'w') {|f| f.write(h.to_json)} end |