PStore Meets YAML
I love the
PStore standard library. It's a very graceful interface to get some fairly robust serialized mini-database handling in just a few lines. With it you get:
- Transactions with commit and rollbacks (automatic on exception).
- File locking, shared and exclusive.
- Multiprocessing safety.
PStore does even more, including some file mode checking and MD5 hashing to avoid unneeded writes, but the above are the major selling points for me.
Now, if I had to level any one complaint at
PStore, it would be that because it uses
Marshal under the hood it doesn't create files you can easily browse or tweak by hand. (
Marshal is a feature, don't get me wrong. It's fast, which is very helpful.) Sometimes though I want
PStore protection with the
YAML file format.
I'm embarrassed to admit that I use to use a hack for this:
require "pstore" require "yaml" class PStore; Marshal = YAML; end
That just redefines the
Marshal constant in a scope that should only alter
PStore. The library only uses
load() and those methods work the same with
Ready for the punch-line?
I learned today that my fragile hack has been in vain, no matter how clever it may be.
YAML ships with a file that will load and modify
PStore for you. Usage is as simple as:
From there just replace your
PStore.new() calls with
YAML::Store.new() and you're in business.
YAML::Store is a subclass of
PStore, so you won't need to change one bit of the API to get
PStore robustness with