Sha256: a37487785eb17255a2212694361f60e6c4322c29963e4261d34aa54a0e05aeea

Contents?: true

Size: 1.79 KB

Versions: 13

Compression:

Stored size: 1.79 KB

Contents

require 'util/json_store'

# 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.
class JSONFilePersistedHash

  def initialize( file )
    @store = JSONStore.new(file, true)
  end


  # Methods borrowed from Ruby's Hash class

  def []( key )
    @store.transaction { @store[key] }
  end

  def []=( key, val )
    @store.transaction { @store[key]=val }
  end

  def delete( key )
    @store.transaction { @store.delete key }
  end

  def empty?
    @store.transaction { @store.to_h.empty? }
  end

  def has_key?( key )
    @store.transaction { @store.to_h.has_key? key }
  end

  def include?( key )
    has_key? key
  end

  def to_s
    @store.transaction { @store.to_h.to_s }
  end

  def to_h
    @store.transaction { @store.to_h }
  end

  def keys
    @store.transaction { @store.to_h.keys }
  end

  def length
    @store.transaction { @store.to_h.length }
  end


  # PersistedHash-only public methods

  # Adds a <key, value> pair to the PersistedHash _only if_
  # there is currently no value associated with the specified key.
  # @return [Boolean] false if the key already exists, true if the
  # <key, value> pair was added successfully
  def add_key_value?(key, val)
    @store.transaction do
      return false if @store.to_h.key? key
      @store[key]=val
    end
    true
  end

  # Removes a <key, value> pair from the PersistedHash _only if_
  # there is currently a value associated with the specified key.
  # @return [Boolean] false if there is no value associated with
  # the specified key, true otherwise
  def delete_key_value?( key )
    @store.transaction { return false if @store.delete(key).nil? }
    true
  end

end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
nutella_lib-0.4.15 lib/util/json_file_persisted_hash.rb
nutella_lib-0.4.14 lib/util/json_file_persisted_hash.rb
nutella_lib-0.4.13 lib/util/json_file_persisted_hash.rb
nutella_lib-0.4.12 lib/util/json_file_persisted_hash.rb
nutella_lib-0.4.11 lib/util/json_file_persisted_hash.rb
nutella_lib-0.4.10 lib/util/json_file_persisted_hash.rb
nutella_lib-0.4.9 lib/util/json_file_persisted_hash.rb
nutella_lib-0.4.8 lib/util/json_file_persisted_hash.rb
nutella_lib-0.4.7 lib/util/json_file_persisted_hash.rb
nutella_lib-0.4.6 lib/util/json_file_persisted_hash.rb
nutella_lib-0.4.5 lib/util/json_file_persisted_hash.rb
nutella_lib-0.4.4 lib/util/json_file_persisted_hash.rb
nutella_lib-0.4.3 lib/util/json_file_persisted_hash.rb