Sha256: b04be7157f787e106ff5ff899735a3a0df33739d496bb0e669f08250287f59c4

Contents?: true

Size: 903 Bytes

Versions: 1

Compression:

Stored size: 903 Bytes

Contents

require 'pstore'

class SparaDisk
  def initialize(name)
    @store = PStore.new(name)
  end

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

  def get(key)
    @store.transaction(true) { @store.fetch(key, nil) }
  end

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

  def keys
    @store.transaction(true) { @store.roots }
  end

  def vals
    @store.transaction(true) do
      # @store.instance_variable_get(:@table).values
      @store.roots.inject([]) do |values, key|
        values.push(@store[key])
        values
      end
    end
  end

  def all
    @store.transaction(true) do
      # @store.instance_variable_get(:@table)
      @store.roots.inject({}) do |db, key|
        db[key] = @store[key]
        db
      end
    end
  end

  def clean!
    @store.transaction do
      @store.roots.each { |key| @store.delete(key) }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
spara-0.0.2 lib/spara/spara_disk.rb