Module: Gemmy::Patches::HashPatch::InstanceMethods::Persisted
- Defined in:
- lib/gemmy/patches/hash_patch.rb
Overview
Sets up a hash to mirror all changes to a database file All nested gets & sets require a list of keys, passed as subsequent args. Instead of [] and []=, use get and set
Everything in the db is contained in a hash with one predefined key, :data. The value is an empty, autovivified hash.
This also makes the caller hash autovivified
Example:
hash = {}.persisted(“db.yaml”) hash.set(:a, :b, 0) # => this writes to disk and memory hash.get(:a, :b) # => reads from memory hash.get(:a, :b, disk: true) # => reads from disk
Instance Method Summary collapse
Instance Method Details
#persisted(path) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/gemmy/patches/hash_patch.rb', line 83 def persisted(path) require 'yaml/store' autovivified = Gemmy::Patches::HashPatch::InstanceMethods::Autovivified.method(:_autovivified) autovivified.call(self).tap do |hash| hash.instance_exec do @db = YAML::Store.new path @db.transaction do @db[:data] = autovivified.call({}) end end hash.extend Gemmy::Patches::HashPatch::PersistedHash end end |