Class: Drum::PersistentHash
- Defined in:
- lib/drum/utils/persist.rb
Overview
A wrapper around a hash that stores values persistently in a YAML.
Instance Attribute Summary collapse
-
#value ⇒ Hash
The wrapped hash.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Reads a mapping from the hash.
-
#[]=(key, value) ⇒ Object
Writes a mapping to the hash and stores it on disk.
-
#initialize(file_path, value = {}) ⇒ PersistentHash
constructor
Creates a new persistent hash.
-
#load ⇒ Object
Loads the hash from the file.
-
#store ⇒ Object
Saves the hash to the file.
Methods included from Log
Methods included from YAMLUtils
Constructor Details
#initialize(file_path, value = {}) ⇒ PersistentHash
Creates a new persistent hash.
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/drum/utils/persist.rb', line 18 def initialize(file_path, value={}) @file_path = file_path begin self.load rescue StandardError => e unless e.is_a?(Errno::ENOENT) log.warn "Could not load #{file_path}: #{e.inspect}... creating from scratch" end @value = value self.store end end |
Instance Attribute Details
#value ⇒ Hash
Returns The wrapped hash.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/drum/utils/persist.rb', line 10 class PersistentHash include Log, YAMLUtils attr_reader :value # Creates a new persistent hash. # # @param [String] file_path The path to the stored YAML file (may be non-existent). # @param [Hash] value The initial default value, if the file doesn't exist yet or is malformed def initialize(file_path, value={}) @file_path = file_path begin self.load rescue StandardError => e unless e.is_a?(Errno::ENOENT) log.warn "Could not load #{file_path}: #{e.inspect}... creating from scratch" end @value = value self.store end end # Loads the hash from the file. def load @value = from_yaml(File.read(@file_path)) end # Saves the hash to the file. def store File.write(@file_path, @value.to_yaml) end # Writes a mapping to the hash and stores it on disk. # # @param [Object] key The key to use. # @param [Object] value The value to map the key to. def []=(key, value) @value[key] = value store end # Reads a mapping from the hash. # # @param [Object] key The key to use. # @return [Object] The value the key is mapped to. def [](key) @value[key] end end |
Instance Method Details
#[](key) ⇒ Object
Reads a mapping from the hash.
54 55 56 |
# File 'lib/drum/utils/persist.rb', line 54 def [](key) @value[key] end |
#[]=(key, value) ⇒ Object
Writes a mapping to the hash and stores it on disk.
45 46 47 48 |
# File 'lib/drum/utils/persist.rb', line 45 def []=(key, value) @value[key] = value store end |
#load ⇒ Object
Loads the hash from the file.
32 33 34 |
# File 'lib/drum/utils/persist.rb', line 32 def load @value = from_yaml(File.read(@file_path)) end |
#store ⇒ Object
Saves the hash to the file.
37 38 39 |
# File 'lib/drum/utils/persist.rb', line 37 def store File.write(@file_path, @value.to_yaml) end |