Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created February 8, 2022 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/ddc1dcbac4867d7c443adf08a4a481f3 to your computer and use it in GitHub Desktop.
Save havenwood/ddc1dcbac4867d7c443adf08a4a481f3 to your computer and use it in GitHub Desktop.
Handling nested PStore transactions (IRC example)
require 'pstore'
class Store
def initialize(relative_path: '.data.pstore')
@store = PStore.new(relative_path, true)
@store.ultra_safe = true
@store.transaction(true) { }
@in_transaction = false
@store
end
def transaction(read_only = false)
return yield @store if @in_transaction
@in_transaction = true
@store.transaction read_only do
yield @store
end
ensure
@in_transaction = false
end
end
store = Store.new
store.transaction do
store.transaction do |data|
data[:meaning] = 42
end
end
store.transaction { |hash| hash[:meaning] }
#=> 42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment