ruby-mdbx/lib/mdbx/database.rb
mahlon@martini.nu d92ba7c5eb Checkpoint commit.
Fleshing out collections, automatic serialization of values.  Stringify
all keys.

FossilOrigin-Name: 8bb5e27eacd18bc34b60309a03cdce31921f790c821dab89bf12cd9bfc19825d
2020-12-16 08:29:50 +00:00

56 lines
1.1 KiB
Ruby

# -*- ruby -*-
# vim: set nosta noet ts=4 sw=4 ft=ruby:
# encoding: utf-8
require 'mdbx' unless defined?( MDBX )
# TODO: rdoc
#
class MDBX::Database
### Open an existing (or create a new) mdbx database at filesystem
### +path+. In block form, the database is automatically closed.
###
### MDBX::Database.open( path ) -> db
### MDBX::Database.open( path, options ) -> db
### MDBX::Database.open( path, options ) do |db|
### db[ 'key' ] #=> value
### end
###
def self::open( *args, &block )
db = new( *args )
db.serializer = ->( v ) { Marshal.dump( v ) }
db.deserializer = ->( v ) { Marshal.load( v ) }
if block_given?
begin
yield db
ensure
db.close
end
end
return db
end
# Only instantiate Database objects via #open.
private_class_method :new
# The options used to instantiate this database.
attr_reader :options
# The path on disk of the database.
attr_reader :path
# A Proc for automatically serializing values.
attr_accessor :serializer
# A Proc for automatically deserializing values.
attr_accessor :deserializer
end # class MDBX::Database