Sha256: 5e93c6fca92f643ccc5bf55265987070e204b32cba9ff3a9039ecd8db6835f2d

Contents?: true

Size: 1.06 KB

Versions: 2

Compression:

Stored size: 1.06 KB

Contents

require 'yaml'

# A store backed by a persistent on-disk yaml file.
class Robut::Storage::YamlStore < Robut::Storage::Base
    
  class << self

    # The path to the file this store will persist to. 
    attr_reader :file

    # Sets the path to the file this store will persist to, and forces
    # a reload of all of the data.
    def file=(f)
      @file = f
      @internal = nil # force reload
      @file
    end

    # Sets the key +k+ to the value +v+ 
    def []=(k, v)
      internal[k] = v
      persist!
      v
    end

    # Returns the value at the key +k+.
    def [](k)
      internal[k]
    end

    private

    # The internal in-memory representation of the yaml file
    def internal
      @internal ||= begin
        YAML.load_file(file) rescue {}
      end
    end
    
    # Persists the data in this store to disk. Throws an exception if
    # we don't have a file set.
    def persist!
      raise "Robut::Storage::YamlStore.file must be set" unless file
      f = File.open(file, "w")
      f.puts internal.to_yaml
      f.close
    end
    
  end
    
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
robut-0.2.1 lib/robut/storage/yaml_store.rb
robut-0.2.0 lib/robut/storage/yaml_store.rb