Class: Drum::PersistentHash

Inherits:
Object show all
Includes:
Log, YAMLUtils
Defined in:
lib/drum/utils/persist.rb

Overview

A wrapper around a hash that stores values persistently in a YAML.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log

#log

Methods included from YAMLUtils

#from_yaml

Constructor Details

#initialize(file_path, value = {}) ⇒ PersistentHash

Creates a new persistent hash.

Parameters:

  • file_path (String)

    The path to the stored YAML file (may be non-existent).

  • value (Hash) (defaults to: {})

    The initial default value, if the file doesn't exist yet or is malformed



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

#valueHash

Returns The wrapped hash.

Returns:

  • (Hash)

    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.

Parameters:

  • key (Object)

    The key to use.

Returns:

  • (Object)

    The value the key is mapped to.



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.

Parameters:

  • key (Object)

    The key to use.

  • value (Object)

    The value to map the key to.



45
46
47
48
# File 'lib/drum/utils/persist.rb', line 45

def []=(key, value)
  @value[key] = value
  store
end

#loadObject

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

#storeObject

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