Created
February 8, 2022 05:32
-
-
Save havenwood/ddc1dcbac4867d7c443adf08a4a481f3 to your computer and use it in GitHub Desktop.
Handling nested PStore transactions (IRC example)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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