Skip to content

Instantly share code, notes, and snippets.

@pachacamac
Last active July 13, 2018 15:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pachacamac/54efea9abcde6d34fa00 to your computer and use it in GitHub Desktop.
Save pachacamac/54efea9abcde6d34fa00 to your computer and use it in GitHub Desktop.
a super simple pstore based key value store for minimal db-needs
require 'pstore'
class Store
def initialize(file, thread_safe = true)
@store = PStore.new(file, thread_safe)
end
def crud(key=nil, val=nil)
val ? @store.transaction{ @store[key] = val }
: @store.transaction(false){ key ? @store[key] : Hash[@store.roots.map{|k| [k, @store[k]]}] }
end
end
mydb = Store.new('db.pstore')
# save something to the store
mydb.crud(:foo, 'bar')
# read something from the store
mydb.crud(:foo) # 'bar'
mydb.crud(:xxx) # nil
# get the whole store as a Hash
mydb.store # {:foo => 'bar'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment